Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 99e4212

Browse files
committed
Better ServiceLocatorAwareInterface duck typing
Do not check if ServiceLocatorAwareInterface exists, as that will skip the initializer when it does, but the instance does not implement it and *does* fit duck typing rules.
1 parent c4d7430 commit 99e4212

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

src/Controller/ControllerManager.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ public function __construct($configOrContainerInstance, array $v3config = [])
5454
$this->addInitializer([$this, 'injectEventManager']);
5555
$this->addInitializer([$this, 'injectConsole']);
5656
$this->addInitializer([$this, 'injectPluginManager']);
57-
$this->addInitializer([$this, 'injectServiceLocator']);
5857
parent::__construct($configOrContainerInstance, $v3config);
58+
59+
// Added after parent construction, as v2 abstract plugin managers add
60+
// one during construction.
61+
$this->addInitializer([$this, 'injectServiceLocator']);
5962
}
6063

6164
/**
@@ -204,6 +207,7 @@ public function injectPluginManager($first, $second)
204207
*/
205208
public function injectServiceLocator($first, $second)
206209
{
210+
printf("In %s\n", __METHOD__);
207211
if ($first instanceof ContainerInterface) {
208212
$container = $first;
209213
$controller = $second;
@@ -217,7 +221,7 @@ public function injectServiceLocator($first, $second)
217221
$container = $container->getServiceLocator() ?: $container;
218222
}
219223

220-
if (! interface_exists(ServiceLocatorAwareInterface::class)
224+
if (! $controller instanceof ServiceLocatorAwareInterface
221225
&& method_exists($controller, 'setServiceLocator')
222226
) {
223227
trigger_error(sprintf(

src/Service/ServiceManagerConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function __construct(array $config = [])
142142
$instance->setServiceLocator($container);
143143
}
144144

145-
if (! interface_exists(ServiceLocatorAwareInterface::class)
145+
if (! $instance instanceof ServiceLocatorAwareInterface
146146
&& method_exists($instance, 'setServiceLocator')
147147
) {
148148
trigger_error(sprintf(

test/Service/ServiceManagerConfigTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,10 @@ public function testEventManagerInitializerCanBeReplaced()
219219

220220
public function testServiceLocatorAwareInitializerInjectsDuckTypedImplementations()
221221
{
222-
$serviceManager = new ServiceManager(['factories' => [
222+
$serviceManager = new ServiceManager();
223+
(new ServiceManagerConfig(['factories' => [
223224
TestAsset\DuckTypedServiceLocatorAware::class => InvokableFactory::class,
224-
]]);
225-
(new ServiceManagerConfig())->configureServiceManager($serviceManager);
225+
]]))->configureServiceManager($serviceManager);
226226

227227
$instance = $serviceManager->get(TestAsset\DuckTypedServiceLocatorAware::class);
228228
$this->assertInstanceOf(TestAsset\DuckTypedServiceLocatorAware::class, $instance);

0 commit comments

Comments
 (0)