diff --git a/CHANGELOG.md b/CHANGELOG.md index 2719137..ae889f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,8 @@ All notable changes to this project will be documented in this file, in reverse ### Added -- Nothing. +- [#64](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/64) + adds configuration option to change default template suffix used by NamespacedPathStackResolver ### Changed diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index c6f9bb2..90c1c69 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -38,6 +38,7 @@ public function getDependencies() : array public function getTemplates() : array { return [ + 'default_suffix' => 'phtml', 'layout' => 'layout::default', 'paths' => [], ]; diff --git a/src/ZendViewRenderer.php b/src/ZendViewRenderer.php index 0d63551..23e95f2 100644 --- a/src/ZendViewRenderer.php +++ b/src/ZendViewRenderer.php @@ -75,9 +75,10 @@ class ZendViewRenderer implements TemplateRendererInterface * * @param null|RendererInterface $renderer * @param null|string|ModelInterface $layout + * @param null|string $defaultSuffix The default template file suffix, if any * @throws Exception\InvalidArgumentException for invalid $layout types */ - public function __construct(RendererInterface $renderer = null, $layout = null) + public function __construct(RendererInterface $renderer = null, $layout = null, string $defaultSuffix = null) { if (null === $renderer) { $renderer = $this->createRenderer(); @@ -109,6 +110,9 @@ public function __construct(RendererInterface $renderer = null, $layout = null) $this->renderer = $renderer; $this->resolver = $this->getNamespacedResolver($resolver); + if (null !== $defaultSuffix) { + $this->resolver->setDefaultSuffix($defaultSuffix); + } $this->layout = $layout; } diff --git a/src/ZendViewRendererFactory.php b/src/ZendViewRendererFactory.php index 31c574e..7e680a1 100644 --- a/src/ZendViewRendererFactory.php +++ b/src/ZendViewRendererFactory.php @@ -36,6 +36,7 @@ * * * 'templates' => [ + * 'extension' => 'default template file extension', * 'layout' => 'name of layout view to use, if any', * 'map' => [ * // template => filename pairs @@ -76,7 +77,7 @@ public function __invoke(ContainerInterface $container) : ZendViewRenderer $this->injectHelpers($renderer, $container); // Inject renderer - $view = new ZendViewRenderer($renderer, $config['layout'] ?? null); + $view = new ZendViewRenderer($renderer, $config['layout'] ?? null, $config['default_suffix'] ?? null); // Add template paths $allPaths = isset($config['paths']) && is_array($config['paths']) ? $config['paths'] : []; diff --git a/test/TestAsset/zendview-custom-suffix.pht b/test/TestAsset/zendview-custom-suffix.pht new file mode 100644 index 0000000..1c5c6de --- /dev/null +++ b/test/TestAsset/zendview-custom-suffix.pht @@ -0,0 +1,3 @@ +

This is a template file for ZendView with pht suffix

+ +

You are using !

diff --git a/test/ZendViewRendererFactoryTest.php b/test/ZendViewRendererFactoryTest.php index 5a3b02d..112a637 100644 --- a/test/ZendViewRendererFactoryTest.php +++ b/test/ZendViewRendererFactoryTest.php @@ -22,6 +22,7 @@ use Zend\Expressive\ZendView\UrlHelper; use Zend\Expressive\ZendView\ZendViewRenderer; use Zend\Expressive\ZendView\ZendViewRendererFactory; +use Zend\Expressive\ZendView\NamespacedPathStackResolver; use Zend\View\HelperPluginManager; use Zend\View\Model\ModelInterface; use Zend\View\Renderer\PhpRenderer; @@ -201,14 +202,18 @@ public function testConfiguresPaths() $this->assertPathNamespaceCount(3, null, $paths); $dirSlash = DIRECTORY_SEPARATOR; - // @codingStandardsIgnoreStart - $this->assertPathNamespaceContains(__DIR__ . '/TestAsset/bar' . $dirSlash, 'foo', $paths, var_export($paths, true)); + + $this->assertPathNamespaceContains( + __DIR__ . '/TestAsset/bar' . $dirSlash, + 'foo', + $paths, + var_export($paths, true) + ); $this->assertPathNamespaceContains(__DIR__ . '/TestAsset/baz' . $dirSlash, 'bar', $paths); $this->assertPathNamespaceContains(__DIR__ . '/TestAsset/bat' . $dirSlash, 'bar', $paths); $this->assertPathNamespaceContains(__DIR__ . '/TestAsset/one' . $dirSlash, null, $paths); $this->assertPathNamespaceContains(__DIR__ . '/TestAsset/two' . $dirSlash, null, $paths); $this->assertPathNamespaceContains(__DIR__ . '/TestAsset/three' . $dirSlash, null, $paths); - // @codingStandardsIgnoreEnd } public function testConfiguresTemplateMap() @@ -247,6 +252,34 @@ public function testConfiguresTemplateMap() $this->assertEquals('baz', $resolver->get('bar')); } + public function testConfiguresCustomDefaultSuffix() + { + $config = [ + 'templates' => [ + 'default_suffix' => 'php', + ], + ]; + + $this->container->has('config')->willReturn(true); + $this->container->get('config')->willReturn($config); + $this->container->has(HelperPluginManager::class)->willReturn(false); + $this->container->has(PhpRenderer::class)->willReturn(false); + + $factory = new ZendViewRendererFactory(); + $view = $factory($this->container->reveal()); + + $r = new ReflectionProperty($view, 'resolver'); + $r->setAccessible(true); + $resolver = $r->getValue($view); + + $this->assertInstanceOf( + NamespacedPathStackResolver::class, + $resolver, + 'Expected NamespacedPathStackResolver not found!' + ); + $this->assertEquals('php', $resolver->getDefaultSuffix()); + } + public function testInjectsCustomHelpersIntoHelperManager() { $this->container->has('config')->willReturn(false); diff --git a/test/ZendViewRendererTest.php b/test/ZendViewRendererTest.php index 40fd7e1..049b010 100644 --- a/test/ZendViewRendererTest.php +++ b/test/ZendViewRendererTest.php @@ -607,6 +607,18 @@ public function testRenderChildWithDefaultParameter() static::assertEquals($content, $result); } + public function testCanRenderWithCustomDefaultSuffix() + { + $name = 'zend-custom-suffix'; + $suffix = 'pht'; + $renderer = new ZendViewRenderer(null, null, $suffix); + $renderer->addPath(__DIR__ . '/TestAsset'); + $result = $renderer->render('zendview-custom-suffix', ['name' => $name]); + $content = file_get_contents(__DIR__ . '/TestAsset/zendview-custom-suffix.' . $suffix); + $content = str_replace('', $name, $content); + $this->assertEquals($content, $result); + } + public function testChangeLayoutInTemplate() { $renderer = new ZendViewRenderer();