Compare commits

...

10 Commits

Author SHA1 Message Date
Abel Hoogeveen 49e567ea30
Beautification updates.
continuous-integration/drone/push Build is passing Details
- Type, argument and return declarations
- Suggestion of ext-json in composer.json
2021-11-29 22:13:20 +01:00
Abel Hoogeveen 7f3341bdc2
PHP 8.0 compatibility update.
continuous-integration/drone/push Build is passing Details
- Removed Smarty support
- PHPUnit 9 support
- Auto testing with drone.
2021-11-29 21:54:10 +01:00
Abel Hoogeveen 777a0fe7c5
Release 1.2.0 2019-09-21 19:53:37 +02:00
Abel Hoogeveen 21c6cb8ae1
Merge remote-tracking branch 'upstream/master' 2019-09-18 00:10:12 +02:00
Abel Hoogeveen 3679f49426
Release 1.2.0-RC5 2019-09-18 00:06:26 +02:00
Abel Hoogeveen 00704f5be1 Merge branch 'holiday-branch' into 'master'
Release 1.2.0-RC4

See merge request fuzeworks/Layout!2
2019-08-21 17:28:51 +00:00
Abel Hoogeveen f37d4a38d8
Modified two aspects of `FuzeWorks\Layout`:
- It is now possible to load different engines by passing the bool $ignoreCurrentEngine to Layout::get(). This allows the developer to use different engines in succession without having to Layout::reset() the system.
- The PHPEngine now passes assigned variables with their original name. The use of $vars in templates is no longer required, but still possible.
2019-08-21 18:17:08 +02:00
Abel Hoogeveen e5268efcdf
Release 1.2.0-RC3 2019-03-05 11:16:05 +01:00
Abel Hoogeveen 581d3f93bf
Implemented minor changes.
- Fixed exception documentation
- Latte Engine now saves to the TempDir and not a subdirectory.
2019-03-01 11:18:08 +01:00
Abel Hoogeveen c417068e95 Merge branch 'rc120-release' into 'master'
Release 1.2.0-RC1

See merge request fuzeworks/Layout!1
2019-02-17 14:59:14 +00:00
24 changed files with 203 additions and 446 deletions

30
.drone.yml Normal file
View File

@ -0,0 +1,30 @@
kind: pipeline
type: docker
name: test
steps:
- name: composer
image: composer:latest
commands:
- composer install
- name: php74test
image: registry.i15.nl/i15/fuzephp:7.4-alpine
commands:
- docker-php-ext-enable xdebug
- vendor/bin/phpunit -c test/phpunit.xml
- name: php80test
image: registry.i15.nl/i15/fuzephp:8.0-alpine
commands:
- docker-php-ext-enable xdebug
- vendor/bin/phpunit -c test/phpunit.xml
- name: coverage
image: registry.i15.nl/i15/fuzephp:8.0-alpine
commands:
- docker-php-ext-enable xdebug
- vendor/bin/phpunit -c test/phpunit.xml --coverage-text
image_pull_secrets:
- dockerconfig

3
.gitignore vendored
View File

@ -3,4 +3,5 @@ composer.phar
.idea/
build/
test/temp/
vendor/
vendor/
*.cache

View File

