Skip to content

Commit

Permalink
Merge pull request #19 from alexander-schranz/feature/register-plates…
Browse files Browse the repository at this point in the history
…-engine-service

Make plates engine service available
  • Loading branch information
Ocramius authored Sep 20, 2022
2 parents 67bfb32 + 32e7c51 commit edb0dac
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 141 deletions.
85 changes: 40 additions & 45 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,31 @@
<InvalidStringClass occurrences="1">
<code>new $extension()</code>
</InvalidStringClass>
<MixedArgument occurrences="3">
<MixedArgument occurrences="7">
<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>$path</code>
<code>$path</code>
</MixedArgument>
<MixedArrayAccess occurrences="1">
<MixedArrayAccess occurrences="2">
<code>$config['plates']</code>
<code>$config['templates']</code>
</MixedArrayAccess>
<MixedAssignment occurrences="4">
<MixedAssignment occurrences="11">
<code>$allPaths</code>
<code>$config</code>
<code>$config</code>
<code>$config</code>
<code>$config</code>
<code>$extension</code>
<code>$extension</code>
<code>$namespace</code>
<code>$namespace</code>
<code>$path</code>
<code>$paths</code>
</MixedAssignment>
</file>
<file src="src/PlatesRenderer.php">
Expand All @@ -56,31 +68,6 @@
<code>is_object($templateName)</code>
</TypeDoesNotContainType>
</file>
<file src="src/PlatesRendererFactory.php">
<MixedArgument occurrences="3">
<code>$config['extension']</code>
<code>$namespace</code>
<code>$path</code>
</MixedArgument>
<MixedArrayAccess occurrences="1">
<code>$config['templates']</code>
</MixedArrayAccess>
<MixedAssignment occurrences="7">
<code>$allPaths</code>
<code>$config</code>
<code>$config</code>
<code>$namespace</code>
<code>$namespace</code>
<code>$path</code>
<code>$paths</code>
</MixedAssignment>
<MixedInferredReturnType occurrences="1">
<code>PlatesEngine</code>
</MixedInferredReturnType>
<MixedReturnStatement occurrences="1">
<code>$container-&gt;get(PlatesEngine::class)</code>
</MixedReturnStatement>
</file>
<file src="test/ConfigProviderTest.php">
<RedundantCondition occurrences="1">
<code>assertIsArray</code>
Expand Down Expand Up @@ -213,7 +200,8 @@
</PossiblyUndefinedMethod>
</file>
<file src="test/PlatesEngineFactoryTest.php">
<MixedArgument occurrences="10">
<InvalidArgument occurrences="1"/>
<MixedArgument occurrences="12">
<code>$helper</code>
<code>$this-&gt;container-&gt;reveal()</code>
<code>$this-&gt;container-&gt;reveal()</code>
Expand All @@ -222,6 +210,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 @@ -232,7 +222,11 @@
<code>array</code>
<code>array</code>
</MixedInferredReturnType>
<MixedMethodCall occurrences="23">
<MixedMethodCall occurrences="27">
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
Expand All @@ -257,14 +251,17 @@
<code>willReturn</code>
<code>willReturn</code>
</MixedMethodCall>
<PossiblyUndefinedMethod occurrences="30">
<PossiblyUndefinedMethod occurrences="36">
<code>get</code>
<code>get</code>
<code>get</code>
<code>get</code>
<code>get</code>
<code>get</code>
<code>get</code>
<code>get</code>
<code>get</code>
<code>has</code>
<code>has</code>
<code>has</code>
<code>has</code>
Expand All @@ -281,6 +278,9 @@
<code>has</code>
<code>has</code>
<code>has</code>
<code>has</code>
<code>reveal</code>
<code>reveal</code>
<code>reveal</code>
<code>reveal</code>
<code>reveal</code>
Expand All @@ -293,11 +293,15 @@
<code>TestExtension</code>
<code>\ZendTest\Expressive\Plates\stdClass</code>
</UndefinedClass>
<UndefinedThisPropertyAssignment occurrences="1">
<code>$this-&gt;errorCaught</code>
</UndefinedThisPropertyAssignment>
<UndefinedThisPropertyFetch occurrences="1">
<code>$this-&gt;errorCaught</code>
</UndefinedThisPropertyFetch>
</file>
<file src="test/PlatesRendererFactoryTest.php">
<InvalidArgument occurrences="1"/>
<MixedArgument occurrences="6">
<code>$this-&gt;container-&gt;reveal()</code>
<MixedArgument occurrences="5">
<code>$this-&gt;container-&gt;reveal()</code>
<code>$this-&gt;container-&gt;reveal()</code>
<code>$this-&gt;container-&gt;reveal()</code>
Expand All @@ -307,11 +311,7 @@
<MixedInferredReturnType occurrences="1">
<code>Engine</code>
</MixedInferredReturnType>
<MixedMethodCall occurrences="21">
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
<MixedMethodCall occurrences="17">
<code>willReturn</code>
<code>willReturn</code>
<code>willReturn</code>
Expand All @@ -336,15 +336,13 @@
<PossiblyNullArgument occurrences="1">
<code>$namespace ?: null</code>
</PossiblyNullArgument>
<PossiblyUndefinedMethod occurrences="27">
<PossiblyUndefinedMethod occurrences="22">
<code>get</code>
<code>get</code>
<code>get</code>
<code>get</code>
<code>get</code>
<code>get</code>
<code>get</code>
<code>has</code>
<code>has</code>
<code>has</code>
<code>has</code>
Expand All @@ -356,9 +354,6 @@
<code>has</code>
<code>has</code>
<code>has</code>
<code>has</code>
<code>has</code>
<code>reveal</code>
<code>reveal</code>
<code>reveal</code>
<code>reveal</code>
Expand Down
2 changes: 2 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Mezzio\Plates;

