Skip to content

Commit

Permalink
Merge 5ff8bf6 into 7dd94dc
Browse files Browse the repository at this point in the history
  • Loading branch information
boesing authored Apr 2, 2020
2 parents 7dd94dc + 5ff8bf6 commit 1c00dad
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 31 deletions.
58 changes: 27 additions & 31 deletions src/ConfigPostProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

namespace Laminas\ZendFrameworkBridge;

use function array_flip;
use function array_intersect_key;
use function array_key_exists;
use function array_pop;
use function array_push;
use function count;
use function in_array;
use function is_array;
use function is_callable;
use function is_int;
Expand Down Expand Up @@ -75,11 +75,19 @@ function ($value, array $keys) {

// service- and pluginmanager handling
function ($value) {
$keysOfInterest = ['aliases', 'invokables', 'factories'];
if (! is_array($value)) {
return null;
}

return is_array($value) && array_intersect_key(array_flip($keysOfInterest), $value) !== []
$keysOfInterest = [
'aliases' => true,
'factories' => true,
'invokables' => true,
];

return is_array($value) && array_intersect_key($keysOfInterest, $value) !== []
? [$this, 'replaceDependencyConfiguration']
: null;
: [$this, '__invoke'];
},

// Array values
Expand Down Expand Up @@ -251,9 +259,13 @@ private function replaceDependencyConfiguration(array $config)

$config = $this->replaceDependencyInvokables($config);
$config = $this->replaceDependencyFactories($config);
$delegators = isset($config['delegators']) ? $this->replaceDependencyDelegators($config['delegators']) : [];
if ($delegators) {
$config['delegators'] = $delegators;

foreach ($config as $key => $data) {
if (in_array($key, ['aliases', 'invokables', 'factories'], true)) {
continue;
}

$config[$key] = $this->__invoke($config[$key], [$key]);
}

return $config;
Expand Down Expand Up @@ -316,6 +328,10 @@ private function replaceDependencyInvokables(array $config)
}

foreach ($config['invokables'] as $alias => $target) {
if (! is_string($alias)) {
continue;
}

$newTarget = $this->replacements->replace($target);
$newAlias = $this->replacements->replace($alias);

Expand Down Expand Up @@ -354,6 +370,10 @@ private function replaceDependencyFactories(array $config)
}

foreach ($config['factories'] as $service => $factory) {
if (! is_string($service)) {
continue;
}

$replacedService = $this->replacements->replace($service);
$factory = is_string($factory) ? $this->replacements->replace($factory) : $factory;
$config['factories'][$replacedService] = $factory;
Expand All @@ -372,28 +392,4 @@ private function replaceDependencyFactories(array $config)

return $config;
}

private function replaceDependencyDelegators(array $delegators)
{
if (empty($delegators)) {
return $delegators;
}

$replacedDelegators = [];
foreach ($delegators as $service => $factories) {
$service = $this->replacements->replace($service);
if (!is_array($factories)) {
$replacedDelegators[$service] = $factories;
// Invalid configuration aint replaced.
continue;
}

foreach ($factories as $index => $factory) {
$factory = is_string($factory) ? $this->replacements->replace($factory) : $factory;
$replacedDelegators[$service][$index] = $factory;
}
}

return $replacedDelegators;
}
}
4 changes: 4 additions & 0 deletions test/ConfigPostProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public function configurations()
yield 'process invokable config' => ['InvokableConfig.php'];
yield 'non aliased service config' => ['NonAliasedServiceConfiguration.php'];
yield 'config delegators' => ['Delegators.php'];
yield 'abstract factories' => ['AbstractFactories.php'];
yield 'lazy services' => ['LazyServices.php'];
yield 'service manager configuration' => ['FullServiceManagerConfiguration.php'];
yield 'invalid service manager configuration' => ['InvalidServiceManagerConfiguration.php'];
}