@ -1,82 +0,0 @@
before_script:
# Install dependencies
- set -xe
- apt-get update -yqq
- apt-get install git zip unzip -yqq
stages:
- build
- test
- deploy
build:composer:
image: php:7.2
stage: build
script:
- curl -sS https://getcomposer.org/installer | php
- php composer.phar install
cache:
key: "$CI_BUILD_REF_$CI_BUILD_REF_NAME"
paths:
- vendor/
test:7.1:
stage: test
image: php:7.1
script:
- vendor/bin/phpunit -c test/phpunit.xml
cache:
key: "$CI_BUILD_REF_$CI_BUILD_REF_NAME"
paths:
- vendor
test:7.2:
stage: test
image: php:7.2
script:
- vendor/bin/phpunit -c test/phpunit.xml
cache:
key: "$CI_BUILD_REF_$CI_BUILD_REF_NAME"
paths:
- vendor/
test:7.3:
stage: test
image: php:7.3
script:
- vendor/bin/phpunit -c test/phpunit.xml
cache:
key: "$CI_BUILD_REF_$CI_BUILD_REF_NAME"
paths:
- vendor/
test:coverage:
stage: test
image: php:7.2
script:
- pecl install xdebug
- docker-php-ext-enable xdebug
- vendor/bin/phpunit -c test/phpunit.xml --coverage-text
cache:
key: "$CI_BUILD_REF_$CI_BUILD_REF_NAME"
paths:
- vendor/
release:
stage: deploy
image: php:7.2
only:
- master
script:
- pecl install xdebug
- docker-php-ext-enable xdebug
- vendor/bin/phpunit -c test/phpunit.xml --coverage-text
artifacts:
name: "${CI_BUILD_NAME}_${CI_BUILD_REF_NAME}"
paths:
- build/
expire_in: 3 weeks
cache:
key: "$CI_BUILD_REF_$CI_BUILD_REF_NAME"
paths:
- vendor/

View File

@ -1,12 +0,0 @@
language: php
php:
- 7.1
- 7.2
- 7.3
script:
- php vendor/bin/phpunit -v -c test/phpunit.xml --coverage-text
before_script:
- composer install

View File

@ -1,7 +1,7 @@
FuzeWorks::Layout Component - Readme
===================
Version 1.2.0
Version 1.3.0
A versatile PHP Framework built to perform.

View File

@ -4,27 +4,22 @@
"license": ["MIT"],
"authors": [
{
"name": "TechFuze",
"homepage": "https://techfuze.net"
},
{
"name": "FuzeWorks Community",
"homepage": "https://techfuze.net/fuzeworks/contributors"
"name": "Abel Hoogeveen",
"homepage": "https://i15.nl"
}
],
"require": {
"php": ">=7.1.0",
"fuzeworks/core": "1.2.0-RC2"
"php": ">=7.4.0",
"fuzeworks/core": "~1.3.0"
},
"require-dev": {
"ext-json": "*",
"smarty/smarty": "~3.1",
"latte/latte": "~2.4",
"phpunit/phpunit": "^7",
"mikey179/vfsStream": "1.6.5"
"latte/latte": "~2.5",
"phpunit/phpunit": "^9",
"mikey179/vfsstream": "~1.6.0"
},
"suggest": {
"smarty/smarty": "Template Engine that is natively supported by FuzeWorks.",
"ext-json": "For usage with the JSON template engine",
"latte/latte": "Template Engine that is natively supported by FuzeWorks"
},
"autoload": {
@ -32,5 +27,4 @@
"FuzeWorks\\": "src/FuzeWorks/"
}
}
}

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

