Skip to content

Commit df6b238

Browse files
committed
Throw a sensible exception when controller has been removed
1 parent 22a6a7e commit df6b238

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ protected function createController($controller)
7575
return $service;
7676
}
7777

78+
$this->throwExceptionIfControllerWasRemoved($controller);
79+
7880
throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller));
7981
}
8082

@@ -94,10 +96,19 @@ protected function instantiateController($class)
9496
} catch (\TypeError $e) {
9597
}
9698

97-
if ($this->container instanceof Container && isset($this->container->getRemovedIds()[$class])) {
98-
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);
99-
}
99+
$this->throwExceptionIfControllerWasRemoved($class, $e);
100100

101101
throw $e;
102102
}
103+
104+
/**
105+
* @param string $controller
106+
* @param \Exception|\Throwable|null $previous
107+
*/
108+
private function throwExceptionIfControllerWasRemoved($controller, $previous = null)
109+
{
110+
if ($this->container instanceof Container && isset($this->container->getRemovedIds()[$controller])) {
111+
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"?', $controller), 0, $previous);
112+
}
113+
}
103114
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,32 @@ public function testNonInstantiableControllerWithCorrespondingService()
175175
$this->assertSame(array($service, 'action'), $controller);
176176
}
177177

178+
/**
179+
* @expectedException \LogicException
180+
* @expectedExceptionMessage Controller "app.my_controller" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?
181+
*/
182+
public function testExceptionWhenUsingRemovedControllerService()
183+
{
184+
$container = $this->getMockBuilder(Container::class)->getMock();
185+
$container->expects($this->at(0))
186+
->method('has')
187+
->with('app.my_controller')
188+
->will($this->returnValue(true))
189+
;
190+
191+
$container->expects($this->atLeastOnce())
192+
->method('getRemovedIds')
193+
->with()
194+
->will($this->returnValue(array('app.my_controller' => true)))
195+
;
196+
197+
$resolver = $this->createControllerResolver(null, $container);
198+
199+
$request = Request::create('/');
200+
$request->attributes->set('_controller', 'app.my_controller');
201+
$resolver->getController($request);
202+
}
203+
178204
/**
179205
* @dataProvider getUndefinedControllers
180206
*/

0 commit comments

Comments
 (0)