Skip to content

Updated for usage wit Typo3 v11 and PHP 7 including code cleanup #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
{
"name": "Miles Croxford",
"email": "[email protected]",
"homepage": "http://milescroxford.com",
"homepage": "https://milescroxford.com",
"role": "Developer"
}
],
"require": {
"php" : ">=5.4.0",
"symfony/filesystem": "^2.8 | ^3.0"
"symfony/filesystem": "^5.3"
},
"require-dev": {
"phpunit/phpunit" : "^4.8.36 || ^5.7 || ^6.5",
Expand Down
44 changes: 22 additions & 22 deletions src/Cache/CacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CacheProvider
*
* @var bool $attempted
*/
private $attempted = false;
private bool $attempted = false;

/**
* How long for the cache to be fresh
Expand All @@ -52,43 +52,43 @@ class CacheProvider
*
* @var bool $hit
*/
private $hit = false;
private bool $hit = false;

/**
* The loaded Vars object from cache
*
* @var \M1\Vars\Vars $loaded_vars
* @var Vars $loaded_vars
*/
private $loaded_vars;
private Vars $loaded_vars;

/**
* The cache file name
*
* @var string $name
*/
private $name;
private string $name;

/**
* Is the cache turned on
*
* @var boolean $provide
*/
private $provide;
private bool $provide;

/**
* If cached, the time when the class was cached
*
* @var int $time
*/
private $time;
private int $time;

/**
* Creates a new instance of the cacheProvider for Vars
*
* @param string|array $resource The main configuration resource
* @param array $options The options being used for Vars
* @param array $options The options being used for Vars
*/
public function __construct($resource, $options)
public function __construct($resource, array $options)
{
$this->setProvide($options['cache']);
$this->setPath($options['cache_path'], true);
Expand All @@ -102,7 +102,7 @@ public function __construct($resource, $options)
*
* @return bool Returns true if has the cached resource
*/
public function checkCache()
public function checkCache(): bool
{
if ($this->provide && $this->path && !$this->getAttempted()) {
$file = sprintf('%s/vars/%s.php', $this->path, $this->name);
Expand All @@ -129,7 +129,7 @@ public function load()
/**
* Transfer the contents of the parent Vars object into a file for cache
*
* @param \M1\Vars\Vars $vars Parent vars object
* @param Vars $vars Parent vars object
*
* @codeCoverageIgnore
*/
Expand All @@ -151,7 +151,7 @@ public function makeCache(Vars $vars)
*
* @return bool Is cache on or off
*/
public function getProvide()
public function getProvide(): bool
{
return $this->provide;
}
Expand All @@ -161,9 +161,9 @@ public function getProvide()
*
* @param bool $provide Does the cache want to be on or off
*
* @return \M1\Vars\Cache\CacheProvider
* @return CacheProvider
*/
public function setProvide($provide)
public function setProvide(bool $provide): CacheProvider
{
$this->provide = $provide;
return $this;
Expand All @@ -174,7 +174,7 @@ public function setProvide($provide)
*
* @return bool Has the cache been attempted
*/
public function getAttempted()
public function getAttempted(): bool
{
return $this->attempted;
}
Expand All @@ -184,15 +184,15 @@ public function getAttempted()
*
* @return int Cache expire time
*/
public function getExpire()
public function getExpire(): int
{
return $this->expire;
}

/**
* Returns how long the cache lasts for
*
* @return int Cache expire time
* @return bool Cache expire time
*/
public function isHit()
{
Expand All @@ -204,7 +204,7 @@ public function isHit()
*
* @return int Cache creation time
*/
public function getTime()
public function getTime(): int
{
return $this->time;
}
Expand All @@ -214,9 +214,9 @@ public function getTime()
*
* @param int $time Time when vars cached was created
*
* @return \M1\Vars\Cache\CacheProvider The cacheProvider object
* @return CacheProvider The cacheProvider object
*/
public function setTime($time)
public function setTime(int $time): CacheProvider
{
$this->time = $time;
return $this;
Expand All @@ -225,9 +225,9 @@ public function setTime($time)
/**
* Returns the loaded Vars object
*
* @return \M1\Vars\Vars The loaded Vars object
* @return Vars The loaded Vars object
*/
public function getLoadedVars()
public function getLoadedVars(): Vars
{
return $this->loaded_vars;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Loader/AbstractLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ abstract class AbstractLoader
*
* @var string
*/
protected $entity;
protected string $entity;

/**
* The supported extensions
*
* @var array
*/
public static $supported = array();
public static array $supported = array();

/**
* Construct the loader with the passed entity
*
* @param string $entity The passed entity
*/
public function __construct($entity)
public function __construct(string $entity)
{
$this->entity = $entity;
}
Expand All @@ -68,7 +68,7 @@ abstract public function load();
*
* @return bool Does the loader support the file
*/
public function supports()
public function supports(): bool
{
$extension = pathinfo($this->entity, PATHINFO_EXTENSION);
return in_array($extension, static::$supported);
Expand Down
37 changes: 22 additions & 15 deletions src/Loader/DirectoryLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,27 @@

namespace M1\Vars\Loader;

use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RegexIterator;

/**
* This file provides dir loading support for Vars
*
* @since 0.1.1
*/
class DirectoryLoader extends AbstractLoader
{
private bool $recursive;

/**
* Construct the loader with the passed entity
*
* @param string $entity The passed entity
* @param bool $recursive Search the directories recursively
* @param bool $recursive Search the directories recursively
*/
public function __construct($entity, $recursive)
public function __construct($entity, bool $recursive)
{
parent::__construct($entity);
$this->recursive = $recursive;
Expand All @@ -58,16 +65,16 @@ public function load()
/**
* Returns the supported files in the directory recursively
*
* @return \RegexIterator The supported files in the directories
* @return RegexIterator The supported files in the directories
*/
private function getSupportedFilesRecursively()
private function getSupportedFilesRecursively(): RegexIterator
{
$dir_it = new \RecursiveDirectoryIterator($this->entity, \RecursiveDirectoryIterator::SKIP_DOTS);
$dir_it = new RecursiveDirectoryIterator($this->entity, FilesystemIterator::SKIP_DOTS);

$files = new \RecursiveIteratorIterator(
$files = new RecursiveIteratorIterator(
$dir_it,
\RecursiveIteratorIterator::LEAVES_ONLY,
\RecursiveIteratorIterator::CATCH_GET_CHILD
RecursiveIteratorIterator::LEAVES_ONLY,
RecursiveIteratorIterator::CATCH_GET_CHILD
);

return $this->createRegexIterator($files);
Expand All @@ -76,9 +83,9 @@ private function getSupportedFilesRecursively()
/**
* Returns the supported files in the directory
*
* @return \RegexIterator The supported files in the directory
* @return RegexIterator The supported files in the directory
*/
private function getSupportedFiles()
private function getSupportedFiles(): RegexIterator
{
$files = new \FilesystemIterator($this->entity);

Expand All @@ -88,13 +95,13 @@ private function getSupportedFiles()
/**
* Returns the supported files in the directory
*
* @param \FilesystemIterator|\RecursiveIteratorIterator $files The found files in the directory/ies
* @param \FilesystemIterator|RecursiveIteratorIterator $files The found files in the directory/ies
*
* @return \RegexIterator The supported files in the directory using the regexiterator
* @return RegexIterator The supported files in the directory using the regexiterator
*/
private function createRegexIterator($files)
private function createRegexIterator($files): RegexIterator
{
return new \RegexIterator(
return new RegexIterator(
$files,
'/^.*\.(' . implode('|', static::$supported) . ')$/i'
);
Expand All @@ -107,7 +114,7 @@ private function createRegexIterator($files)
*
* @return array|bool The usable resources if any, else false
*/
private function makeResources($paths)
private function makeResources(array $paths)
{
if ($paths && !empty($paths)) {
$resources = array();
Expand Down
10 changes: 6 additions & 4 deletions src/Loader/EnvLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
namespace M1\Vars\Loader;

use M1\Env\Parser;
use RuntimeException;
use Throwable;

/**
* The Env file loader
Expand All @@ -30,19 +32,19 @@ class EnvLoader extends AbstractLoader
/**
* {@inheritdoc}
*/
public static $supported = array('env');
public static array $supported = array('env');

/**
* {@inheritdoc}
*
* @throws \RuntimeException If `m1/env` library is not installed or the file can not be parsed
* @throws RuntimeException If `m1/env` library is not installed or the file can not be parsed
*/
public function load()
{
try {
$this->content = Parser::parse(file_get_contents($this->entity));
} catch (\Exception $e) {
throw new \RuntimeException(sprintf(
} catch (Throwable $e) {
throw new RuntimeException(sprintf(
"%s threw an exception: %s",
$this->entity,
$e
Expand Down
9 changes: 6 additions & 3 deletions src/Loader/IniLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

namespace M1\Vars\Loader;

use RuntimeException;
use Throwable;

/**
* The ini file loader
*
Expand All @@ -28,7 +31,7 @@ class IniLoader extends AbstractLoader
/**
* {@inheritdoc}
*/
public static $supported = array('ini');
public static array $supported = array('ini');

/**
* {@inheritdoc}
Expand All @@ -37,8 +40,8 @@ public function load()
{
try {
$this->content = parse_ini_file($this->entity, true);
} catch (\Exception $e) {
throw new \RuntimeException(sprintf(
} catch (Throwable $e) {
throw new RuntimeException(sprintf(
"%s threw an exception: %s",
$this->entity,
$e
Expand Down
9 changes: 6 additions & 3 deletions src/Loader/JsonLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

namespace M1\Vars\Loader;

use RunntimeException;
use RuntimeException;

/**
* The json file loader
*
Expand All @@ -28,12 +31,12 @@ class JsonLoader extends AbstractLoader
/**
* {@inheritdoc}
*/
public static $supported = array('json');
public static array $supported = array('json');

/**
* {@inheritdoc}
*
* @throws \RunntimeException If the json file fails to load
* @throws RunntimeException If the json file fails to load
*/
public function load()
{
Expand All @@ -42,7 +45,7 @@ public function load()
if (json_last_error() !== JSON_ERROR_NONE) {
$message = function_exists('json_last_error_msg') ? json_last_error_msg() : 'Parse error';

throw new \RuntimeException(
throw new RuntimeException(
sprintf("'%s' failed to load with the error '%s'", $this->entity, $message)
);
}
Expand Down
Loading