Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 0d521b5

Browse files
committed
add support for custom default suffix
1 parent e87f12d commit 0d521b5

File tree

6 files changed

+51
-2
lines changed

6 files changed

+51
-2
lines changed

src/ConfigProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function getDependencies() : array
3838
public function getTemplates() : array
3939
{
4040
return [
41+
'default_suffix' => 'phtml',
4142
'layout' => 'layout::default',
4243
'paths' => [],
4344
];

src/ZendViewRenderer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ class ZendViewRenderer implements TemplateRendererInterface
7575
*
7676
* @param null|RendererInterface $renderer
7777
* @param null|string|ModelInterface $layout
78+
* @param null|string $defaultSuffix The default template file suffux, if any
7879
* @throws Exception\InvalidArgumentException for invalid $layout types
7980
*/
80-
public function __construct(RendererInterface $renderer = null, $layout = null)
81+
public function __construct(RendererInterface $renderer = null, $layout = null, string $defaultSuffix = null)
8182
{
8283
if (null === $renderer) {
8384
$renderer = $this->createRenderer();
@@ -109,6 +110,9 @@ public function __construct(RendererInterface $renderer = null, $layout = null)
109110

110111
$this->renderer = $renderer;
111112
$this->resolver = $this->getNamespacedResolver($resolver);
113+
if (null !== $defaultSuffix) {
114+
$this->resolver->setDefaultSuffix($defaultSuffix);
115+
}
112116
$this->layout = $layout;
113117
}
114118

src/ZendViewRendererFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
*
3737
* <code>
3838
* 'templates' => [
39+
* 'extension' => 'default template file extension',
3940
* 'layout' => 'name of layout view to use, if any',
4041
* 'map' => [
4142
* // template => filename pairs
@@ -76,7 +77,7 @@ public function __invoke(ContainerInterface $container) : ZendViewRenderer
7677
$this->injectHelpers($renderer, $container);
7778

7879
// Inject renderer
79-
$view = new ZendViewRenderer($renderer, $config['layout'] ?? null);
80+
$view = new ZendViewRenderer($renderer, $config['layout'] ?? null, $config['default_suffix'] ?? null);
8081

8182
// Add template paths
8283
$allPaths = isset($config['paths']) && is_array($config['paths']) ? $config['paths'] : [];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h1>This is a template file for ZendView with <em>php</em> suffix</h1>
2+
3+
<p>You are using <strong><?php echo $name ?></strong>!</p>

test/ZendViewRendererFactoryTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Zend\Expressive\ZendView\UrlHelper;
2323
use Zend\Expressive\ZendView\ZendViewRenderer;
2424
use Zend\Expressive\ZendView\ZendViewRendererFactory;
25+
use Zend\Expressive\ZendView\NamespacedPathStackResolver;
2526
use Zend\View\HelperPluginManager;
2627
use Zend\View\Model\ModelInterface;
2728
use Zend\View\Renderer\PhpRenderer;
@@ -247,6 +248,34 @@ public function testConfiguresTemplateMap()
247248
$this->assertEquals('baz', $resolver->get('bar'));
248249
}
249250

251+
public function testConfiguresCustomDefaultSuffix()
252+
{
253+
$config = [
254+
'templates' => [
255+
'default_suffix' => 'php',
256+
],
257+
];
258+
259+
$this->container->has('config')->willReturn(true);
260+
$this->container->get('config')->willReturn($config);
261+
$this->container->has(HelperPluginManager::class)->willReturn(false);
262+
$this->container->has(PhpRenderer::class)->willReturn(false);
263+
264+
$factory = new ZendViewRendererFactory();
265+
$view = $factory($this->container->reveal());
266+
267+
$r = new ReflectionProperty($view, 'resolver');
268+
$r->setAccessible(true);
269+
$resolver = $r->getValue($view);
270+
271+
// @codingStandardsIgnoreStart
272+
// phpcs:disable
273+
$this->assertInstanceOf(NamespacedPathStackResolver::class, $resolver, 'Expected NamespacedPathStackResolver not found!');
274+
// phpcs:enable
275+
// @codingStandardsIgnoreEnd
276+
$this->assertEquals('php', $resolver->getDefaultSuffix());
277+
}
278+
250279
public function testInjectsCustomHelpersIntoHelperManager()
251280
{
252281
$this->container->has('config')->willReturn(false);

test/ZendViewRendererTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,17 @@ public function testRenderChildWithDefaultParameter()
607607
static::assertEquals($content, $result);
608608
}
609609

610+
public function testCanRenderWithCustomSuffix()
611+
{
612+
$name = 'zend-custom-suffix';
613+
$renderer = new ZendViewRenderer(null, null, 'php');
614+
$renderer->addPath(__DIR__ . '/TestAsset');
615+
$result = $renderer->render('zendview-custom-suffix', ['name' => $name]);
616+
$content = file_get_contents(__DIR__ . '/TestAsset/zendview-custom-suffix.php');
617+
$content = str_replace('<?php echo $name ?>', $name, $content);
618+
$this->assertEquals($content, $result);
619+
}
620+
610621
public function testChangeLayoutInTemplate()
611622
{
612623
$renderer = new ZendViewRenderer();

0 commit comments

Comments
 (0)