@ -31,14 +31,14 @@
* @link http://techfuze.net/fuzeworks
* @since Version 0.0.1
*
* @version Version 1.2.0
* @version Version 1.3.0
*/
namespace FuzeWorks;
use FuzeWorks\Event\LayoutDisplayEvent;
use FuzeWorks\Event\LayoutLoadEvent;
use FuzeWorks\TemplateEngine\{JsonEngine,PHPEngine,SmartyEngine,LatteEngine,TemplateEngine};
use FuzeWorks\TemplateEngine\{JsonEngine,PHPEngine,LatteEngine,TemplateEngine};
use FuzeWorks\Exception\LayoutException;
use FuzeWorks\Exception\EventException;
@ -55,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
@ -128,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())
@ -149,12 +149,12 @@ class Layout
* Remember that doing so will result in a LayoutException when multiple compatible files are found.
*
* @param string $file File to load
* @param array $directories Directory to load it from
*
* @param array $directories Directory to load it from
* @param bool $ignoreCurrentEngine
* @return string The output of the template
* @throws LayoutException On error
*/
public function get(string $file, array $directories = []): string
public function get(string $file, array $directories = [], bool $ignoreCurrentEngine = false): string
{
Logger::newLevel("Loading template file '".$file."'");
@ -165,26 +165,33 @@ class Layout
$this->loadTemplateEngines();
// First retrieve the filePath
if (is_null($this->current_engine)) {
if (is_null($this->current_engine))
$this->setFileFromString($file, $directories, array_keys($this->file_extensions));
} else {
elseif ($ignoreCurrentEngine)
$this->setFileFromString($file, $directories, array_keys($this->file_extensions));
else
$this->setFileFromString($file, $directories, $this->current_engine->getFileExtensions());
}
// Then assign some basic variables for the template
// @TODO: Implement csrfTokenName and csrfHash from security under layoutLoadEvent
// Select an engine if one is not already selected
if (is_null($this->current_engine)) {
$this->current_engine = $this->getEngineFromExtension($this->getExtensionFromFile($this->file));
}
$this->current_engine->setDirectory($this->directory);
// Select an engine if one is not already selected
if (!$ignoreCurrentEngine)
$engine = $this->current_engine;
else
$engine = $this->getEngineFromExtension($this->getExtensionFromFile($this->file));
// Set directory of the engine
$engine->setDirectory($this->directory);
// And run an Event to see what other parts have to say about it
try {
/** @var LayoutLoadEvent $event */
$event = Events::fireEvent('layoutLoadEvent', $this->file, $this->directory, $this->current_engine, $this->assigned_variables);
$event = Events::fireEvent('layoutLoadEvent', $this->file, $this->directory, $engine, $this->assigned_variables);
// @codeCoverageIgnoreStart
} catch (EventException $e) {
throw new LayoutException("layoutEvent threw exception: '".$e->getMessage()."''", 1);
@ -196,14 +203,14 @@ class Layout
return 'cancelled';
// And re-fetch the data from the event
$this->current_engine = $event->engine;
$engine = $event->engine;
$this->assigned_variables = $event->assigned_variables;
Logger::stopLevel();
// And finally run it
if (file_exists($event->file)) {
return $this->current_engine->get($event->file, $this->assigned_variables);
return $engine->get($event->file, $this->assigned_variables);
}
throw new LayoutException('The requested file was not found', 1);
@ -217,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)]];
@ -232,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);
}
@ -318,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'];
@ -334,7 +341,7 @@ class Layout
*
* @return null|string Path to the file
*/
public function getFile()
public function getFile(): ?string
{
return $this->file;
}
@ -344,7 +351,7 @@ class Layout
*
* @param string $file Path to the file
*/
public function setFile($file)
public function setFile(string $file)
{
$this->file = $file;
}
@ -354,7 +361,7 @@ class Layout
*
* @return null|string Path to the directory
*/
public function getDirectory()
public function getDirectory(): ?string
{
return $this->directory;
}
@ -364,7 +371,7 @@ class Layout
*
* @param string $directory Path to the directory
*/
public function setDirectory($directory)
public function setDirectory(string $directory)
{
$this->directory = $directory;
}
@ -375,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;
}
@ -385,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;
}
@ -411,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])) {
@ -502,10 +509,6 @@ class Layout
if (class_exists('\Latte\Engine', true))
$this->registerEngine(new LatteEngine(), 'Latte', array('latte'));
// Smarty Engine
if (class_exists('\Smarty', true))
$this->registerEngine(new SmartyEngine(), 'Smarty', array('tpl'));
$this->engines_loaded = true;
return true;
}
@ -515,8 +518,10 @@ class Layout
/**
* Resets the layout manager to its default state.
*
* @param bool $keep_assigned_vars
*/
public function reset()
public function reset(bool $keep_assigned_vars = false)
{
if (!is_null($this->current_engine))
$this->current_engine->reset();
@ -527,7 +532,9 @@ class Layout
$this->file_extensions = array();
$this->current_engine = null;
$this->assigned_variables = array();
if (!$keep_assigned_vars)
$this->assigned_variables = array();
Logger::log('Reset the layout manager to its default state');
}
}

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,29 +62,27 @@ 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))
{
// If possible, load Latte\Engine
$this->latte = new Latte;
$this->latte->setTempDirectory(realpath(Core::$tempDir . DS . 'Latte'));
$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,36 +47,37 @@ 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;
$vars = $this->assigned_variables;
$directory = $this->directory;
// Then run the file
if (!is_null($file)) {
ob_start();
include $file;
// Preset assigned variables
foreach ($vars as $key => $val)
$$key = $val;
return ob_get_clean();
}
// Then run the file
ob_start();
include $file;
return ob_get_clean();
}
public function getFileExtensions(): array
@ -87,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

