Skip to content

Commit

Permalink
bug #29546 [DI] map snake-case ids of service subscribers to camel-ca…
Browse files Browse the repository at this point in the history
…se 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
  • Loading branch information
nicolas-grekas committed Dec 17, 2018
2 parents ab95ae3 + af17da9 commit 5333bd2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Expand Up @@ -91,6 +91,11 @@ protected function processValue($value, $isRoot = false)
$name = null;
}

if (null !== $name && !$this->container->has($name) && !$this->container->has($type.' $'.$name)) {
$camelCaseName = lcfirst(str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $name))));
$name = $this->container->has($type.' $'.$camelCaseName) ? $camelCaseName : $name;
}

$subscriberMap[$key] = new TypedReference((string) $serviceMap[$key], $type, $optionalBehavior ?: ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $name);
unset($serviceMap[$key]);
}
Expand Down
Expand Up @@ -204,11 +204,17 @@ public function testServiceSubscriberWithSemanticId()
$subscriber = new class() implements ServiceSubscriberInterface {
public static function getSubscribedServices()
{
return array('some.service' => 'stdClass');
return array(
'some.service' => 'stdClass',
'some_service' => 'stdClass',
'another_service' => 'stdClass',
);
}
};
$container->register('some.service', 'stdClass');
$container->setAlias('stdClass $someService', 'some.service');
$container->setAlias('stdClass $some_service', 'some.service');
$container->setAlias('stdClass $anotherService', 'some.service');
$container->register('foo', \get_class($subscriber))
->addMethodCall('setContainer', array(new Reference(PsrContainerInterface::class)))
->addTag('container.service_subscriber');
Expand All @@ -221,13 +227,17 @@ public static function getSubscribedServices()

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

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

$expected = array(
'some.service' => new ServiceClosureArgument(new TypedReference('some.service', 'stdClass')),
'some_service' => new ServiceClosureArgument(new TypedReference('stdClass $some_service', 'stdClass')),
'another_service' => new ServiceClosureArgument(new TypedReference('stdClass $anotherService', 'stdClass')),
);
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
}
Expand Down

0 comments on commit 5333bd2

Please sign in to comment.