Skip to content

[HttpKernel] Add a better error messages when passing a private or non-tagged controller #25201

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

Merged
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 @@ -13,6 +13,7 @@

use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Request;

/**
Expand Down Expand Up @@ -86,6 +87,17 @@ protected function instantiateController($class)
return $this->container->get($class);
}

return parent::instantiateController($class);
try {
return parent::instantiateController($class);
} catch (\ArgumentCountError $e) {
} catch (\ErrorException $e) {
} catch (\TypeError $e) {
}

if ($this->container instanceof Container && in_array($class, $this->container->getRemovedIds(), true)) {
throw new \LogicException(sprintf('Controller "%s" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?', $class), 0, $e);
}

throw $e;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ContainerControllerResolver;

Expand Down Expand Up @@ -106,6 +108,48 @@ public function testNonInstantiableController()
$this->assertSame(array(NonInstantiableController::class, 'action'), $controller);
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage Controller "Symfony\Component\HttpKernel\Tests\Controller\ImpossibleConstructController" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?
*/
public function testNonConstructController()
{
$container = $this->getMockBuilder(Container::class)->getMock();
$container->expects($this->at(0))
->method('has')
->with(ImpossibleConstructController::class)
->will($this->returnValue(true))
;

$container->expects($this->at(1))
->method('has')
->with(ImpossibleConstructController::class)
->will($this->returnValue(false))
;

$container->expects($this->atLeastOnce())
->method('getRemovedIds')
->with()
->will($this->returnValue(array(ImpossibleConstructController::class)))
;

$resolver = $this->createControllerResolver(null, $container);
$request = Request::create('/');
$request->attributes->set('_controller', array(ImpossibleConstructController::class, 'action'));

if (\PHP_VERSION_ID < 70100) {
ErrorHandler::register();
try {
$resolver->getController($request);
} finally {
restore_error_handler();
restore_exception_handler();
}
} else {
$resolver->getController($request);
}
}

public function testNonInstantiableControllerWithCorrespondingService()
{
$service = new \stdClass();
Expand Down Expand Up @@ -196,3 +240,14 @@ public static function action()
{
}
}

class ImpossibleConstructController
{
public function __construct($toto, $controller)
{
}

public function action()
{
}
}