Skip to content

Commit 5333bd2

Browse files
bug #29546 [DI] map snake-case ids of service subscribers to camel-case autowiring aliases (nicolas-grekas)
This PR was merged into the 4.2 branch. Discussion ---------- [DI] map snake-case ids of service subscribers to camel-case autowiring aliases | Q | A | ------------- | --- | Branch? | 4.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Discovered during the workshop at SymfonyCon Lisbon. Commits ------- af17da9 [DI] map snake-case ids of service subscribers to camel-case autowiring aliases
2 parents ab95ae3 + af17da9 commit 5333bd2

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/Symfony/Component/DependencyInjection/Compiler/RegisterServiceSubscribersPass.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ protected function processValue($value, $isRoot = false)
9191
$name = null;
9292
}
9393

94+
if (null !== $name && !$this->container->has($name) && !$this->container->has($type.' $'.$name)) {
95+
$camelCaseName = lcfirst(str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $name))));
96+
$name = $this->container->has($type.' $'.$camelCaseName) ? $camelCaseName : $name;
97+
}
98+
9499
$subscriberMap[$key] = new TypedReference((string) $serviceMap[$key], $type, $optionalBehavior ?: ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $name);
95100
unset($serviceMap[$key]);
96101
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterServiceSubscribersPassTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,17 @@ public function testServiceSubscriberWithSemanticId()
204204
$subscriber = new class() implements ServiceSubscriberInterface {
205205
public static function getSubscribedServices()
206206
{
207-
return array('some.service' => 'stdClass');
207+
return array(
208+
'some.service' => 'stdClass',
209+
'some_service' => 'stdClass',
210+
'another_service' => 'stdClass',
211+
);
208212
}
209213
};
210214
$container->register('some.service', 'stdClass');
211215
$container->setAlias('stdClass $someService', 'some.service');
216+
$container->setAlias('stdClass $some_service', 'some.service');
217+
$container->setAlias('stdClass $anotherService', 'some.service');
212218
$container->register('foo', \get_class($subscriber))
213219
->addMethodCall('setContainer', array(new Reference(PsrContainerInterface::class)))
214220
->addTag('container.service_subscriber');
@@ -221,13 +227,17 @@ public static function getSubscribedServices()
221227

222228
$expected = array(
223229
'some.service' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'some.service')),
230+
'some_service' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'some_service')),
231+
'another_service' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'anotherService')),
224232
);
225233
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
226234

227235
(new AutowirePass())->process($container);
228236

229237
$expected = array(
230238
'some.service' => new ServiceClosureArgument(new TypedReference('some.service', 'stdClass')),
239+
'some_service' => new ServiceClosureArgument(new TypedReference('stdClass $some_service', 'stdClass')),
240+
'another_service' => new ServiceClosureArgument(new TypedReference('stdClass $anotherService', 'stdClass')),
231241
);
232242
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
233243
}

0 commit comments

Comments
 (0)