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"
},
"suggest": {
"ext-json": "For usage with the JSON template engine",
"latte/latte": "Template Engine that is natively supported by FuzeWorks"
},
"autoload": {

View File

@ -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)
{

View File

@ -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;
}

View File

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

View File

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

View File

@ -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])) {

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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.

View File

@ -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();

View File

@ -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();