use League\Plates\Engine as PlatesEngine;
use Mezzio\Template\TemplateRendererInterface;

class ConfigProvider
Expand All @@ -27,6 +28,7 @@ public function getDependencies(): array
'Zend\Expressive\Plates\PlatesRenderer' => PlatesRenderer::class,
],
'factories' => [
PlatesEngine::class => PlatesEngineFactory::class,
PlatesRenderer::class => PlatesRendererFactory::class,
],
];
Expand Down
32 changes: 32 additions & 0 deletions src/PlatesEngineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
use function get_class;
use function gettype;
use function is_array;
use function is_numeric;
use function is_object;
use function is_string;
use function sprintf;
use function trigger_error;

use const E_USER_WARNING;

/**
* Create and return a Plates engine instance.
Expand Down Expand Up @@ -56,6 +60,34 @@ 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']);
}

// Add template paths
$allPaths = isset($config['paths']) && is_array($config['paths']) ? $config['paths'] : [];

foreach ($allPaths as $namespace => $paths) {
$namespace = is_numeric($namespace) ? null : $namespace;
foreach ((array) $paths as $path) {
if (! $namespace && ! $engine->getDirectory()) {
$engine->setDirectory($path);
continue;
}

if (! $namespace) {
trigger_error('Cannot add duplicate un-namespaced path in Plates template adapter', E_USER_WARNING);
continue;
}

$engine->addFolder($namespace, $path, true);
}
}

return $engine;
}

Expand Down
47 changes: 3 additions & 44 deletions src/PlatesRendererFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use League\Plates\Engine as PlatesEngine;
use Psr\Container\ContainerInterface;

use function is_array;
use function is_numeric;

/**
* Create and return a Plates template instance.
*
Expand All @@ -36,47 +33,9 @@ class PlatesRendererFactory
{
public function __invoke(ContainerInterface $container): PlatesRenderer
{
$config = $container->has('config') ? $container->get('config') : [];
$config = $config['templates'] ?? [];

// Create the engine instance:
$engine = $this->createEngine($container);

// Set file extension
if (isset($config['extension'])) {
$engine->setFileExtension($config['extension']);
}

// Inject engine
$plates = new PlatesRenderer($engine);

// Add template paths
$allPaths = isset($config['paths']) && is_array($config['paths']) ? $config['paths'] : [];
foreach ($allPaths as $namespace => $paths) {
$namespace = is_numeric($namespace) ? null : $namespace;
foreach ((array) $paths as $path) {
$plates->addPath($path, $namespace);
}
}

return $plates;
}

/**
* Create and return a Plates Engine instance.
*
* If the container has the League\Plates\Engine service, returns it.
*
* Otherwise, invokes the PlatesEngineFactory with the $container to create
* and return the instance.
*/
private function createEngine(ContainerInterface $container): PlatesEngine
{
if ($container->has(PlatesEngine::class)) {
return $container->get(PlatesEngine::class);
}
/** @var PlatesEngine $engine */
$engine = $container->get(PlatesEngine::class);

$engineFactory = new PlatesEngineFactory();
return $engineFactory($container);
return new PlatesRenderer($engine);
}
}
49 changes: 49 additions & 0 deletions test/PlatesEngineFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use League\Plates\Engine as PlatesEngine;
use League\Plates\Extension\ExtensionInterface;
use LogicException;
use Mezzio\Helper\ServerUrlHelper;
use Mezzio\Helper\UrlHelper;
use Mezzio\Plates\Exception\InvalidExtensionException;
Expand All @@ -21,6 +22,10 @@
use ZendTest\Expressive\Plates\TestAsset\TestExtension;

use function is_string;
use function restore_error_handler;
use function set_error_handler;

use const E_USER_WARNING;

class PlatesEngineFactoryTest extends TestCase
{
Expand Down Expand Up @@ -222,6 +227,50 @@ public function testFactoryRaisesExceptionWhenNonServiceClassIsAnInvalidExtensio
$factory($this->container->reveal());
}

public function testExceptionIsRaisedIfMultiplePathsSpecifyDefaultNamespace(): void
{
$config = [
'templates' => [
'paths' => [
0 => __DIR__ . '/TestAsset/bar',
1 => __DIR__ . '/TestAsset/baz',
],
],
];
$this->container->has('config')->willReturn(true);
$this->container->get('config')->willReturn($config);
$factory = new PlatesEngineFactory();

// phpcs:ignore WebimpressCodingStandard.NamingConventions.ValidVariableName.NotCamelCaps
set_error_handler(function (int $_errno, string $_errstr): void {
$this->errorCaught = true;
}, E_USER_WARNING);
$factory($this->container->reveal());
restore_error_handler();
$this->assertTrue($this->errorCaught, 'Did not detect duplicate path for default namespace');
}

public function testExceptionIsRaisedIfMultiplePathsInSameNamespace(): void
{
$config = [
'templates' => [
'paths' => [
'bar' => [
__DIR__ . '/TestAsset/baz',
__DIR__ . '/TestAsset/bat',
],
],
],
];
$this->container->has('config')->willReturn(true);
$this->container->get('config')->willReturn($config);
$factory = new PlatesEngineFactory();

$this->expectException(LogicException::class);
$this->expectExceptionMessage('already being used');
$factory($this->container->reveal());
}

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

0 comments on commit edb0dac

Please sign in to comment.