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

Commit ff0b0a7

Browse files
committed
Merge branch 'develop'
2 parents 9626301 + e719441 commit ff0b0a7

File tree

7 files changed

+82
-5
lines changed

7 files changed

+82
-5
lines changed

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5+
## 2.1.0 - TBD
6+
7+
### Added
8+
9+
- [#64](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/64)
10+
adds configuration option to change default template suffix used by NamespacedPathStackResolver
11+
12+
### Changed
13+
14+
- Nothing.
15+
16+
### Deprecated
17+
18+
- Nothing.
19+
20+
### Removed
21+
22+
- Nothing.
23+
24+
### Fixed
25+
26+
- Nothing.
27+
528
## 2.0.3 - TBD
629

730
### Added

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 suffix, 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>pht</em> suffix</h1>
2+
3+
<p>You are using <strong><?php echo $name ?></strong>!</p>

test/ZendViewRendererFactoryTest.php

Lines changed: 36 additions & 3 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;
@@ -201,14 +202,18 @@ public function testConfiguresPaths()
201202
$this->assertPathNamespaceCount(3, null, $paths);
202203

203204
$dirSlash = DIRECTORY_SEPARATOR;
204-
// @codingStandardsIgnoreStart
205-
$this->assertPathNamespaceContains(__DIR__ . '/TestAsset/bar' . $dirSlash, 'foo', $paths, var_export($paths, true));
205+
206+
$this->assertPathNamespaceContains(
207+
__DIR__ . '/TestAsset/bar' . $dirSlash,
208+
'foo',
209+
$paths,
210+
var_export($paths, true)
211+
);
206212
$this->assertPathNamespaceContains(__DIR__ . '/TestAsset/baz' . $dirSlash, 'bar', $paths);
207213
$this->assertPathNamespaceContains(__DIR__ . '/TestAsset/bat' . $dirSlash, 'bar', $paths);
208214
$this->assertPathNamespaceContains(__DIR__ . '/TestAsset/one' . $dirSlash, null, $paths);
209215
$this->assertPathNamespaceContains(__DIR__ . '/TestAsset/two' . $dirSlash, null, $paths);
210216
$this->assertPathNamespaceContains(__DIR__ . '/TestAsset/three' . $dirSlash, null, $paths);
211-
// @codingStandardsIgnoreEnd
212217
}
213218

214219
public function testConfiguresTemplateMap()
@@ -247,6 +252,34 @@ public function testConfiguresTemplateMap()
247252
$this->assertEquals('baz', $resolver->get('bar'));
248253
}
249254

255+
public function testConfiguresCustomDefaultSuffix()
256+
{
257+
$config = [
258+
'templates' => [
259+
'default_suffix' => 'php',
260+
],
261+
];
262+
263+
$this->container->has('config')->willReturn(true);
264+
$this->container->get('config')->willReturn($config);
265+
$this->container->has(HelperPluginManager::class)->willReturn(false);
266+
$this->container->has(PhpRenderer::class)->willReturn(false);
267+
268+
$factory = new ZendViewRendererFactory();
269+
$view = $factory($this->container->reveal());
270+
271+
$r = new ReflectionProperty($view, 'resolver');
272+
$r->setAccessible(true);
273+
$resolver = $r->getValue($view);
274+
275+
$this->assertInstanceOf(
276+
NamespacedPathStackResolver::class,
277+
$resolver,
278+
'Expected NamespacedPathStackResolver not found!'
279+
);
280+
$this->assertEquals('php', $resolver->getDefaultSuffix());
281+
}
282+
250283
public function testInjectsCustomHelpersIntoHelperManager()
251284
{
252285
$this->container->has('config')->willReturn(false);

test/ZendViewRendererTest.php

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

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

0 commit comments

Comments
 (0)