@ -1,172 +0,0 @@
<?php
/**
* FuzeWorks Framework Layout Template System.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2018 TechFuze
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2018, TechFuze. (http://techfuze.net)
* @license https://opensource.org/licenses/MIT MIT License
*
* @link http://techfuze.net/fuzeworks
* @since Version 0.0.1
*
* @version Version 1.2.0
*/
namespace FuzeWorks\TemplateEngine;
use FuzeWorks\Core;
use Smarty;
/**
* Wrapper for the Smarty Template Engine.
*
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2018, TechFuze. (http://techfuze.net)
*/
class SmartyEngine implements TemplateEngine
{
/**
* The currently used directory by the template.
*
* @var string
*/
protected $directory;
/**
* All the currently assigned variables.
*
* @var array
*/
protected $assigned_variables = array();
/**
* Instance of the Smarty Template Engine.
*
* @var \Smarty
*/
protected $smartyInstance;
public function setDirectory($directory)
{
$this->directory = $directory;
}
public function get($file, $assigned_variables)
{
// First set all the variables
$this->assigned_variables = $assigned_variables;
// Load Smarty
$this->loadSmarty();
// Set the directory
$this->smartyInstance->setTemplateDir($this->directory);
// Then assign all variables
foreach ($this->assigned_variables as $key => $value) {
$this->smartyInstance->assign($key, $value);
}
// And finally, load the template
return $this->smartyInstance->fetch($file);
}
/**
* Loads a Smarty instance if it is not already loaded.
*/
private function loadSmarty()
{
if (is_null($this->smartyInstance)) {
$this->smartyInstance = new Smarty();
// Then prepare all variables
$this->smartyInstance->setCompileDir(Core::$tempDir . DS . 'Smarty' . DS . 'Compile');
$this->smartyInstance->setCacheDir(Core::$tempDir . DS . 'Smarty');
}
}
public function getFileExtensions(): array
{
return array('tpl');
}
public function reset(): bool
{
$this->smartyInstance = null;
$this->directory = null;
$this->assigned_variables = array();
return true;
}
/**
* Retrieve a value from Smarty.
*
* @param string $name Variable name
*
* @return mixed Variable Value
*
* @throws \FuzeWorks\LayoutException on error
*/
public function __get($name)
{
// First load Smarty
$this->loadSmarty();
return $this->smartyInstance->$name;
}
/**
* Set a variable in Smarty.
*
* @param string $name Variable Name
* @param mixed $value Variable Value
*
* @throws \FuzeWorks\LayoutException on error
*/
public function __set($name, $value)
{
// First load Smarty
$this->loadSmarty();
$this->smartyInstance->$name = $value;
}
/**
* Calls a function in Smarty.
*
* @param string $name Name of the function to be called
* @param Paramaters $params Parameters to be used
*
* @return mixed Function output
*/
public function __call($name, $params)
{
// First load Smarty
$this->loadSmarty();
return call_user_func_array(array($this->smartyInstance, $name), $params);
}
}

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

