Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function getDependencies() : array
public function getTemplates() : array
{
return [
'default_suffix' => 'phtml',
'layout' => 'layout::default',
'paths' => [],
];
Expand Down
6 changes: 5 additions & 1 deletion src/ZendViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion src/ZendViewRendererFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
*
* <code>
* 'templates' => [
* 'extension' => 'default template file extension',
* 'layout' => 'name of layout view to use, if any',
* 'map' => [
* // template => filename pairs
Expand Down Expand Up @@ -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'] : [];
Expand Down
3 changes: 3 additions & 0 deletions test/TestAsset/zendview-custom-suffix.pht
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>This is a template file for ZendView with <em>pht</em> suffix</h1>

<p>You are using <strong><?php echo $name ?></strong>!</p>
39 changes: 36 additions & 3 deletions test/ZendViewRendererFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions test/ZendViewRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<?php echo $name ?>', $name, $content);
$this->assertEquals($content, $result);
}

public function testChangeLayoutInTemplate()
{
$renderer = new ZendViewRenderer();
Expand Down