/**
Expand Down
26 changes: 26 additions & 0 deletions test/TestAsset/ConfigPostProcessor/AbstractFactories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;
use Zend\ServiceManager\Factory\InvokableFactory;

return [
'service_manager' => [
'factories' => [
'MyService' => InvokableFactory::class,
],
'abstract_factories' => [
ConfigAbstractFactory::class,
'Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory',
],
],

'dependencies' => [
'factories' => [
'MyService' => InvokableFactory::class,
],
'abstract_factories' => [
ConfigAbstractFactory::class,
'Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory',
],
],
];
26 changes: 26 additions & 0 deletions test/TestAsset/ConfigPostProcessor/AbstractFactories.php.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;
use Laminas\ServiceManager\Factory\InvokableFactory;

return [
'service_manager' => [
'factories' => [
'MyService' => InvokableFactory::class,
],
'abstract_factories' => [
ConfigAbstractFactory::class,
'Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory',
],
],

'dependencies' => [
'factories' => [
'MyService' => InvokableFactory::class,
],
'abstract_factories' => [
ConfigAbstractFactory::class,
'Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory',
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

return [
'service_manager' => [
'abstract_factories' => [
'Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory',
],
'aliases' => [
'Zend\Cache\Storage\StorageInterface' => 'Zend\Cache\Storage\Adapter\Redis',
],
'delegators' => [
'Zend\Cache\Storage\Adapter\Redis' => [
'Zend\ServiceManager\Proxy\LazyServiceFactory',
],
],
'factories' => [
'Zend\Form\Factory' => 'Some\Vendor\Zend\Form\ZendFormFactory',
'MyService' => 'Zend\ServiceManager\Factory\InvokableFactory',
],
'initializers' => [
// Just for testing purposes, this initializer does not exist
'Zend\Form\FactoryInitializer',
],
'invokables' => [
'Zend\Expressive\Router\RouterInterface' => 'MyService',
],
'lazy_services' => [
'class_map' => [
'Zend\Cache\Storage\Adapter\Redis' => 'Zend\Cache\Storage\Adapter\Redis',
],
],
'services' => [
// NOTE: this is an invalid configuration, you have to provide service instances in this array.
// For testing purposes, we have to change this to a string we can use to verify that the value
// is not touched at all. Using instances would lead to
'Zend\Cache\Storage\Adapter\Redis' => 'Zend\Cache\Storage\Adapter\Redis',
],
'shared' => [
'Zend\Form\Factory' => false,
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

return [
'service_manager' => [
'abstract_factories' => [
'Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory',
],
'aliases' => [
'Zend\Cache\Storage\StorageInterface' => 'Laminas\Cache\Storage\StorageInterface',
'Laminas\Cache\Storage\StorageInterface' => 'Laminas\Cache\Storage\Adapter\Redis',
'Zend\Expressive\Router\RouterInterface' => 'Mezzio\Router\RouterInterface',
'Zend\Form\Factory' => 'Laminas\Form\Factory',

],
'delegators' => [
'Laminas\Cache\Storage\Adapter\Redis' => [
'Laminas\ServiceManager\Proxy\LazyServiceFactory',
],
],
'factories' => [
'MyService' => 'Laminas\ServiceManager\Factory\InvokableFactory',
'Laminas\Form\Factory' => 'Some\Vendor\Zend\Form\ZendFormFactory',
],
'initializers' => [
// Just for testing purposes, this initializer does not exist
'Laminas\Form\FactoryInitializer',
],
'invokables' => [
'Mezzio\Router\RouterInterface' => 'MyService',
],
'lazy_services' => [
'class_map' => [
'Laminas\Cache\Storage\Adapter\Redis' => 'Laminas\Cache\Storage\Adapter\Redis',
],
],
'services' => [
'Laminas\Cache\Storage\Adapter\Redis' => 'Zend\Cache\Storage\Adapter\Redis',
],
'shared' => [
'Laminas\Form\Factory' => false,
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

return [
'service_manager' => [
'aliases' => [
'MyAlias' => 'MyOtherService',
],
'factories' => [
'MyOtherService', 'MyOtherFactory',
'MyOtherService' => 'MyOtherFactory',
],
'invokables' => [
'Foo', 'Bar',
'Foo' => 'Bar',
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

return [
'service_manager' => [
'aliases' => [
'MyAlias' => 'MyOtherService',
],
'factories' => [
'MyOtherService', 'MyOtherFactory',
'MyOtherService' => 'MyOtherFactory',
],
'invokables' => [
'Foo', 'Bar',
'Foo' => 'Bar',
],
],
];
24 changes: 24 additions & 0 deletions test/TestAsset/ConfigPostProcessor/LazyServices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\ServiceManager\Proxy\LazyServiceFactory;

return [
'dependencies' => [
'factories' => [
'Zend\Db\Adapter\Adapter' => 'Zend\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory',
],
'lazy_services' => [
// Mapping services to their class names is required
// since the ServiceManager is not a declarative DIC.
'class_map' => [
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\Adapter',
],
],
'delegators' => [
'Zend\Db\Adapter\Adapter' => [
LazyServiceFactory::class,
],
],
],
];
27 changes: 27 additions & 0 deletions test/TestAsset/ConfigPostProcessor/LazyServices.php.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Laminas\ServiceManager\Factory\InvokableFactory;
use Laminas\ServiceManager\Proxy\LazyServiceFactory;

return [
'dependencies' => [
'factories' => [
'Laminas\Db\Adapter\Adapter' => 'Laminas\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory',
],
'lazy_services' => [
// Mapping services to their class names is required
// since the ServiceManager is not a declarative DIC.
'class_map' => [
'Laminas\Db\Adapter\Adapter' => 'Laminas\Db\Adapter\Adapter',
],
],
'delegators' => [
'Laminas\Db\Adapter\Adapter' => [
LazyServiceFactory::class,
],
],
'aliases' => [
'Zend\Db\Adapter\Adapter' => 'Laminas\Db\Adapter\Adapter'
],
],
];

0 comments on commit 1c00dad

Please sign in to comment.