@ -31,7 +31,7 @@
* @link http://techfuze.net/fuzeworks
* @since Version 1.2.0
*
* @version Version 1.2.0
* @version Version 1.3.0
*/
use FuzeWorks\Logger;
@ -41,17 +41,11 @@ chdir(dirname(__DIR__));
// Load the FuzeWorks container
$container = require('bootstrap.php');
Logger::disableHandlers();
// Load the test abstract
require_once 'layout/LayoutTestAbstract.php';
// Reset error and exception handlers
restore_error_handler();
restore_exception_handler();
// Set logger template for output in CLI
Logger::setLoggerTemplate('logger_cli');
// Display all errors
ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);
@ -59,4 +53,10 @@ error_reporting(E_ALL | E_STRICT);
// Set localhost "remote" IP
isset($_SERVER['REMOTE_ADDR']) OR $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
// Alias vfsStream
class_alias('org\bovigo\vfs\vfsStream', 'vfsStream');
class_alias('org\bovigo\vfs\vfsStreamDirectory', 'vfsStreamDirectory');
class_alias('org\bovigo\vfs\vfsStreamWrapper', 'vfsStreamWrapper');
// Set logger template for output in CLI
Logger::setLoggerTemplate('logger_cli');

View File

@ -31,9 +31,11 @@
* @link http://techfuze.net/fuzeworks
* @since Version 1.2.0
*
* @version Version 1.2.0
* @version Version 1.3.0
*/
use FuzeWorks\LayoutComponent;
require_once(dirname(__DIR__) . '/vendor/autoload.php');
$configurator = new FuzeWorks\Configurator();
@ -44,14 +46,14 @@ $configurator->setLogDirectory(dirname(__FILE__) . '/temp');
// Other values
$configurator->setTimeZone('Europe/Amsterdam');
$configurator->enableDebugMode(true);
// Debug related
$configurator->enableDebugMode();
$configurator->setDebugAddress('ALL');
// Implement the Layout Component
$configurator->addComponent(new \FuzeWorks\LayoutComponent());
$configurator->addComponent(new LayoutComponent());
// Create container
$container = $configurator->createContainer();
// And return the result
return $container;
return $configurator->createContainer();

View File

