Skip to content

Commit

Permalink
Updated the template engine to pass a collection of globals, if avail…
Browse files Browse the repository at this point in the history
…able, to templates.
  • Loading branch information
danbettles committed Sep 28, 2022
1 parent 1e41706 commit 36f8c54
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 23 deletions.
36 changes: 28 additions & 8 deletions src/TemplateEngine/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use DanBettles\Marigold\Exception\FileNotFoundException;
use DanBettles\Marigold\Php;
use DanBettles\Marigold\ServiceFactory;
use SplFileInfo;

use function array_replace;
Expand All @@ -18,13 +19,17 @@ class Engine

private TemplateFileLoader $templateFileLoader;

private ?ServiceFactory $globals;

public function __construct(
Php $php,
TemplateFileLoader $templateFileLoader
TemplateFileLoader $templateFileLoader,
ServiceFactory $globals = null
) {
$this
->setPhp($php)
->setTemplateFileLoader($templateFileLoader)
->setGlobals($globals)
;
}

Expand Down Expand Up @@ -52,14 +57,20 @@ public function render(

$output = new OutputFacade($this);

$augmentedVariables = [
'input' => $variables,
'output' => $output,
];

if ($this->getGlobals()) {
$augmentedVariables['globals'] = $this->getGlobals();
}

$renderedOutput = null;

$this->getPhp()->executeFile(
$templateFile->getPathname(),
[
'input' => $variables,
'output' => $output,
],
$augmentedVariables,
$renderedOutput
);

Expand All @@ -76,9 +87,7 @@ public function render(
($wrapperTargetVarName) => $renderedOutput,
]);

return (new self($this->getPhp(), $this->getTemplateFileLoader()))
->render($wrapperPathnameOrTemplateFile, $wrapperVariables)
;
return (clone $this)->render($wrapperPathnameOrTemplateFile, $wrapperVariables);
}

return $renderedOutput;
Expand Down Expand Up @@ -106,6 +115,17 @@ public function getTemplateFileLoader(): TemplateFileLoader
return $this->templateFileLoader;
}

private function setGlobals(?ServiceFactory $globals): self
{
$this->globals = $globals;
return $this;
}

public function getGlobals(): ?ServiceFactory
{
return $this->globals;
}

/**
* Factory method, for convenience.
*/
Expand Down
7 changes: 3 additions & 4 deletions tests/src/PhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
use DanBettles\Marigold\AbstractTestCase;
use DanBettles\Marigold\Exception\FileNotFoundException;
use DanBettles\Marigold\Php;
use ReflectionMethod;

use const null;

