Skip to content

Commit

Permalink
Merge pull request #20 from alexander-schranz/patch-1
Browse files Browse the repository at this point in the history
Allow `'plates'` configuration to define its own file extension
  • Loading branch information
Ocramius authored Nov 4, 2022
2 parents 8132f11 + 7c250a4 commit d31eba7
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 28 deletions.
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 24 additions & 18 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.27.0@faf106e717c37b8c81721845dba9de3d8deed8ff">
<files psalm-version="4.29.0@7ec5ffbd5f68ae03782d7fd33fff0c45a69f95b3">
<file src="src/Extension/EscaperExtensionFactory.php">
<MixedArgument occurrences="1">
<code>$config['encoding'] ?? null</code>
Expand Down Expand Up @@ -27,27 +27,21 @@
<code>$config['extension']</code>
<code>$container-&gt;get(Extension\EscaperExtension::class)</code>
<code>$container-&gt;get(Extension\UrlExtension::class)</code>
<code>$extension</code>
<code>$namespace</code>
<code>$mezzioConfig</code>
<code>$path</code>
<code>$path</code>
<code>$platesConfig</code>
</MixedArgument>
<MixedArrayAccess occurrences="2">
<code>$config['plates']</code>
<code>$config['templates']</code>
</MixedArrayAccess>
<MixedAssignment occurrences="11">
<code>$allPaths</code>
<code>$config</code>
<MixedArgumentTypeCoercion occurrences="1">
<code>$config['extensions']</code>
</MixedArgumentTypeCoercion>
<MixedAssignment occurrences="6">
<code>$config</code>
<code>$config</code>
<code>$config</code>
<code>$extension</code>
<code>$extension</code>
<code>$namespace</code>
<code>$namespace</code>
<code>$mezzioConfig</code>
<code>$path</code>
<code>$paths</code>
<code>$platesConfig</code>
</MixedAssignment>
</file>
<file src="src/PlatesRenderer.php">
Expand Down Expand Up @@ -201,7 +195,7 @@
</file>
<file src="test/PlatesEngineFactoryTest.php">
<InvalidArgument occurrences="1"/>
<MixedArgument occurrences="12">
<MixedArgument occurrences="14">
<code>$helper</code>
<code>$this-&gt;container-&gt;reveal()</code>
<code>$this-&gt;container-&gt;reveal()</code>
Expand All @@ -212,6 +206,8 @@
<code>$this-&gt;container-&gt;reveal()</code>
<code>$this-&gt;container-&gt;reveal()</code>
<code>$this-&gt;container-&gt;reveal()</code>
<code>$this-&gt;container-&gt;reveal()</code>
<code>$this-&gt;container-&gt;reveal()</code>
<code>TestExtension::class</code>
<code>\ZendTest\Expressive\Plates\stdClass::class</code>
</MixedArgument>
Expand All @@ -222,7 +218,11 @@
<code>array</code>
<code>array</code>
</MixedInferredReturnType>
<MixedMethodCall occurrences="27">
<MixedMethodCall occurrences="31">
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
Expand Down Expand Up @@ -251,7 +251,8 @@
<code>willReturn</code>
<code>willReturn</code>
</MixedMethodCall>
<PossiblyUndefinedMethod occurrences="36">
<PossiblyUndefinedMethod occurrences="42">
<code>get</code>
<code>get</code>
<code>get</code>
<code>get</code>
Expand All @@ -261,6 +262,9 @@
<code>get</code>
<code>get</code>
<code>get</code>
<code>get</code>
<code>has</code>
<code>has</code>
<code>has</code>
<code>has</code>
<code>has</code>
Expand Down Expand Up @@ -288,6 +292,8 @@
<code>reveal</code>
<code>reveal</code>
<code>reveal</code>
<code>reveal</code>
<code>reveal</code>
</PossiblyUndefinedMethod>
<UndefinedClass occurrences="2">
<code>TestExtension</code>
Expand Down
16 changes: 12 additions & 4 deletions src/PlatesEngineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Mezzio\Helper;
use Psr\Container\ContainerInterface;

use function array_replace_recursive;
use function class_exists;
use function get_class;
use function gettype;
Expand Down Expand Up @@ -48,7 +49,15 @@ class PlatesEngineFactory
public function __invoke(ContainerInterface $container): PlatesEngine
{
$config = $container->has('config') ? $container->get('config') : [];
$config = $config['plates'] ?? [];

$mezzioConfig = isset($config['templates']) && is_array($config['templates'])
? $config['templates']
: [];
$platesConfig = isset($config['plates']) && is_array($config['plates'])
? $config['plates']
: [];

$config = array_replace_recursive($mezzioConfig, $platesConfig);

// Create the engine instance:
$engine = new PlatesEngine();
Expand All @@ -60,9 +69,6 @@ public function __invoke(ContainerInterface $container): PlatesEngine
$this->injectExtensions($container, $engine, $config['extensions']);
}

$config = $container->has('config') ? $container->get('config') : [];
$config = $config['templates'] ?? [];

// Set file extension
if (isset($config['extension'])) {
$engine->setFileExtension($config['extension']);
Expand Down Expand Up @@ -138,6 +144,8 @@ private function injectEscaperExtension(ContainerInterface $container, PlatesEng

/**
* Inject all configured extensions into the engine.
*
* @param array<ExtensionInterface|string> $extensions
*/
private function injectExtensions(ContainerInterface $container, PlatesEngine $engine, array $extensions): void
{
Expand Down
35 changes: 35 additions & 0 deletions test/PlatesEngineFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,41 @@ public function testExceptionIsRaisedIfMultiplePathsInSameNamespace(): void
$factory($this->container->reveal());
}

public function testSetExtensionByTemplatesConfig(): void
{
$config = [
'templates' => [
'extension' => 'html.twig',
],
];
$this->container->has('config')->willReturn(true);
$this->container->get('config')->willReturn($config);
$factory = new PlatesEngineFactory();

$engine = $factory($this->container->reveal());

$this->assertSame('html.twig', $engine->getFileExtension());
}

public function testOverrideExtensionByPlatesConfig(): void
{
$config = [
'templates' => [
'extension' => 'html.twig',
],
'plates' => [
'extension' => 'plates.php',
],
];
$this->container->has('config')->willReturn(true);
$this->container->get('config')->willReturn($config);
$factory = new PlatesEngineFactory();

$engine = $factory($this->container->reveal());

$this->assertSame('plates.php', $engine->getFileExtension());
}

public function provideHelpersToUnregister(): array
{
return [
Expand Down

0 comments on commit d31eba7

Please sign in to comment.