@ -31,9 +31,12 @@
* @link http://techfuze.net/fuzeworks
* @since Version 1.2.0
*
* @version Version 1.2.0
* @version Version 1.3.0
*/
use FuzeWorks\Configurator;
use FuzeWorks\Exception\EventException;
use FuzeWorks\Exception\LayoutException;
use FuzeWorks\Factory;
use FuzeWorks\Layout;
use FuzeWorks\Events;
@ -57,11 +60,11 @@ class LayoutTest extends LayoutTestAbstract
*/
protected $layout;
public function setUp()
public function setUp(): void
{
// Load the factory first
$this->factory = Factory::getInstance();
$this->layout = Factory::getInstance()->layouts;
$this->layout = Factory::getInstance('layouts');
$this->layout->reset();
}
@ -75,18 +78,18 @@ 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');
// Create container
$container = $configurator->createContainer();
// Init container;
$this->assertTrue(property_exists($container, 'layouts'));
$this->assertInstanceOf('FuzeWorks\Layout', $container->layouts);
$this->assertInstanceOf('FuzeWorks\Layout', Factory::getInstance('layouts'));
}
/**
@ -147,12 +150,13 @@ class LayoutTest extends LayoutTestAbstract
/**
* @depends testGetFilePath
* @expectedException FuzeWorks\Exception\LayoutException
* @covers \FuzeWorks\Layout::setFileFromString
* @covers \FuzeWorks\Layout::getFileFromString
*/
public function testMalformedPaths()
{
$this->expectException(LayoutException::class);
// Extensions to be used in this test
$extensions = array('php', 'json');
@ -160,33 +164,35 @@ class LayoutTest extends LayoutTestAbstract
}
/**
* @expectedException FuzeWorks\Exception\LayoutException
* @covers \FuzeWorks\Layout::setFileFromString
* @covers \FuzeWorks\Layout::getFileFromString
*/
public function testMissingDirectory()
{
$this->expectException(LayoutException::class);
// Directory that does not exist
$this->layout->setFileFromString('test', [3=>['test'.DS.'templates'.DS.'doesNotExist']], array('php'));
}
/**
* @expectedException FuzeWorks\Exception\LayoutException
* @covers \FuzeWorks\Layout::setFileFromString
* @covers \FuzeWorks\Layout::getFileFromString
*/
public function testMissingFile()
{
$this->expectException(LayoutException::class);
$this->layout->setFileFromString('test', [3=>['test'.DS.'templates'.DS.'testMissingFile']], array('php'));
}
/**
* @expectedException FuzeWorks\Exception\LayoutException
* @covers \FuzeWorks\Layout::setFileFromString
* @covers \FuzeWorks\Layout::getFileFromString
*/
public function testUnknownFileExtension()
{
$this->expectException(LayoutException::class);
$this->layout->setFileFromString('test', [3=>['test'.DS.'templates'.DS.'testUnknownFileExtension']], array('php'));
}
@ -225,12 +231,13 @@ class LayoutTest extends LayoutTestAbstract
}
/**
* @expectedException FuzeWorks\Exception\LayoutException
* @covers \FuzeWorks\Layout::get
* @covers \FuzeWorks\Event\LayoutLoadEvent::init
*/
public function testLayoutGetEventWrongFile()
{
$this->expectException(LayoutException::class);
$directories = ['test'.DS.'templates'.DS.'testLayoutGetEventWrongFile'];
Events::addListener(function($event){
$event->file = 'does_not_exist';
@ -265,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
@ -289,16 +296,16 @@ class LayoutTest extends LayoutTestAbstract
// Test all the default engines
$this->assertInstanceOf('FuzeWorks\TemplateEngine\PHPEngine', $this->layout->getEngineFromExtension('php'));
$this->assertInstanceOf('FuzeWorks\TemplateEngine\JsonEngine', $this->layout->getEngineFromExtension('json'));
$this->assertInstanceOf('FuzeWorks\TemplateEngine\SmartyEngine', $this->layout->getEngineFromExtension('tpl'));
$this->assertInstanceOf('FuzeWorks\TemplateEngine\LatteEngine', $this->layout->getEngineFromExtension('latte'));
}
/**
* @depends testGetEngineFromExtension
* @expectedException FuzeWorks\Exception\LayoutException
* @covers \FuzeWorks\Layout::getEngineFromExtension
*/
public function testGetEngineFromExtensionFail()
{
$this->expectException(LayoutException::class);
$this->layout->getEngineFromExtension('faulty');
}
@ -320,13 +327,14 @@ class LayoutTest extends LayoutTestAbstract
/**
* @covers \FuzeWorks\Layout::loadTemplateEngines
* @expectedException \FuzeWorks\Exception\LayoutException
*/
public function testLoadLoadEngineEvent()
{
$this->expectException(LayoutException::class);
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();
@ -357,11 +365,12 @@ class LayoutTest extends LayoutTestAbstract
/**
* @depends testCustomEngine
* @expectedException \FuzeWorks\Exception\LayoutException
* @covers \FuzeWorks\Layout::registerEngine
*/
public function testExistingCustomEngine()
{
$this->expectException(LayoutException::class);
// Create mock
$mock = $this->getMockBuilder('FuzeWorks\TemplateEngine\TemplateEngine')->getMock();
$mock->method('get')->willReturn('output');
@ -375,11 +384,12 @@ class LayoutTest extends LayoutTestAbstract
/**
* @depends testCustomEngine
* @expectedException \FuzeWorks\Exception\LayoutException
* @covers \FuzeWorks\Layout::registerEngine
*/
public function testCustomEngineWithExistingExtensions()
{
$this->expectException(LayoutException::class);
// Create mock
$mock = $this->getMockBuilder('FuzeWorks\TemplateEngine\TemplateEngine')->getMock();
$mock->method('get')->willReturn('output');
@ -419,21 +429,21 @@ class LayoutTest extends LayoutTestAbstract
/**
* @depends testSetEngine
* @expectedException \FuzeWorks\Exception\LayoutException
* @covers \FuzeWorks\Layout::setEngine
*/
public function testSetInvalidEngine()
{
$this->expectException(LayoutException::class);
$this->layout->setEngine('invalid');
}
/**
* @depends testSetEngine
* @expectedException \FuzeWorks\Exception\LayoutException
* @covers \FuzeWorks\Layout::getEngine
*/
public function testGetInvalidEngine()
{
$this->expectException(LayoutException::class);
$this->layout->getEngine('invalid');
}
@ -453,8 +463,8 @@ class LayoutTest extends LayoutTestAbstract
$this->assertEquals('JSON Template Check', json_decode($this->layout->get('json', $directories), true)[0]);
$this->layout->reset();
// And the Smarty Engine
$this->assertEquals('Smarty Template Check', $this->layout->get('smarty', $directories));
// And the Latte Engine
$this->assertEquals("Latte Template Check", $this->layout->get('latte', $directories));
}
/**
@ -475,10 +485,11 @@ class LayoutTest extends LayoutTestAbstract
$this->assertEquals('value', json_decode($this->layout->get('json', $directories), true)['data']['key']);
$this->layout->reset();
// And the Smarty Engine
// And the Latte Engine
$this->layout->assign('key', 'value');
$this->assertEquals('value', $this->layout->get('smarty', $directories));
$this->assertEquals('value', $this->layout->get('latte', $directories));
}
}
class MockEngine {

View File

@ -31,9 +31,10 @@
* @link http://techfuze.net/fuzeworks
* @since Version 1.2.0
*
* @version Version 1.2.0
* @version Version 1.3.0
*/
use FuzeWorks\Config;
use PHPUnit\Framework\TestCase;
use FuzeWorks\Events;
use FuzeWorks\Factory;
@ -51,15 +52,21 @@ abstract class LayoutTestAbstract extends TestCase
*
* Reset the layout manager
*/
public function tearDown()
public function tearDown(): void
{
// Clear all events created by tests
Events::$listeners = array();
// Reset all config files
Factory::getInstance()->config->discardConfigFiles();
Factory::getInstance('config')->discardConfigFiles();
// Re-enable events, in case they have been disabled
Events::enable();
// Remove COnfig overrides
Config::$configOverrides = [];
// Remove autoloader
Core::clearAutoloader();
}
}

View File

@ -1,34 +1,17 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/phpunit.xsd"
bootstrap="autoload.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
colors="false">
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="autoload.php" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" stopOnSkipped="false" colors="false">
<coverage processUncoveredFiles="false">
<include>
<directory suffix=".php">../</directory>
</include>
<exclude>
<directory suffix=".php">../vendor/</directory>
<directory suffix=".php">../test/</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="Core Functionality">
<testsuite name="Layout Suite">
<directory>./</directory>
</testsuite>
</testsuites>
<logging>
<log type="json" target="../build/phpunit/logfile.json"/>
<log type="junit" target="../build/phpunit/logfile.xml"/>
<log type="testdox-html" target="../build/phpunit/testdox.html"/>
<log type="testdox-text" target="../build/phpunit/testdox.txt"/>
</logging>
<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">../</directory>
<exclude>
<directory suffix=".php">../vendor/</directory>
<directory suffix=".php">../test/</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

View File

@ -0,0 +1 @@
Latte Template Check

View File

@ -1 +0,0 @@
Smarty Template Check