Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3 Ported 279 from ZF repository - fixes merging of duplicate delegator factory definitions #43

Merged
merged 5 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy;
use Psr\Container\ContainerInterface;
use Laminas\Stdlib\ArrayUtils;

use function array_intersect;
use function array_merge_recursive;
use function class_exists;
use function get_class;
use function gettype;
Expand Down Expand Up @@ -363,7 +363,7 @@ public function configure(array $config)
}

if (isset($config['delegators'])) {
$this->delegators = array_merge_recursive($this->delegators, $config['delegators']);
$this->mergeDelegators($config['delegators']);
}

if (isset($config['shared'])) {
Expand All @@ -384,7 +384,7 @@ public function configure(array $config)
// If lazy service configuration was provided, reset the lazy services
// delegator factory.
if (isset($config['lazy_services']) && ! empty($config['lazy_services'])) {
$this->lazyServices = array_merge_recursive($this->lazyServices, $config['lazy_services']);
$this->lazyServices = ArrayUtils::merge($this->lazyServices, $config['lazy_services']);
$this->lazyServicesDelegator = null;
}

Expand Down Expand Up @@ -737,6 +737,32 @@ private function createLazyServiceDelegatorFactory()
return $this->lazyServicesDelegator;
}

/**
* Merge delegators avoiding multiple same delegators for the same service.
* It works with strings and class instances.
* It's not possible to de-duple anonymous functions
*
* @param string[][]|Factory\DelegatorFactoryInterface[][] $config
* @return string[][]|Factory\DelegatorFactoryInterface[][]
*/
private function mergeDelegators(array $config)
{
foreach ($config as $key => $delegators) {
if (! array_key_exists($key, $this->delegators)) {
$this->delegators[$key] = $delegators;
continue;
}

foreach ($delegators as $delegator) {
if (! in_array($delegator, $this->delegators[$key], true)) {
GeeH marked this conversation as resolved.
Show resolved Hide resolved
$this->delegators[$key][] = $delegator;
}
}
}

return $this->delegators;
}

/**
* Create aliases and factories for invokable classes.
*
Expand Down
48 changes: 48 additions & 0 deletions test/ServiceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Laminas\ServiceManager\ServiceManager;
use LaminasTest\ServiceManager\TestAsset\InvokableObject;
use LaminasTest\ServiceManager\TestAsset\SimpleServiceManager;
use Laminas\ServiceManager\Proxy\LazyServiceFactory;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use stdClass;
Expand Down Expand Up @@ -364,4 +365,51 @@ public function testResolvedAliasNoMatchingAbstractFactoryReturnsFalse()

self::assertFalse($serviceManager->has('Alias'));
}

/**
* @group #3
* @see https://github.com/laminas/laminas-servicemanager/issues/3
*/
public function testConfiguringADelegatorMultipleTimesDoesNotLeadToDuplicateDelegatorCalls()
{
$delegatorFactory = function (
ContainerInterface $container,
$name,
GeeH marked this conversation as resolved.
Show resolved Hide resolved
callable $callback
) {
/** @var InvokableObject $instance */
$instance = $callback();
$options = $instance->getOptions();
$inc = $options['inc'] ?? 0;
return new InvokableObject(['inc' => ++$inc]);
};

$config = [
'factories' => [
'Foo' => function () {
return new InvokableObject();
},
],
'delegators' => [
'Foo' => [
$delegatorFactory,
LazyServiceFactory::class,
],
],
'lazy_services' => [
GeeH marked this conversation as resolved.
Show resolved Hide resolved
'class_map' => [
'Foo' => InvokableObject::class,
],
],
];

$serviceManager = new ServiceManager($config);
$serviceManager->configure($config);

/** @var InvokableObject $instance */
$instance = $serviceManager->get('Foo');

self::assertInstanceOf(InvokableObject::class, $instance);
GeeH marked this conversation as resolved.
Show resolved Hide resolved
self::assertSame(1, $instance->getOptions()['inc']);
}
}