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

Skip to content

Commit d66eb54

Browse files
committed
AutowirePass/PhpDumper adjustments per @nicolas-grekas
1 parent 7805201 commit d66eb54

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ protected function processValue($value, bool $isRoot = false)
103103
private function doProcessValue($value, bool $isRoot = false)
104104
{
105105
if ($value instanceof TypedReference) {
106-
if ($ref = $this->getAutowiredReference($value)) {
106+
if ($ref = $this->getAutowiredReference($value, true)) {
107107
return $ref;
108108
}
109109
if (ContainerBuilder::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE === $value->getInvalidBehavior()) {
@@ -294,7 +294,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
294294
}
295295

296296
$getValue = function () use ($type, $parameter, $class, $method) {
297-
if (!$value = $this->getAutowiredReference($ref = new TypedReference($type, $type, ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE, Target::parseName($parameter)))) {
297+
if (!$value = $this->getAutowiredReference($ref = new TypedReference($type, $type, ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE, Target::parseName($parameter)), true)) {
298298
$failureMessage = $this->createTypeNotFoundMessageCallback($ref, sprintf('argument "$%s" of method "%s()"', $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method));
299299

300300
if ($parameter->isDefaultValueAvailable()) {
@@ -349,7 +349,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
349349
/**
350350
* Returns a reference to the service matching the given type, if any.
351351
*/
352-
private function getAutowiredReference(TypedReference $reference): ?TypedReference
352+
private function getAutowiredReference(TypedReference $reference, bool $filterType): ?TypedReference
353353
{
354354
$this->lastFailure = null;
355355
$type = $reference->getType();
@@ -358,6 +358,19 @@ private function getAutowiredReference(TypedReference $reference): ?TypedReferen
358358
return $reference;
359359
}
360360

361+
if ($filterType && preg_match('/([&|])/', $type, $m)) {
362+
$types = explode($m[0], $type);
363+
364+
if ('|' === $m[0]) {
365+
// remove built-in types
366+
$types = array_filter($types, function($type) { return !\in_array(strtolower($type), ['int', 'string', 'array', 'bool', 'float', 'iterable', 'object', 'mixed', 'null'], true); });
367+
}
368+
369+
sort($types);
370+
371+
$types = implode($m[0], $types);
372+
}
373+
361374
if (null !== $name = $reference->getName()) {
362375
if ($this->container->has($alias = $type.' $'.$name) && !$this->container->findDefinition($alias)->isAbstract()) {
363376
return new TypedReference($alias, $type, $reference->getInvalidBehavior());

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1754,7 +1754,7 @@ private function dumpValue($value, bool $interpolate = true): string
17541754

17551755
$returnedType = '';
17561756
if ($value instanceof TypedReference) {
1757-
$returnedType = sprintf(': %s\%s', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE >= $value->getInvalidBehavior() ? '' : '?', $value->getType());
1757+
$returnedType = sprintf(': %s\%s', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE >= $value->getInvalidBehavior() ? '' : '?', str_replace(['|', '&'], ['|\\', '&\\'], $value->getType()));
17581758
}
17591759

17601760
$code = sprintf('return %s;', $code);

0 commit comments

Comments
 (0)