diff --git a/src/TemplateEngine/Engine.php b/src/TemplateEngine/Engine.php index 7064b63..5018b71 100755 --- a/src/TemplateEngine/Engine.php +++ b/src/TemplateEngine/Engine.php @@ -6,6 +6,7 @@ use DanBettles\Marigold\Exception\FileNotFoundException; use DanBettles\Marigold\Php; +use DanBettles\Marigold\ServiceFactory; use SplFileInfo; use function array_replace; @@ -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) ; } @@ -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 ); @@ -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; @@ -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. */ diff --git a/tests/src/PhpTest.php b/tests/src/PhpTest.php index 8cbc8e6..51fb5dd 100755 --- a/tests/src/PhpTest.php +++ b/tests/src/PhpTest.php @@ -7,7 +7,6 @@ use DanBettles\Marigold\AbstractTestCase; use DanBettles\Marigold\Exception\FileNotFoundException; use DanBettles\Marigold\Php; -use ReflectionMethod; use const null; @@ -15,10 +14,10 @@ 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 diff --git a/tests/src/RouterTest.php b/tests/src/RouterTest.php index 67ddae3..0c6db04 100755 --- a/tests/src/RouterTest.php +++ b/tests/src/RouterTest.php @@ -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' => [ diff --git a/tests/src/TemplateEngine/EngineTest.php b/tests/src/TemplateEngine/EngineTest.php index 7650e5d..0a0cd99 100755 --- a/tests/src/TemplateEngine/EngineTest.php +++ b/tests/src/TemplateEngine/EngineTest.php @@ -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; @@ -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. @@ -50,6 +63,8 @@ public function testRenderReturnsTheRenderedOutputOfTheTemplate(): void $expectedOutput = 'Hello, Dan!'; + $globalsStub = $this->createStub(ServiceFactory::class); + $phpMock = $this ->getMockBuilder(Php::class) ->onlyMethods(['executeFile']) @@ -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 @@ -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 @@ -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()); @@ -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()); diff --git a/tests/src/TemplateEngine/OutputFacadeTest.php b/tests/src/TemplateEngine/OutputFacadeTest.php index a7400f1..dfabf65 100755 --- a/tests/src/TemplateEngine/OutputFacadeTest.php +++ b/tests/src/TemplateEngine/OutputFacadeTest.php @@ -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 @@ -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());