class PhpTest extends AbstractTestCase
{
public function testExecutefileIsAnInstanceMethod(): void
{
$executeFile = new ReflectionMethod(Php::class, 'executeFile');
$executeFileMethod = $this->getTestedClass()->getMethod('executeFile');

$this->assertTrue($executeFile->isPublic());
$this->assertFalse($executeFile->isStatic());
$this->assertTrue($executeFileMethod->isPublic());
$this->assertFalse($executeFileMethod->isStatic());
}

public function testExecutefileExecutesAPhpFileAndReturnsTheReturnValue(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/src/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public function testGeneratepathGeneratesAUrlPath(): void
$this->assertSame('/foo/123/bar/456', $path);
}

public function testParameterValuesDoNotHaveToBePassedToGeneratepath(): void
public function testParameterValuesNeedNotBePassedToGeneratepath(): void
{
$routes = [
'posts' => [
Expand Down
30 changes: 23 additions & 7 deletions tests/src/TemplateEngine/EngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use DanBettles\Marigold\AbstractTestCase;
use DanBettles\Marigold\Exception\FileNotFoundException;
use DanBettles\Marigold\Php;
use DanBettles\Marigold\ServiceFactory;
use DanBettles\Marigold\TemplateEngine\Engine;
use DanBettles\Marigold\TemplateEngine\OutputFacade;
use DanBettles\Marigold\TemplateEngine\TemplateFile;
use DanBettles\Marigold\TemplateEngine\TemplateFileLoader;
use ReflectionMethod;
use SplFileInfo;

use function array_keys;
Expand All @@ -31,6 +31,19 @@ public function testIsInstantiable(): void
$this->assertSame($loader, $engine->getTemplateFileLoader());
}

public function testConstructorAcceptsAServiceFactoryForGlobals(): void
{
$serviceFactory = new ServiceFactory([]);

$engine = new Engine(
new Php(),
new TemplateFileLoader([$this->getFixturesDir()]),
$serviceFactory
);

$this->assertSame($serviceFactory, $engine->getGlobals());
}

/**
* If we know the engine is using `TemplateFileLoader` in conjunction with `executeFile()` then we can be confident
* in its behaviour.
Expand All @@ -50,6 +63,8 @@ public function testRenderReturnsTheRenderedOutputOfTheTemplate(): void

$expectedOutput = 'Hello, Dan!';

$globalsStub = $this->createStub(ServiceFactory::class);

$phpMock = $this
->getMockBuilder(Php::class)
->onlyMethods(['executeFile'])
Expand All @@ -62,11 +77,12 @@ public function testRenderReturnsTheRenderedOutputOfTheTemplate(): void
->method('executeFile')
->with(
$templateFilePathname,
$this->callback(function ($variables) use ($templateVars) {
$this->callback(function ($variables) use ($templateVars, $globalsStub) {
return is_array($variables)
&& ['input', 'output'] == array_keys($variables)
&& ['input', 'output', 'globals'] == array_keys($variables)
&& $templateVars === $variables['input']
&& $variables['output'] instanceof OutputFacade
&& $globalsStub === $variables['globals']
;
}),
null
Expand Down Expand Up @@ -96,7 +112,7 @@ public function testRenderReturnsTheRenderedOutputOfTheTemplate(): void

/** @var Php $phpMock */
/** @var TemplateFileLoader $loaderMock */
$actualOutput = (new Engine($phpMock, $loaderMock))
$actualOutput = (new Engine($phpMock, $loaderMock, $globalsStub))
->render(
$templateFileBasename,
$templateVars
Expand Down Expand Up @@ -160,9 +176,9 @@ public function testRenderAcceptsAFileInfoObject(): void
;
}

public function testVarsDoNotHaveToBePassedToRender(): void
public function testVarsNeedNotBePassedToRender(): void
{
$renderMethod = new ReflectionMethod(Engine::class, 'render');
$renderMethod = $this->getTestedClass()->getMethod('render');
$variablesParam = $renderMethod->getParameters()[1];

$this->assertTrue($variablesParam->isOptional());
Expand Down Expand Up @@ -213,7 +229,7 @@ public function testRenderThrowsAnExceptionIfTheFileDoesNotExist(

public function testCreateReturnsANewInstance(): void
{
$createMethod = new ReflectionMethod(Engine::class, 'create');
$createMethod = $this->getTestedClass()->getMethod('create');

$this->assertTrue($createMethod->isPublic());
$this->assertTrue($createMethod->isStatic());
Expand Down
5 changes: 2 additions & 3 deletions tests/src/TemplateEngine/OutputFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use DanBettles\Marigold\TemplateEngine\Engine;
use DanBettles\Marigold\TemplateEngine\OutputFacade;
use DanBettles\Marigold\TemplateEngine\TemplateFileLoader;
use ReflectionMethod;
use SplFileInfo;

class OutputFacadeTest extends AbstractTestCase
Expand Down Expand Up @@ -106,9 +105,9 @@ public function testWrapwithSetsTheWrapperArgs(): void
$this->assertSame($facade, $something);
}

public function testVarsDoNotHaveToBePassedToWrapwith(): void
public function testVarsNeedNotBePassedToWrapwith(): void
{
$wrapwithMethod = new ReflectionMethod(OutputFacade::class, 'wrapWith');
$wrapwithMethod = $this->getTestedClass()->getMethod('wrapWith');
$variablesParam = $wrapwithMethod->getParameters()[2];

$this->assertTrue($variablesParam->isOptional());
Expand Down

0 comments on commit 36f8c54

Please sign in to comment.