diff --git a/composer.lock b/composer.lock index e22509e..3b7d2ce 100644 --- a/composer.lock +++ b/composer.lock @@ -989,16 +989,16 @@ }, { "name": "composer/pcre", - "version": "3.0.0", + "version": "3.0.2", "source": { "type": "git", "url": "https://github.com/composer/pcre.git", - "reference": "e300eb6c535192decd27a85bc72a9290f0d6b3bd" + "reference": "4482b6409ca6bfc2af043a5711cd21ac3e7a8dfb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/composer/pcre/zipball/e300eb6c535192decd27a85bc72a9290f0d6b3bd", - "reference": "e300eb6c535192decd27a85bc72a9290f0d6b3bd", + "url": "https://api.github.com/repos/composer/pcre/zipball/4482b6409ca6bfc2af043a5711cd21ac3e7a8dfb", + "reference": "4482b6409ca6bfc2af043a5711cd21ac3e7a8dfb", "shasum": "" }, "require": { @@ -1040,7 +1040,7 @@ ], "support": { "issues": "https://github.com/composer/pcre/issues", - "source": "https://github.com/composer/pcre/tree/3.0.0" + "source": "https://github.com/composer/pcre/tree/3.0.2" }, "funding": [ { @@ -1056,7 +1056,7 @@ "type": "tidelift" } ], - "time": "2022-02-25T20:21:48+00:00" + "time": "2022-11-03T20:24:16+00:00" }, { "name": "composer/semver", diff --git a/psalm-baseline.xml b/psalm-baseline.xml index e95de48..5794bdd 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,5 @@ - + $config['encoding'] ?? null @@ -27,27 +27,21 @@ $config['extension'] $container->get(Extension\EscaperExtension::class) $container->get(Extension\UrlExtension::class) - $extension - $namespace + $mezzioConfig $path $path + $platesConfig - - $config['plates'] - $config['templates'] - - - $allPaths - $config + + $config['extensions'] + + $config - $config - $config - $extension $extension - $namespace - $namespace + $mezzioConfig $path $paths + $platesConfig @@ -201,7 +195,7 @@ - + $helper $this->container->reveal() $this->container->reveal() @@ -212,6 +206,8 @@ $this->container->reveal() $this->container->reveal() $this->container->reveal() + $this->container->reveal() + $this->container->reveal() TestExtension::class \ZendTest\Expressive\Plates\stdClass::class @@ -222,7 +218,11 @@ array array - + + willReturn + willReturn + willReturn + willReturn willReturn willReturn willReturn @@ -251,7 +251,8 @@ willReturn willReturn - + + get get get get @@ -261,6 +262,9 @@ get get get + get + has + has has has has @@ -288,6 +292,8 @@ reveal reveal reveal + reveal + reveal TestExtension diff --git a/src/PlatesEngineFactory.php b/src/PlatesEngineFactory.php index 628fe1f..90be966 100644 --- a/src/PlatesEngineFactory.php +++ b/src/PlatesEngineFactory.php @@ -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; @@ -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(); @@ -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']); @@ -138,6 +144,8 @@ private function injectEscaperExtension(ContainerInterface $container, PlatesEng /** * Inject all configured extensions into the engine. + * + * @param array $extensions */ private function injectExtensions(ContainerInterface $container, PlatesEngine $engine, array $extensions): void { diff --git a/test/PlatesEngineFactoryTest.php b/test/PlatesEngineFactoryTest.php index 14574c3..69a8a16 100644 --- a/test/PlatesEngineFactoryTest.php +++ b/test/PlatesEngineFactoryTest.php @@ -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 [