diff --git a/composer.json b/composer.json index 7058a20..3784717 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,12 @@ "files": [ "src/DeltaCMS/Functions.php" ] - }, + }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests" + } + }, "require": { "twig/twig": "^2.7", "psr/simple-cache": "^1.0", diff --git a/public/index.php b/public/index.php index 8c3ea70..4ce9b88 100644 --- a/public/index.php +++ b/public/index.php @@ -21,7 +21,7 @@ if (!$ap->isCacheEnabled()) { $cache->clear(); } -$caches = $cache->getMultiple(array( +$caches = (array) $cache->getMultiple(array( 'routes', 'templates', 'forms' @@ -32,12 +32,20 @@ $cachesBool = $caches["routes"] === null || $caches['templates'] === null || $ca if (!$ap->isCacheEnabled() || $cachesBool) { $cache->clear(); $modulesDIR = dirname(__DIR__) . "/src/Modules"; - $modules = array_diff(scandir($modulesDIR), array('..', '.')); + $dirs = scandir($modulesDIR); + if ($dirs === false) { + throw new Exception("Modules folder seems is not readable, Exiting", 1); + } + $modules = array_diff($dirs, array('..', '.')); /** @var string $module */ foreach ($modules as $module) { $moduleDIR = $modulesDIR . "/" . $module; if (is_dir($moduleDIR) && is_file($moduleDIR . "/" . strtolower($module) . ".json")) { - $json = json_decode(file_get_contents($moduleDIR . "/" . strtolower($module) . ".json")); + $fileContent = file_get_contents($moduleDIR . "/" . strtolower($module) . ".json"); + if ($fileContent === false) { + throw new Exception($module . " couldn't be read, please check that the JSON file is readable", 1); + } + $json = json_decode($fileContent); if (isset($json->templateFolder)) { $cache->set( 'templates', @@ -69,7 +77,7 @@ if (!$ap->isCacheEnabled() || $cachesBool) { } } } - $caches = $cache->getMultiple(array( + $caches = (array) $cache->getMultiple(array( 'routes', 'templates', 'forms' @@ -94,12 +102,10 @@ foreach ($caches['routes'] as $key => $value) { $loader->loadClass($value->controller); $function = $value->function; // dump($function); - /** @var DeltaCMS\Classes\Controller $controller */ + /** @var \DeltaCMS\Controller $controller */ $controller = new $value->controller(); // dd(new $routeArgs->controller()); - if ($composants) { - $controller->setUrlArguments($composants); - } + $controller->setUrlArguments($composants); // if(isset($json->templateFolder)) $controller->loadTwig($json->templateFolder); echo $controller->$function(); die; diff --git a/src/DeltaCMS/Cache/FileCache.php b/src/DeltaCMS/Cache/FileCache.php index f1b6fee..95c2295 100644 --- a/src/DeltaCMS/Cache/FileCache.php +++ b/src/DeltaCMS/Cache/FileCache.php @@ -2,6 +2,8 @@ namespace DeltaCMS\Cache; +use Exception; + class FileCache extends AbstractCache { private $folder; @@ -29,11 +31,16 @@ class FileCache extends AbstractCache } $file = $this->folder . DIRECTORY_SEPARATOR . $key; if (is_file($file)) { - $res = unserialize(file_get_contents($file)); - if ($res["ttl"] > time() && $res['value'] !== null) { - return $res["value"]; + $content = file_get_contents($file); + if ($content !== false) { + $res = unserialize($content); + if ($res["ttl"] > time() && $res['value'] !== null) { + return $res["value"]; + } else { + $this->delete($key); + } } else { - $this->delete($key); + throw new Exception("Cache file couldn't be read", 1); } } return $default; @@ -62,7 +69,11 @@ class FileCache extends AbstractCache public function clear() { - $keys = array_diff(scandir($this->folder), array("..", ".")); + $files = scandir($this->folder); + if ($files === false) { + throw new Exception("couldn't clear cache, the folder seems unreadable", 1); + } + $keys = array_diff($files, array("..", ".")); foreach ($keys as $key) { $this->delete($key); } diff --git a/src/DeltaCMS/Controller.php b/src/DeltaCMS/Controller.php index c142f0b..fe60890 100644 --- a/src/DeltaCMS/Controller.php +++ b/src/DeltaCMS/Controller.php @@ -8,7 +8,7 @@ class Controller protected $urlArguments = array(); protected $moduleRoot = null; - /** @var \DeltaCMS\Cache\FileCache */ + /** @var \Psr\SimpleCache\CacheInterface */ protected $cache; diff --git a/src/DeltaCMS/DeltaCMS.php b/src/DeltaCMS/DeltaCMS.php index 0f1ba9f..6c93b1d 100644 --- a/src/DeltaCMS/DeltaCMS.php +++ b/src/DeltaCMS/DeltaCMS.php @@ -27,8 +27,6 @@ class DeltaCMS /** * Get actual DeltaCMS instance * - * @param array $settings - * Settings to set to the software * * @return DeltaCMS */ @@ -53,12 +51,8 @@ class DeltaCMS return isset($this->logger) ? $this->logger : $this->logger = new Logger(dirname($this->root)); } - /** @var \Twig\Loader\FileSystemLoader $loader */ + /** @var \Twig\Loader\FilesystemLoader $loader */ private $loader; - // private function setLoader(FilesystemLoader $loader) - // { - // $this->loader = $loader; - // } public function addLoaderFolder(string $path, string $prefix = "DeltaCMS") { diff --git a/src/DeltaCMS/Enum.php b/src/DeltaCMS/Enum.php index e52fff9..8803ac1 100644 --- a/src/DeltaCMS/Enum.php +++ b/src/DeltaCMS/Enum.php @@ -2,6 +2,8 @@ namespace DeltaCMS; +use ReflectionClass; + abstract class Enum { /** @var null|array $constCacheArray */ diff --git a/src/DeltaCMS/Form.php b/src/DeltaCMS/Form.php index e53b5bf..8dafad8 100644 --- a/src/DeltaCMS/Form.php +++ b/src/DeltaCMS/Form.php @@ -39,7 +39,7 @@ class Form $adder = "add" . ucfirst($name); $value = filter_input(INPUT_POST, $name); $field = $this->getField($settings->type); - $value = $field->processValue($value, $settings); + $value = $field->getValue(); if (is_callable(array($entity, $setter))) { $entity->$setter($value); } elseif (is_callable(array($entity, $adder))) { @@ -86,7 +86,7 @@ class Form $form = $forms[$formname]; $this->fields = $form->fields; // dd($this->fields); - if ($this->isSubmitting($this->fields)) { + if ($this->isSubmitting()) { if ($entity !== null) { $this->putFieldsInEntity( $entity, diff --git a/src/DeltaCMS/Form/ArrayInput.php b/src/DeltaCMS/Form/ArrayInput.php index 46fc433..b37559f 100644 --- a/src/DeltaCMS/Form/ArrayInput.php +++ b/src/DeltaCMS/Form/ArrayInput.php @@ -7,6 +7,7 @@ class ArrayInput extends AbstractInput public function __construct() { + parent::__construct(); $this->options = array( 'elements' => array() ); diff --git a/src/DeltaCMS/Form/EntityInput.php b/src/DeltaCMS/Form/EntityInput.php index 4dcabb1..942e8c6 100644 --- a/src/DeltaCMS/Form/EntityInput.php +++ b/src/DeltaCMS/Form/EntityInput.php @@ -8,6 +8,7 @@ class EntityInput extends AbstractInput { public function __construct() { + parent::__construct(); $this->setOption("type", "text"); } diff --git a/src/DeltaCMS/Form/Input.php b/src/DeltaCMS/Form/Input.php index 9491cae..1aa3a91 100644 --- a/src/DeltaCMS/Form/Input.php +++ b/src/DeltaCMS/Form/Input.php @@ -15,7 +15,7 @@ interface Input /** * get options that will be passed into the final object * - * @return void + * @return array */ public function getAttributes(): array; @@ -29,7 +29,7 @@ interface Input /** * get options that will be passed into the final object * - * @return void + * @return array */ public function getOptions(): array; diff --git a/src/DeltaCMS/Functions.php b/src/DeltaCMS/Functions.php index cd22c51..815a353 100644 --- a/src/DeltaCMS/Functions.php +++ b/src/DeltaCMS/Functions.php @@ -1,4 +1,5 @@ regex &| options->setting * - * @return bool|array + * @return bool|stdClass */ function slugEqualToURI($slug, $uri, $options) { @@ -49,14 +50,17 @@ function slugEqualToURI($slug, $uri, $options) function jsonc_decode($filename, $assoc = false, $depth = 512, $options = 0) { - return json_decode( - preg_replace( - '![ \t]*//.*[ \t]*[\r\n]!', - '', - file_get_contents($filename) - ), - $assoc, - $depth, - $options + $fileContent = file_get_contents($filename); + if ($fileContent === false) { + throw new Exception("File" . $filename . " is not readable or not existing", 1); + } + $fileContent = preg_replace( + '![ \t]*//.*[ \t]*[\r\n]!', + '', + $fileContent ); + if ($fileContent === null) { + throw new Exception("An error occured", 1); + } + return json_decode($fileContent, $assoc, $depth, $options); } diff --git a/src/DeltaCMS/Logger.php b/src/DeltaCMS/Logger.php index 83f9943..9a7d01d 100644 --- a/src/DeltaCMS/Logger.php +++ b/src/DeltaCMS/Logger.php @@ -5,6 +5,8 @@ namespace DeltaCMS; use Psr\Log\AbstractLogger; use Psr\Log\InvalidArgumentException; +use Exception; + class Logger extends AbstractLogger { @@ -26,7 +28,11 @@ class Logger extends AbstractLogger public function __construct($file = "./logs.log") { if (!file_exists($file)) { - fclose(fopen($file, 'w')); + $ressource = fopen($file, 'w'); + if ($ressource === false) { + throw new Exception("File at " . $file . " can't be created. exiting", 1); + } + fclose($ressource); } $this->file = $file; } diff --git a/tests/LoggerTest.php b/tests/LoggerTest.php index 7e75e69..61b17f6 100644 --- a/tests/LoggerTest.php +++ b/tests/LoggerTest.php @@ -1,9 +1,12 @@