Skip to content

Commit 6345f3b

Browse files
author
Amrouche Hamza
committed
[HttpKernel] Add a better error messages when passing a private or non-tagged controller
1 parent 1f14f4d commit 6345f3b

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/Symfony/Component/HttpKernel/Controller/ContainerControllerResolver.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Psr\Container\ContainerInterface;
1515
use Psr\Log\LoggerInterface;
16+
use Symfony\Component\Debug\Exception\FatalThrowableError;
17+
use Symfony\Component\DependencyInjection\Container;
1618
use Symfony\Component\HttpFoundation\Request;
1719

1820
/**
@@ -86,6 +88,16 @@ protected function instantiateController($class)
8688
return $this->container->get($class);
8789
}
8890

89-
return parent::instantiateController($class);
91+
$e = null;
92+
93+
try {
94+
return parent::instantiateController($class);
95+
} catch (FatalThrowableError $e) {
96+
} catch (\ArgumentCountError $e) {
97+
}
98+
99+
if (null !== $e && $this->container instanceof Container && in_array($class, $this->container->getRemovedIds())) {
100+
throw new \InvalidArgumentException(sprintf('The controller %s seems to be removed from the container, which means that he maybe private, add the tag \'controller.service_arguments\' or made it public.', $class));
101+
}
90102
}
91103
}

src/Symfony/Component/HttpKernel/Tests/Controller/ContainerControllerResolverTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Psr\Container\ContainerInterface;
1515
use Psr\Log\LoggerInterface;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpKernel\Controller\ContainerControllerResolver;
1819

@@ -106,6 +107,43 @@ public function testNonInstantiableController()
106107
$this->assertSame(array(NonInstantiableController::class, 'action'), $controller);
107108
}
108109

110+
public function testNonConstructController()
111+
{
112+
$container = $this->getMockBuilder(ContainerBuilder::class)->getMock();
113+
$container->expects($this->at(0))
114+
->method('has')
115+
->with(ImpossibleConstructController::class)
116+
->will($this->returnValue(true))
117+
;
118+
119+
$container->expects($this->at(1))
120+
->method('has')
121+
->with(ImpossibleConstructController::class)
122+
->will($this->returnValue(false))
123+
;
124+
125+
$container->expects($this->atLeastOnce())
126+
->method('getRemovedIds')
127+
->with()
128+
->will($this->returnValue(array(ImpossibleConstructController::class)))
129+
;
130+
131+
if (method_exists($this, 'expectException')) {
132+
$this->expectException(\InvalidArgumentException::class);
133+
$this->expectExceptionMessage('The controller Symfony\Component\HttpKernel\Tests\Controller\ImpossibleConstructController seems to be removed from the container, which means that he maybe private, add the tag \'controller.service_arguments\' or made it public.');
134+
} else {
135+
$this->setExpectedException(\InvalidArgumentException::class, 'The controller Symfony\Component\HttpKernel\Tests\Controller\ImpossibleConstructController seems to be removed from the container, which means that he maybe private, add the tag \'controller.service_arguments\' or made it public.');
136+
}
137+
138+
$resolver = $this->createControllerResolver(null, $container);
139+
$request = Request::create('/');
140+
$request->attributes->set('_controller', array(ImpossibleConstructController::class, 'action'));
141+
142+
$controller = $resolver->getController($request);
143+
144+
$this->assertSame(array(ImpossibleConstructController::class, 'action'), $controller);
145+
}
146+
109147
public function testNonInstantiableControllerWithCorrespondingService()
110148
{
111149
$service = new \stdClass();
@@ -196,3 +234,14 @@ public static function action()
196234
{
197235
}
198236
}
237+
238+
class ImpossibleConstructController
239+
{
240+
public function __construct($toto, $controller)
241+
{
242+
}
243+
244+
public function action()
245+
{
246+
}
247+
}

0 commit comments

Comments
 (0)