8000 adjust location of controller autowire check · symfony/symfony@d9b0417 · GitHub
[go: up one dir, main page]

Skip to content

Commit d9b0417

Browse files
committed
adjust location of controller autowire check
1 parent f359ada commit d9b0417

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,6 @@ public function process(ContainerBuilder $container)
121121
$args = [];
122122
foreach ($parameters as $p) {
123123
/** @var \ReflectionParameter $p */
124-
if (class_exists(Autowire::class) && $attr = $p->getAttributes(Autowire::class)[0] ?? null) {
125-
$value = $attr->newInstance()->value;
126-
127-
if ($value instanceof Reference) {
128-
$args[$p->name] = new Reference($value, $p->allowsNull() ? ContainerInterface::NULL_ON_INVALID_REFERENCE : ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE);
129-
} else {
130-
$args[$p->name] = new Reference('.value.'.$container->hash($value));
131-
$container->register((string) $args[$p->name], 'mixed')
132-
->setFactory('current')
133-
->addArgument([$value]);
134-
}
135-
136-
continue;
137-
}
138-
139124
$type = ltrim($target = (string) ProxyHelper::getTypeHint($r, $p), '\\');
140125
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
141126

@@ -164,7 +149,7 @@ public function process(ContainerBuilder $container)
164149
}
165150

166151
continue;
167-
} elseif (!$type || !$autowire || '\\' !== $target[0]) {
152+
} elseif (!$autowire || (!$p->getAttributes(Autowire::class) && (!$type || '\\' !== $target[0]))) {
168153
continue;
169154
} elseif (is_subclass_of($type, \UnitEnum::class)) {
170155
// do not attempt to register enum typed arguments if not already present in bindings
@@ -177,6 +162,21 @@ public function process(ContainerBuilder $container)
177162
continue;
178163
}
179164

165+
if (class_exists(Autowire::class) && $attr = $p->getAttributes(Autowire::class)[0] ?? null) {
166+
$value = $attr->newInstance()->value;
167+
168+
if ($value instanceof Reference) {
169+
$args[$p->name] = $type ? new TypedReference($value, $type, $invalidBehavior, $p->name) : new Reference($value, $invalidBehavior);
170+
} else {
171+
$args[$p->name] = new Reference('.value.'.$container->hash($value));
172+
$container->register((string) $args[$p->name], 'mixed')
173+
->setFactory('current')
174+
->addArgument([$value]);
175+
}
176+
177+
continue;
178+
}
179+
180180
if ($type && !$p->isOptional() && !$p->allowsNull() && !class_exists($type) && !interface_exists($type, false)) {
181181
$message = sprintf('Cannot determine controller argument for "%s::%s()": the $%s argument is type-hinted with the non-existent class or interface: "%s".', $class, $r->name, $p->name, $type);
182182

src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,12 @@ public function testAutowireAttribute()
458458

459459
$container->compile();
460460

461-
$locator = $container->get($locatorId);
461+
$locator = $container->get($locatorId)->get('foo::fooAction');
462462

463-
$this->assertInstanceOf(\stdClass::class, $locator->get('foo::fooAction')->get('service1'));
464-
$this->assertSame('foo/bar', $locator->get('foo::fooAction')->get('value'));
465-
$this->assertSame('foo', $locator->get('foo::fooAction')->get('expression'));
466-
$this->assertNull($locator->get('foo::fooAction')->get('service2'));
463+
$this->assertInstanceOf(\stdClass::class, $locator->get('service1'));
464+
$this->assertSame('foo/bar', $locator->get('value'));
465+
$this->assertSame('foo', $locator->get('expression'));
466+
$this->assertFalse($locator->has('service2'));
467467
}
468468
}
469469

0 commit comments

Comments
 (0)
0