Merge branch 'holiday-branch' into 'master'

Release 1.2.0-RC4

See merge request fuzeworks/Layout!2
This commit is contained in:
Abel Hoogeveen 2019-08-21 17:28:51 +00:00
commit 00704f5be1
3 changed files with 25 additions and 14 deletions

View File

@ -14,12 +14,12 @@
], ],
"require": { "require": {
"php": ">=7.1.0", "php": ">=7.1.0",
"fuzeworks/core": "1.2.0-RC3" "fuzeworks/core": "1.2.0-RC4"
}, },
"require-dev": { "require-dev": {
"ext-json": "*", "ext-json": "*",
"smarty/smarty": "~3.1", "smarty/smarty": "3.1.*",
"latte/latte": "~2.4", "latte/latte": "2.5.*",
"phpunit/phpunit": "^7", "phpunit/phpunit": "^7",
"mikey179/vfsStream": "1.6.5" "mikey179/vfsStream": "1.6.5"
}, },

View File

@ -149,12 +149,12 @@ class Layout
* Remember that doing so will result in a LayoutException when multiple compatible files are found. * Remember that doing so will result in a LayoutException when multiple compatible files are found.
* *
* @param string $file File to load * @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 * @return string The output of the template
* @throws LayoutException On error * @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."'"); Logger::newLevel("Loading template file '".$file."'");
@ -165,26 +165,33 @@ class Layout
$this->loadTemplateEngines(); $this->loadTemplateEngines();
// First retrieve the filePath // 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)); $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()); $this->setFileFromString($file, $directories, $this->current_engine->getFileExtensions());
}
// Then assign some basic variables for the template // Then assign some basic variables for the template
// @TODO: Implement csrfTokenName and csrfHash from security under layoutLoadEvent // @TODO: Implement csrfTokenName and csrfHash from security under layoutLoadEvent
// Select an engine if one is not already selected
if (is_null($this->current_engine)) { if (is_null($this->current_engine)) {
$this->current_engine = $this->getEngineFromExtension($this->getExtensionFromFile($this->file)); $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 // And run an Event to see what other parts have to say about it
try { try {
/** @var LayoutLoadEvent $event */ /** @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 // @codeCoverageIgnoreStart
} catch (EventException $e) { } catch (EventException $e) {
throw new LayoutException("layoutEvent threw exception: '".$e->getMessage()."''", 1); throw new LayoutException("layoutEvent threw exception: '".$e->getMessage()."''", 1);
@ -196,14 +203,14 @@ class Layout
return 'cancelled'; return 'cancelled';
// And re-fetch the data from the event // And re-fetch the data from the event
$this->current_engine = $event->engine; $engine = $event->engine;
$this->assigned_variables = $event->assigned_variables; $this->assigned_variables = $event->assigned_variables;
Logger::stopLevel(); Logger::stopLevel();
// And finally run it // And finally run it
if (file_exists($event->file)) { 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); throw new LayoutException('The requested file was not found', 1);

View File

@ -70,6 +70,10 @@ class PHPEngine implements TemplateEngine
$vars = $this->assigned_variables; $vars = $this->assigned_variables;
$directory = $this->directory; $directory = $this->directory;
// Preset assigned variables
foreach ($vars as $key => $val)
$$key = $val;
// Then run the file // Then run the file
if (!is_null($file)) { if (!is_null($file)) {
ob_start(); ob_start();