From 49e567ea30b0ad617fcdc42d4616fec1db4ee02c Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Mon, 29 Nov 2021 22:13:20 +0100 Subject: [PATCH] Beautification updates. - Type, argument and return declarations - Suggestion of ext-json in composer.json --- composer.json | 1 + src/FuzeWorks/Event/LayoutDisplayEvent.php | 8 ++-- src/FuzeWorks/Event/LayoutLoadEvent.php | 10 ++--- src/FuzeWorks/Exception/LanguageException.php | 4 +- src/FuzeWorks/Exception/LayoutException.php | 1 - src/FuzeWorks/Layout.php | 43 ++++++++++--------- src/FuzeWorks/TemplateEngine/JsonEngine.php | 23 ++++------ src/FuzeWorks/TemplateEngine/LatteEngine.php | 13 +++--- src/FuzeWorks/TemplateEngine/PHPEngine.php | 21 ++++----- .../TemplateEngine/TemplateEngine.php | 6 +-- test/bootstrap.php | 4 +- test/layout/LayoutTest.php | 8 ++-- 12 files changed, 66 insertions(+), 76 deletions(-) diff --git a/composer.json b/composer.json index 7cfad24..9211f87 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "mikey179/vfsstream": "~1.6.0" }, "suggest": { + "ext-json": "For usage with the JSON template engine", "latte/latte": "Template Engine that is natively supported by FuzeWorks" }, "autoload": { diff --git a/src/FuzeWorks/Event/LayoutDisplayEvent.php b/src/FuzeWorks/Event/LayoutDisplayEvent.php index a8cd75c..b047a53 100644 --- a/src/FuzeWorks/Event/LayoutDisplayEvent.php +++ b/src/FuzeWorks/Event/LayoutDisplayEvent.php @@ -51,17 +51,17 @@ class LayoutDisplayEvent extends Event /** * @var string Contents of the layout */ - public $contents; + public string $contents; /** * @var string File. File that the contents derived from */ - public $file; + public string $file; /** - * @var array directories. Directories that the layout file might resides in. + * @var array directories. Directories that the layout file might reside in. */ - public $directories; + public array $directories; public function init(string $contents, string $file, array $directories) { diff --git a/src/FuzeWorks/Event/LayoutLoadEvent.php b/src/FuzeWorks/Event/LayoutLoadEvent.php index daa5729..0e0a3ac 100644 --- a/src/FuzeWorks/Event/LayoutLoadEvent.php +++ b/src/FuzeWorks/Event/LayoutLoadEvent.php @@ -53,28 +53,28 @@ class LayoutLoadEvent extends Event * * @var string */ - public $directory; + public string $directory; /** * The file of the layout to be loaded. * * @var string */ - public $file; + public string $file; /** * The engine the file will be loaded with. * * @var object */ - public $engine; + public object $engine; /** * The assigned variables to the template. * * @var array */ - public $assigned_variables; + public array $assigned_variables; public function init($file, $directory, $engine, $assigned_variables) { @@ -90,7 +90,7 @@ class LayoutLoadEvent extends Event * @param string $key Key of the variable * @param mixed $value Value of the variable */ - public function assign($key, $value) + public function assign(string $key, $value) { $this->assigned_variables[$key] = $value; } diff --git a/src/FuzeWorks/Exception/LanguageException.php b/src/FuzeWorks/Exception/LanguageException.php index 4b2f5a1..a94ef0c 100644 --- a/src/FuzeWorks/Exception/LanguageException.php +++ b/src/FuzeWorks/Exception/LanguageException.php @@ -44,6 +44,4 @@ namespace FuzeWorks\Exception; */ class LanguageException extends Exception { -} - -?> \ No newline at end of file +} \ No newline at end of file diff --git a/src/FuzeWorks/Exception/LayoutException.php b/src/FuzeWorks/Exception/LayoutException.php index 372a392..4cd1689 100644 --- a/src/FuzeWorks/Exception/LayoutException.php +++ b/src/FuzeWorks/Exception/LayoutException.php @@ -46,4 +46,3 @@ class LayoutException extends Exception { } -?> \ No newline at end of file diff --git a/src/FuzeWorks/Layout.php b/src/FuzeWorks/Layout.php index 4b6039e..a9943a3 100644 --- a/src/FuzeWorks/Layout.php +++ b/src/FuzeWorks/Layout.php @@ -36,6 +36,7 @@ namespace FuzeWorks; +use FuzeWorks\Event\LayoutDisplayEvent; use FuzeWorks\Event\LayoutLoadEvent; use FuzeWorks\TemplateEngine\{JsonEngine,PHPEngine,LatteEngine,TemplateEngine}; use FuzeWorks\Exception\LayoutException; @@ -54,56 +55,56 @@ class Layout /** * @var Factory */ - protected $factory; + protected Factory $factory; /** * The file which the current template is loaded from * * @var null|string */ - public $file = null; + public ?string $file = null; /** * The directory where the current template is loaded from * * @var null|string */ - public $directory = null; + public ?string $directory = null; /** * All assigned currently assigned to the template. * * @var array Associative Assigned Variable Array */ - protected $assigned_variables = array(); + protected array $assigned_variables = array(); /** * All engines that can be used for templates. * * @var array of engines */ - protected $engines = array(); + protected array $engines = array(); /** * All file extensions that can be used and are bound to a template engine. * * @var array of names of engines */ - protected $file_extensions = array(); + protected array $file_extensions = array(); /** * whether the template engines are already called. * * @var bool True if loaded */ - protected $engines_loaded = false; + protected bool $engines_loaded = false; /** * The currently selected template engine. * - * @var TemplateEngine + * @var TemplateEngine|null */ - protected $current_engine; + protected ?TemplateEngine $current_engine = null; /** * Standard Component method for initializing components after adding extensions @@ -127,10 +128,10 @@ class Layout * @return mixed * @throws LayoutException On error * @throws EventException - * @throws Exception\ConfigException */ public function display(string $file, array $directories = []): bool { + /** @var LayoutDisplayEvent $event */ $contents = $this->get($file, $directories); $event = Events::fireEvent('layoutDisplayEvent', $contents, $file, $directories); if (!$event->isCancelled()) @@ -223,7 +224,7 @@ class Layout * @return TemplateEngine * @throws LayoutException */ - public function getEngineFromExtension($extension): TemplateEngine + public function getEngineFromExtension(string $extension): TemplateEngine { if (isset($this->file_extensions[strtolower($extension)])) return $this->engines[ $this->file_extensions[strtolower($extension)]]; @@ -238,7 +239,7 @@ class Layout * * @return string Extension of the file */ - public function getExtensionFromFile($fileString): string + public function getExtensionFromFile(string $fileString): string { return substr($fileString, strrpos($fileString, '.') + 1); } @@ -324,11 +325,11 @@ class Layout * * @param string $string The string used by a controller. eg: 'dashboard/home' * @param array $directories The directory to search in for the template - * @param array $extensions Extensions to use for this template. Eg array('php', 'tpl') etc. + * @param array $extensions Extensions to use for this template. Eg array('php', 'tpl') etc. * * @throws LayoutException On error */ - public function setFileFromString($string, array $directories, $extensions = array()) + public function setFileFromString(string $string, array $directories, array $extensions = array()) { $arr = $this->getFileFromString($string, $directories, $extensions); $this->file = $arr['file']; @@ -340,7 +341,7 @@ class Layout * * @return null|string Path to the file */ - public function getFile() + public function getFile(): ?string { return $this->file; } @@ -350,7 +351,7 @@ class Layout * * @param string $file Path to the file */ - public function setFile($file) + public function setFile(string $file) { $this->file = $file; } @@ -360,7 +361,7 @@ class Layout * * @return null|string Path to the directory */ - public function getDirectory() + public function getDirectory(): ?string { return $this->directory; } @@ -370,7 +371,7 @@ class Layout * * @param string $directory Path to the directory */ - public function setDirectory($directory) + public function setDirectory(string $directory) { $this->directory = $directory; } @@ -381,7 +382,7 @@ class Layout * @param string $key Key of the variable * @param mixed $value Value of the variable */ - public function assign($key, $value) + public function assign(string $key, $value) { $this->assigned_variables[$key] = $value; } @@ -391,7 +392,7 @@ class Layout * * @param string $title title of the template */ - public function setTitle($title) + public function setTitle(string $title) { $this->assigned_variables['title'] = $title; } @@ -417,7 +418,7 @@ class Layout * @return bool true on success * @throws LayoutException on error */ - public function setEngine($name): bool + public function setEngine(string $name): bool { $this->loadTemplateEngines(); if (isset($this->engines[$name])) { diff --git a/src/FuzeWorks/TemplateEngine/JsonEngine.php b/src/FuzeWorks/TemplateEngine/JsonEngine.php index 426efff..4a8c103 100644 --- a/src/FuzeWorks/TemplateEngine/JsonEngine.php +++ b/src/FuzeWorks/TemplateEngine/JsonEngine.php @@ -49,14 +49,14 @@ class JsonEngine implements TemplateEngine * * @var array */ - protected $assigned_variables = array(); + protected array $assigned_variables = []; /** * Whether the JSON data should be parsed or left as is. * * @var bool true if to be parsed */ - protected static $string_return = true; + protected static bool $string_return = true; /** * Whether the JSON data should be parsed or left as is. @@ -68,33 +68,26 @@ class JsonEngine implements TemplateEngine self::$string_return = $boolean; } - public function setDirectory($directory) + public function setDirectory(string $directory): bool { return true; } - public function get($file, $assigned_variables) + public function get(string $file, array $assigned_variables): string { // First set all the variables $this->assigned_variables = $assigned_variables; - // First set up the JSON array - $json = array(); - - // Look up if a file is provided - if (!is_null($file)) { - // Retrieve a file - $string = file_get_contents($file); - $json = json_decode($string, true); - } + // Retrieve a file + $string = file_get_contents($file); + $json = json_decode($string, true); // Then assign all variables $json['data'] = $this->assigned_variables; // And return it - if (self::$string_return) { + if (self::$string_return) return json_encode($json); - } return $json; } diff --git a/src/FuzeWorks/TemplateEngine/LatteEngine.php b/src/FuzeWorks/TemplateEngine/LatteEngine.php index cd9aff5..0d03692 100644 --- a/src/FuzeWorks/TemplateEngine/LatteEngine.php +++ b/src/FuzeWorks/TemplateEngine/LatteEngine.php @@ -52,9 +52,9 @@ class LatteEngine implements TemplateEngine /** * Instance of the Latte Engine * - * @var Latte\Engine The Latte Engine to be used + * @var Latte|null The Latte Engine to be used */ - protected $latte; + protected ?Latte $latte; /** * Set the directory of the current template. @@ -62,7 +62,7 @@ class LatteEngine implements TemplateEngine * @param string $directory Template Directory * @throws LayoutException */ - public function setDirectory($directory) + public function setDirectory(string $directory) { if (class_exists('\Latte\Engine', true)) { @@ -71,20 +71,18 @@ class LatteEngine implements TemplateEngine $this->latte->setTempDirectory(realpath(Core::$tempDir)); } else - { throw new LayoutException("Could not load LatteEngine. Is it installed or Composer not loaded?", 1); - } } /** * Handle and retrieve a template file. * * @param string $file Template File - * @param array $assigned_variables All the variables used in this layout + * @param array $assigned_variables All the variables used in this layout * * @return string Output of the template */ - public function get($file, $assigned_variables) + public function get(string $file, array $assigned_variables): string { return $this->latte->renderToString($file, $assigned_variables); } @@ -106,7 +104,6 @@ class LatteEngine implements TemplateEngine { // If possible, load Latte\Engine $this->latte = null; - return true; } } \ No newline at end of file diff --git a/src/FuzeWorks/TemplateEngine/PHPEngine.php b/src/FuzeWorks/TemplateEngine/PHPEngine.php index b00b6bc..0cf9784 100644 --- a/src/FuzeWorks/TemplateEngine/PHPEngine.php +++ b/src/FuzeWorks/TemplateEngine/PHPEngine.php @@ -47,23 +47,23 @@ class PHPEngine implements TemplateEngine /** * The currently used directory by the template. * - * @var string + * @var string|null */ - protected $directory; + protected ?string $directory; /** * All the currently assigned variables. * * @var array */ - protected $assigned_variables = array(); + protected array $assigned_variables = []; - public function setDirectory($directory) + public function setDirectory(string $directory) { $this->directory = $directory; } - public function get($file, $assigned_variables) + public function get(string $file, array $assigned_variables): string { // First set all the variables $this->assigned_variables = $assigned_variables; @@ -75,12 +75,9 @@ class PHPEngine implements TemplateEngine $$key = $val; // Then run the file - if (!is_null($file)) { - ob_start(); - include $file; - - return ob_get_clean(); - } + ob_start(); + include $file; + return ob_get_clean(); } public function getFileExtensions(): array @@ -91,7 +88,7 @@ class PHPEngine implements TemplateEngine public function reset(): bool { $this->directory = null; - $this->assigned_variables = array(); + $this->assigned_variables = []; return true; } diff --git a/src/FuzeWorks/TemplateEngine/TemplateEngine.php b/src/FuzeWorks/TemplateEngine/TemplateEngine.php index 8fe1033..183369e 100644 --- a/src/FuzeWorks/TemplateEngine/TemplateEngine.php +++ b/src/FuzeWorks/TemplateEngine/TemplateEngine.php @@ -49,17 +49,17 @@ interface TemplateEngine * * @param string $directory Template Directory */ - public function setDirectory($directory); + public function setDirectory(string $directory); /** * Handle and retrieve a template file. * * @param string $file Template File - * @param array $assigned_variables All the variables used in this layout + * @param array $assigned_variables All the variables used in this layout * * @return string Output of the template */ - public function get($file, $assigned_variables); + public function get(string $file, array $assigned_variables): string; /** * Retrieve the file extensions that this template engine uses. diff --git a/test/bootstrap.php b/test/bootstrap.php index bed7293..ed5c638 100644 --- a/test/bootstrap.php +++ b/test/bootstrap.php @@ -34,6 +34,8 @@ * @version Version 1.3.0 */ +use FuzeWorks\LayoutComponent; + require_once(dirname(__DIR__) . '/vendor/autoload.php'); $configurator = new FuzeWorks\Configurator(); @@ -50,7 +52,7 @@ $configurator->enableDebugMode(); $configurator->setDebugAddress('ALL'); // Implement the Layout Component -$configurator->addComponent(new \FuzeWorks\LayoutComponent()); +$configurator->addComponent(new LayoutComponent()); // Create container return $configurator->createContainer(); diff --git a/test/layout/LayoutTest.php b/test/layout/LayoutTest.php index 5fbae11..54d8dd1 100644 --- a/test/layout/LayoutTest.php +++ b/test/layout/LayoutTest.php @@ -34,6 +34,8 @@ * @version Version 1.3.0 */ +use FuzeWorks\Configurator; +use FuzeWorks\Exception\EventException; use FuzeWorks\Exception\LayoutException; use FuzeWorks\Factory; use FuzeWorks\Layout; @@ -76,7 +78,7 @@ class LayoutTest extends LayoutTestAbstract $component = new FuzeWorks\LayoutComponent(); // Prepare container - $configurator = new \FuzeWorks\Configurator(); + $configurator = new Configurator(); $configurator->addComponent($component); $configurator->setTempDirectory(dirname(__DIR__) . '/temp'); $configurator->setLogDirectory(dirname(__DIR__) . '/temp'); @@ -270,7 +272,7 @@ class LayoutTest extends LayoutTestAbstract { $this->layout->setDirectories([3=>['test'.DS.'templates'.DS.'testLayoutGet']]); - // First the the variables + // First the variables $this->layout->setTitle('Test Title'); // Test if they are actually set @@ -332,7 +334,7 @@ class LayoutTest extends LayoutTestAbstract Events::addListener(function($event){ $this->assertInstanceOf('\FuzeWorks\Event\NotifierEvent', $event); - throw new \FuzeWorks\Exception\EventException('Forcing failure in loadTemplateEngines()'); + throw new EventException('Forcing failure in loadTemplateEngines()'); }, 'layoutLoadEngineEvent', Priority::NORMAL); $this->layout->loadTemplateEngines();