Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e58293b

Browse files
committed
[DependencyInjection] Refactor ServiceLocatorTagPass service matching
1 parent 7c08e43 commit e58293b

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
1616
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1717
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
use Symfony\Component\DependencyInjection\ContainerInterface;
1819
use Symfony\Component\DependencyInjection\Definition;
1920
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2021
use Symfony\Component\DependencyInjection\Reference;
2122
use Symfony\Component\DependencyInjection\ServiceLocator;
23+
use Symfony\Component\DependencyInjection\TypedReference;
2224

2325
/**
2426
* Applies the "container.service_locator" tag by wrapping references into ServiceClosureArgument instances.
@@ -42,15 +44,31 @@ protected function processValue($value, $isRoot = false)
4244

4345
$arguments = $value->getArguments();
4446
if (!isset($arguments[0]) || !\is_array($arguments[0])) {
45-
throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": an array of references is expected as first argument when the "container.service_locator" tag is set.', $this->currentId));
47+
throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": an array of references or service types is expected as first argument when the "container.service_locator" tag is set.', $this->currentId));
4648
}
4749

4850
foreach ($arguments[0] as $k => $v) {
4951
if ($v instanceof ServiceClosureArgument) {
5052
continue;
5153
}
54+
55+
if (\is_string($type = $v) && preg_match('/^\??[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/', $type)) {
56+
if ($optionalBehavior = '?' === $type[0]) {
57+
$type = substr($type, 1);
58+
$optionalBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
59+
}
60+
61+
$v = new TypedReference((string) new Reference($type), $type, $optionalBehavior ?: ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
62+
}
63+
5264
if (!$v instanceof Reference) {
53-
throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": an array of references is expected as first argument when the "container.service_locator" tag is set, "%s" found for key "%s".', $this->currentId, \is_object($v) ? \get_class($v) : \gettype($v), $k));
65+
throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": an array of references or service types is expected as first argument when the "container.service_locator" tag is set, "%s" found for key "%s".', $this->currentId, \is_object($v) ? \get_class($v) : \gettype($v), $k));
66+
}
67+
68+
if (\is_int($k)) {
69+
unset($arguments[0][$k]);
70+
71+
$k = (string) $v;
5472
}
5573
$arguments[0][$k] = new ServiceClosureArgument($v);
5674
}

0 commit comments

Comments
 (0)