Skip to content

Commit

Permalink
refactor formatting to align with original and reduce the number of c…
Browse files Browse the repository at this point in the history
…alls to container->has in the process

Signed-off-by: Gary Lockett <gary@creativecow.uk>
  • Loading branch information
Gary Lockett committed May 9, 2021
1 parent 422d26d commit 2770293
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 64 deletions.
36 changes: 15 additions & 21 deletions src/TwigEnvironmentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,11 @@ public function __invoke(ContainerInterface $container): Environment
$config = $container->has('config') ? $container->get('config') : [];

if (! is_array($config) && ! $config instanceof ArrayObject) {
throw new Exception\InvalidConfigException(
sprintf(
'"config" service must be an array or ArrayObject for the %s to be able to consume it; received %s',
self::class,
is_object($config) ? get_class($config) : gettype($config)
)
);
throw new Exception\InvalidConfigException(sprintf(
'"config" service must be an array or ArrayObject for the %s to be able to consume it; received %s',
self::class,
is_object($config) ? get_class($config) : gettype($config)
));
}

$debug = (bool) ($config['debug'] ?? false);
Expand Down Expand Up @@ -200,13 +198,11 @@ private function loadExtension($extension, ContainerInterface $container): Exten
}

if (! $extension instanceof ExtensionInterface) {
throw new Exception\InvalidExtensionException(
sprintf(
'Twig extension must be an instance of %s; "%s" given,',
ExtensionInterface::class,
is_object($extension) ? get_class($extension) : gettype($extension)
)
);
throw new Exception\InvalidExtensionException(sprintf(
'Twig extension must be an instance of %s; "%s" given,',
ExtensionInterface::class,
is_object($extension) ? get_class($extension) : gettype($extension)
));
}

return $extension;
Expand Down Expand Up @@ -239,13 +235,11 @@ private function loadRuntimeLoader($runtimeLoader, ContainerInterface $container
}

if (! $runtimeLoader instanceof RuntimeLoaderInterface) {
throw new Exception\InvalidRuntimeLoaderException(
sprintf(
'Twig runtime loader must be an instance of %s; "%s" given,',
RuntimeLoaderInterface::class,
is_object($runtimeLoader) ? get_class($runtimeLoader) : gettype($runtimeLoader)
)
);
throw new Exception\InvalidRuntimeLoaderException(sprintf(
'Twig runtime loader must be an instance of %s; "%s" given,',
RuntimeLoaderInterface::class,
is_object($runtimeLoader) ? get_class($runtimeLoader) : gettype($runtimeLoader)
));
}

return $runtimeLoader;
Expand Down
42 changes: 16 additions & 26 deletions src/TwigExtensionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,30 @@ class TwigExtensionFactory
{
public function __invoke(ContainerInterface $container): TwigExtension
{
if (
! $container->has(ServerUrlHelper::class)
&& ! $container->has(\Zend\Expressive\Helper\ServerUrlHelper::class)
) {
throw new InvalidConfigException(
sprintf(
'Missing required `%s` dependency.',
ServerUrlHelper::class
)
);
$serverUrlHelper = $container->has(ServerUrlHelper::class) ? ServerUrlHelper::class : (
$container->has(\Zend\Expressive\Helper\ServerUrlHelper::class)
? \Zend\Expressive\Helper\ServerUrlHelper::class
: null
);
if ($serverUrlHelper === null) {
throw new InvalidConfigException(sprintf('Missing required `%s` dependency.', ServerUrlHelper::class));
}

if (
! $container->has(UrlHelper::class)
&& ! $container->has(\Zend\Expressive\Helper\UrlHelper::class)
) {
throw new InvalidConfigException(
sprintf(
'Missing required `%s` dependency.',
UrlHelper::class
)
);
$urlHelper = $container->has(UrlHelper::class) ? UrlHelper::class : (
$container->has(\Zend\Expressive\Helper\UrlHelper::class)
? \Zend\Expressive\Helper\UrlHelper::class
: null
);
if ($urlHelper === null) {
throw new InvalidConfigException(sprintf('Missing required `%s` dependency.', UrlHelper::class));
}

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

return new TwigExtension(
$container->has(ServerUrlHelper::class)
? $container->get(ServerUrlHelper::class)
: $container->get(\Zend\Expressive\Helper\ServerUrlHelper::class),
$container->has(UrlHelper::class)
? $container->get(UrlHelper::class)
: $container->get(\Zend\Expressive\Helper\UrlHelper::class),
$container->get($serverUrlHelper),
$container->get($urlHelper),
$config['assets_url'] ?? '',
$config['assets_version'] ?? '',
$config['globals'] ?? []
Expand Down
10 changes: 4 additions & 6 deletions src/TwigRendererFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,10 @@ public static function mergeConfig($config): array
$config = $config instanceof ArrayObject ? $config->getArrayCopy() : $config;

if (! is_array($config)) {
throw new Exception\InvalidConfigException(
sprintf(
'Config service MUST be an array or ArrayObject; received %s',
is_object($config) ? get_class($config) : gettype($config)
)
);
throw new Exception\InvalidConfigException(sprintf(
'Config service MUST be an array or ArrayObject; received %s',
is_object($config) ? get_class($config) : gettype($config)
));
}

$mezzioConfig = isset($config['templates']) && is_array($config['templates'])
Expand Down
6 changes: 1 addition & 5 deletions test/TwigEnvironmentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,14 @@ public function testAddsTwigExtensionIfRouterIsInContainer(): void
$serverUrlHelper = $this->createMock(ServerUrlHelper::class);
$urlHelper = $this->createMock(UrlHelper::class);

$this->container->expects(self::exactly(9))->method('has')->withConsecutive(
$this->container->expects(self::exactly(7))->method('has')->withConsecutive(
['config'],
[TwigExtension::class],
[ServerUrlHelper::class],
[UrlHelper::class],
[ServerUrlHelper::class],
[UrlHelper::class],
['config'],
[ServerUrlHelper::class],
[UrlHelper::class],
)->willReturnOnConsecutiveCalls(
false,
true,
Expand All @@ -129,8 +127,6 @@ public function testAddsTwigExtensionIfRouterIsInContainer(): void
true,
true,
false,
true,
true,
);

$container = $this->container;
Expand Down
8 changes: 2 additions & 6 deletions test/TwigExtensionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,10 @@ public function testUsesAssetsConfigurationWhenAddingTwigExtension(): void
],
];

$this->container->expects(self::exactly(5))->method('has')->withConsecutive(
$this->container->expects(self::exactly(3))->method('has')->withConsecutive(
[ServerUrlHelper::class],
[UrlHelper::class],
['config'],
[ServerUrlHelper::class],
[UrlHelper::class],
)->willReturn(true);
$this->container->expects(self::exactly(3))->method('get')->withConsecutive(
['config'],
Expand Down Expand Up @@ -115,12 +113,10 @@ public function testConfiguresGlobals(): void
],
];

$this->container->expects(self::exactly(5))->method('has')->withConsecutive(
$this->container->expects(self::exactly(3))->method('has')->withConsecutive(
[ServerUrlHelper::class],
[UrlHelper::class],
['config'],
[ServerUrlHelper::class],
[UrlHelper::class],
)->willReturn(true);
$this->container->expects(self::exactly(3))->method('get')->withConsecutive(
['config'],
Expand Down

0 comments on commit 2770293

Please sign in to comment.