Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[HttpKernel] Ignore autowiring errors on controllers unless argument's type is an interface #46275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\TypedReference;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* Creates the service-locators required by ServiceValueResolver.
Expand Down Expand Up @@ -70,13 +72,21 @@ public function process(ContainerBuilder $container)
if (!$r = $container->getReflectionClass($class)) {
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
}
$isContainerAware = $r->implementsInterface(ContainerAwareInterface::class) || is_subclass_of($class, AbstractController::class);

$excludedMethod = [];
if ($r->implementsInterface(ContainerAwareInterface::class) || is_subclass_of($class, AbstractController::class)) {
$excludedMethod['setContainer'] = true;
}
if ($r->implementsInterface(KernelInterface::class)) {
$excludedMethod['registerContainerConfiguration'] = true;
$excludedMethod['loadRoutes'] = true;
}

// get regular public methods
$methods = [];
$arguments = [];
foreach ($r->getMethods(\ReflectionMethod::IS_PUBLIC) as $r) {
if ('setContainer' === $r->name && $isContainerAware) {
if (!$r->getNumberOfParameters() || isset($excludedMethod[$r->name])) {
continue;
}
if (!$r->isConstructor() && !$r->isDestructor() && !$r->isAbstract()) {
Expand Down Expand Up @@ -150,11 +160,11 @@ public function process(ContainerBuilder $container)
} elseif (is_subclass_of($type, \UnitEnum::class)) {
// do not attempt to register enum typed arguments if not already present in bindings
continue;
} elseif (!$p->allowsNull()) {
} elseif (!$p->allowsNull() && interface_exists($type, false)) {
$invalidBehavior = ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE;
}

if (Request::class === $type || SessionInterface::class === $type) {
if (Request::class === $type || Response::class === $type || SessionInterface::class === $type) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ public function testControllerNameIsAnArray()

public function testErrorIsTruncated()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Cannot autowire argument $dummy of "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\DummyController::index()": it references class "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\DummyService" but no such service exists.');
$container = new ContainerBuilder();
$container->addCompilerPass(new RegisterControllerArgumentLocatorsPass());

Expand All @@ -119,7 +117,10 @@ public function testErrorIsTruncated()
$container->compile();

$request = $this->requestWithAttributes(['_controller' => [DummyController::class, 'index']]);
$argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
$argument = new ArgumentMetadata('dummy', DummyServiceInterface::class, false, false, null);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Cannot autowire argument $dummy of "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\DummyController::index()": it references interface "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\DummyServiceInterface" but no such service exists.');
$container->get('argument_resolver.service')->resolve($request, $argument)->current();
}

Expand All @@ -145,13 +146,17 @@ private function assertYieldEquals(array $expected, \Generator $generator)
}
}

interface DummyServiceInterface
{
}

class DummyService
{
}

class DummyController
{
public function index(DummyService $dummy)
public function index(DummyServiceInterface $dummy)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function testAllActions()
$this->assertSame(ServiceLocator::class, $locator->getClass());
$this->assertFalse($locator->isPublic());

$expected = ['bar' => new ServiceClosureArgument(new TypedReference(ControllerDummy::class, ControllerDummy::class, ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE, 'bar'))];
$expected = ['bar' => new ServiceClosureArgument(new TypedReference(ControllerDummy::class, ControllerDummy::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'bar'))];
$this->assertEquals($expected, $locator->getArgument(0));
}

Expand Down Expand Up @@ -437,8 +437,8 @@ public function testBindWithTarget()

$expected = [
'apiKey' => new ServiceClosureArgument(new Reference('the_api_key')),
'service1' => new ServiceClosureArgument(new TypedReference(ControllerDummy::class, ControllerDummy::class, ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE, 'imageStorage')),
'service2' => new ServiceClosureArgument(new TypedReference(ControllerDummy::class, ControllerDummy::class, ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE, 'service2')),
'service1' => new ServiceClosureArgument(new TypedReference(ControllerDummy::class, ControllerDummy::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'imageStorage')),
'service2' => new ServiceClosureArgument(new TypedReference(ControllerDummy::class, ControllerDummy::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'service2')),
];
$this->assertEquals($expected, $locator->getArgument(0));
}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.