Beautification updates.
continuous-integration/drone/push Build is passing Details

- Type, argument and return declarations
- Suggestion of ext-json in composer.json
This commit is contained in:
Abel Hoogeveen 2021-11-29 22:13:20 +01:00
parent 7f3341bdc2
commit 49e567ea30
Signed by: abelhooge
GPG Key ID: C540221690CBFFBA
12 changed files with 66 additions and 76 deletions

View File

@ -19,6 +19,7 @@
"mikey179/vfsstream": "~1.6.0" "mikey179/vfsstream": "~1.6.0"
}, },
"suggest": { "suggest": {
"ext-json": "For usage with the JSON template engine",
"latte/latte": "Template Engine that is natively supported by FuzeWorks" "latte/latte": "Template Engine that is natively supported by FuzeWorks"
}, },
"autoload": { "autoload": {

View File

@ -51,17 +51,17 @@ class LayoutDisplayEvent extends Event
/** /**
* @var string Contents of the layout * @var string Contents of the layout
*/ */
public $contents; public string $contents;
/** /**
* @var string File. File that the contents derived from * @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) public function init(string $contents, string $file, array $directories)
{ {

View File

@ -53,28 +53,28 @@ class LayoutLoadEvent extends Event
* *
* @var string * @var string
*/ */
public $directory; public string $directory;
/** /**
* The file of the layout to be loaded. * The file of the layout to be loaded.
* *
* @var string * @var string
*/ */
public $file; public string $file;
/** /**
* The engine the file will be loaded with. * The engine the file will be loaded with.
* *
* @var object * @var object
*/ */
public $engine; public object $engine;
/** /**
* The assigned variables to the template. * The assigned variables to the template.
* *
* @var array * @var array
*/ */
public $assigned_variables; public array $assigned_variables;
public function init($file, $directory, $engine, $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 string $key Key of the variable
* @param mixed $value Value 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; $this->assigned_variables[$key] = $value;
} }

View File

@ -44,6 +44,4 @@ namespace FuzeWorks\Exception;
*/ */
class LanguageException extends Exception class LanguageException extends Exception
{ {
} }
?>

View File

@ -46,4 +46,3 @@ class LayoutException extends Exception
{ {
} }
?>

View File

@ -36,6 +36,7 @@
namespace FuzeWorks; namespace FuzeWorks;
use FuzeWorks\Event\LayoutDisplayEvent;
use FuzeWorks\Event\LayoutLoadEvent; use FuzeWorks\Event\LayoutLoadEvent;
use FuzeWorks\TemplateEngine\{JsonEngine,PHPEngine,LatteEngine,TemplateEngine}; use FuzeWorks\TemplateEngine\{JsonEngine,PHPEngine,LatteEngine,TemplateEngine};
use FuzeWorks\Exception\LayoutException; use FuzeWorks\Exception\LayoutException;
@ -54,56 +55,56 @@ class Layout
/** /**
* @var Factory * @var Factory
*/ */
protected $factory; protected Factory $factory;
/** /**
* The file which the current template is loaded from * The file which the current template is loaded from
* *
* @var null|string * @var null|string
*/ */
public $file = null; public ?string $file = null;
/** /**
* The directory where the current template is loaded from * The directory where the current template is loaded from
* *
* @var null|string * @var null|string
*/ */
public $directory = null; public ?string $directory = null;
/** /**
* All assigned currently assigned to the template. * All assigned currently assigned to the template.
* *
* @var array Associative Assigned Variable Array * @var array Associative Assigned Variable Array
*/ */
protected $assigned_variables = array(); protected array $assigned_variables = array();
/** /**
* All engines that can be used for templates. * All engines that can be used for templates.
* *
* @var array of engines * @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. * All file extensions that can be used and are bound to a template engine.
* *
* @var array of names of engines * @var array of names of engines
*/ */
protected $file_extensions = array(); protected array $file_extensions = array();
/** /**
* whether the template engines are already called. * whether the template engines are already called.
* *
* @var bool True if loaded * @var bool True if loaded
*/ */
protected $engines_loaded = false; protected bool $engines_loaded = false;
/** /**
* The currently selected template engine. * 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 * Standard Component method for initializing components after adding extensions
@ -127,10 +128,10 @@ class Layout
* @return mixed * @return mixed
* @throws LayoutException On error * @throws LayoutException On error
* @throws EventException * @throws EventException
* @throws Exception\ConfigException
*/ */
public function display(string $file, array $directories = []): bool public function display(string $file, array $directories = []): bool
{ {
/** @var LayoutDisplayEvent $event */
$contents = $this->get($file, $directories); $contents = $this->get($file, $directories);
$event = Events::fireEvent('layoutDisplayEvent', $contents, $file, $directories); $event = Events::fireEvent('layoutDisplayEvent', $contents, $file, $directories);
if (!$event->isCancelled()) if (!$event->isCancelled())
@ -223,7 +224,7 @@ class Layout
* @return TemplateEngine * @return TemplateEngine
* @throws LayoutException * @throws LayoutException
*/ */
public function getEngineFromExtension($extension): TemplateEngine public function getEngineFromExtension(string $extension): TemplateEngine
{ {
if (isset($this->file_extensions[strtolower($extension)])) if (isset($this->file_extensions[strtolower($extension)]))
return $this->engines[ $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 * @return string Extension of the file
*/ */
public function getExtensionFromFile($fileString): string public function getExtensionFromFile(string $fileString): string
{ {
return substr($fileString, strrpos($fileString, '.') + 1); 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 string $string The string used by a controller. eg: 'dashboard/home'
* @param array $directories The directory to search in for the template * @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 * @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); $arr = $this->getFileFromString($string, $directories, $extensions);
$this->file = $arr['file']; $this->file = $arr['file'];
@ -340,7 +341,7 @@ class Layout
* *
* @return null|string Path to the file * @return null|string Path to the file
*/ */
public function getFile() public function getFile(): ?string
{ {
return $this->file; return $this->file;
} }
@ -350,7 +351,7 @@ class Layout
* *
* @param string $file Path to the file * @param string $file Path to the file
*/ */
public function setFile($file) public function setFile(string $file)
{ {
$this->file = $file; $this->file = $file;
} }
@ -360,7 +361,7 @@ class Layout
* *
* @return null|string Path to the directory * @return null|string Path to the directory
*/ */
public function getDirectory() public function getDirectory(): ?string
{ {
return $this->directory; return $this->directory;
} }
@ -370,7 +371,7 @@ class Layout
* *
* @param string $directory Path to the directory * @param string $directory Path to the directory
*/ */
public function setDirectory($directory) public function setDirectory(string $directory)
{ {
$this->directory = $directory; $this->directory = $directory;
} }
@ -381,7 +382,7 @@ class Layout
* @param string $key Key of the variable * @param string $key Key of the variable
* @param mixed $value Value 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; $this->assigned_variables[$key] = $value;
} }
@ -391,7 +392,7 @@ class Layout
* *
* @param string $title title of the template * @param string $title title of the template
*/ */
public function setTitle($title) public function setTitle(string $title)
{ {
$this->assigned_variables['title'] = $title; $this->assigned_variables['title'] = $title;
} }
@ -417,7 +418,7 @@ class Layout
* @return bool true on success * @return bool true on success
* @throws LayoutException on error * @throws LayoutException on error
*/ */
public function setEngine($name): bool public function setEngine(string $name): bool
{ {
$this->loadTemplateEngines(); $this->loadTemplateEngines();
if (isset($this->engines[$name])) { if (isset($this->engines[$name])) {

View File

@ -49,14 +49,14 @@ class JsonEngine implements TemplateEngine
* *
* @var array * @var array
*/ */
protected $assigned_variables = array(); protected array $assigned_variables = [];
/** /**
* Whether the JSON data should be parsed or left as is. * Whether the JSON data should be parsed or left as is.
* *
* @var bool true if to be parsed * @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. * Whether the JSON data should be parsed or left as is.
@ -68,33 +68,26 @@ class JsonEngine implements TemplateEngine
self::$string_return = $boolean; self::$string_return = $boolean;
} }
public function setDirectory($directory) public function setDirectory(string $directory): bool
{ {
return true; return true;
} }
public function get($file, $assigned_variables) public function get(string $file, array $assigned_variables): string
{ {
// First set all the variables // First set all the variables
$this->assigned_variables = $assigned_variables; $this->assigned_variables = $assigned_variables;
// First set up the JSON array // Retrieve a file
$json = array(); $string = file_get_contents($file);
$json = json_decode($string, true);
// Look up if a file is provided
if (!is_null($file)) {
// Retrieve a file
$string = file_get_contents($file);
$json = json_decode($string, true);
}
// Then assign all variables // Then assign all variables
$json['data'] = $this->assigned_variables; $json['data'] = $this->assigned_variables;
// And return it // And return it
if (self::$string_return) { if (self::$string_return)
return json_encode($json); return json_encode($json);
}
return $json; return $json;
} }

View File

@ -52,9 +52,9 @@ class LatteEngine implements TemplateEngine
/** /**
* Instance of the Latte Engine * 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. * Set the directory of the current template.
@ -62,7 +62,7 @@ class LatteEngine implements TemplateEngine
* @param string $directory Template Directory * @param string $directory Template Directory
* @throws LayoutException * @throws LayoutException
*/ */
public function setDirectory($directory) public function setDirectory(string $directory)
{ {
if (class_exists('\Latte\Engine', true)) if (class_exists('\Latte\Engine', true))
{ {
@ -71,20 +71,18 @@ class LatteEngine implements TemplateEngine
$this->latte->setTempDirectory(realpath(Core::$tempDir)); $this->latte->setTempDirectory(realpath(Core::$tempDir));
} }
else else
{
throw new LayoutException("Could not load LatteEngine. Is it installed or Composer not loaded?", 1); throw new LayoutException("Could not load LatteEngine. Is it installed or Composer not loaded?", 1);
}
} }
/** /**
* Handle and retrieve a template file. * Handle and retrieve a template file.
* *
* @param string $file 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 * @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); return $this->latte->renderToString($file, $assigned_variables);
} }
@ -106,7 +104,6 @@ class LatteEngine implements TemplateEngine
{ {
// If possible, load Latte\Engine // If possible, load Latte\Engine
$this->latte = null; $this->latte = null;
return true; return true;
} }
} }

View File

@ -47,23 +47,23 @@ class PHPEngine implements TemplateEngine
/** /**
* The currently used directory by the template. * The currently used directory by the template.
* *
* @var string * @var string|null
*/ */
protected $directory; protected ?string $directory;
/** /**
* All the currently assigned variables. * All the currently assigned variables.
* *
* @var array * @var array
*/ */
protected $assigned_variables = array(); protected array $assigned_variables = [];
public function setDirectory($directory) public function setDirectory(string $directory)
{ {
$this->directory = $directory; $this->directory = $directory;
} }
public function get($file, $assigned_variables) public function get(string $file, array $assigned_variables): string
{ {
// First set all the variables // First set all the variables
$this->assigned_variables = $assigned_variables; $this->assigned_variables = $assigned_variables;
@ -75,12 +75,9 @@ class PHPEngine implements TemplateEngine
$$key = $val; $$key = $val;
// Then run the file // Then run the file
if (!is_null($file)) { ob_start();
ob_start(); include $file;
include $file; return ob_get_clean();
return ob_get_clean();
}
} }
public function getFileExtensions(): array public function getFileExtensions(): array
@ -91,7 +88,7 @@ class PHPEngine implements TemplateEngine
public function reset(): bool public function reset(): bool
{ {
$this->directory = null; $this->directory = null;
$this->assigned_variables = array(); $this->assigned_variables = [];
return true; return true;
} }

View File

@ -49,17 +49,17 @@ interface TemplateEngine
* *
* @param string $directory Template Directory * @param string $directory Template Directory
*/ */
public function setDirectory($directory); public function setDirectory(string $directory);
/** /**
* Handle and retrieve a template file. * Handle and retrieve a template file.
* *
* @param string $file 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 * @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. * Retrieve the file extensions that this template engine uses.

View File

@ -34,6 +34,8 @@
* @version Version 1.3.0 * @version Version 1.3.0
*/ */
use FuzeWorks\LayoutComponent;
require_once(dirname(__DIR__) . '/vendor/autoload.php'); require_once(dirname(__DIR__) . '/vendor/autoload.php');
$configurator = new FuzeWorks\Configurator(); $configurator = new FuzeWorks\Configurator();
@ -50,7 +52,7 @@ $configurator->enableDebugMode();
$configurator->setDebugAddress('ALL'); $configurator->setDebugAddress('ALL');
// Implement the Layout Component // Implement the Layout Component
$configurator->addComponent(new \FuzeWorks\LayoutComponent()); $configurator->addComponent(new LayoutComponent());
// Create container // Create container
return $configurator->createContainer(); return $configurator->createContainer();

View File

@ -34,6 +34,8 @@
* @version Version 1.3.0 * @version Version 1.3.0
*/ */
use FuzeWorks\Configurator;
use FuzeWorks\Exception\EventException;
use FuzeWorks\Exception\LayoutException; use FuzeWorks\Exception\LayoutException;
use FuzeWorks\Factory; use FuzeWorks\Factory;
use FuzeWorks\Layout; use FuzeWorks\Layout;
@ -76,7 +78,7 @@ class LayoutTest extends LayoutTestAbstract
$component = new FuzeWorks\LayoutComponent(); $component = new FuzeWorks\LayoutComponent();
// Prepare container // Prepare container
$configurator = new \FuzeWorks\Configurator(); $configurator = new Configurator();
$configurator->addComponent($component); $configurator->addComponent($component);
$configurator->setTempDirectory(dirname(__DIR__) . '/temp'); $configurator->setTempDirectory(dirname(__DIR__) . '/temp');
$configurator->setLogDirectory(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']]); $this->layout->setDirectories([3=>['test'.DS.'templates'.DS.'testLayoutGet']]);
// First the the variables // First the variables
$this->layout->setTitle('Test Title'); $this->layout->setTitle('Test Title');
// Test if they are actually set // Test if they are actually set
@ -332,7 +334,7 @@ class LayoutTest extends LayoutTestAbstract
Events::addListener(function($event){ Events::addListener(function($event){
$this->assertInstanceOf('\FuzeWorks\Event\NotifierEvent', $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); }, 'layoutLoadEngineEvent', Priority::NORMAL);
$this->layout->loadTemplateEngines(); $this->layout->loadTemplateEngines();