Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FrameworkBundle][HttpKernel] Provide intuitive error message when a …
…controller fails because it's not registered as a service
- Loading branch information
Showing
5 changed files
with
231 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...ony/Component/HttpKernel/Controller/ArgumentResolver/NotTaggedControllerValueResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\Exception\RuntimeException; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
|
||
/** | ||
* Provides an intuitive error message when controller fails because it is not registered as a service. | ||
* | ||
* @author Simeon Kolev <simeon.kolev9@gmail.com> | ||
*/ | ||
final class NotTaggedControllerValueResolver implements ArgumentValueResolverInterface | ||
{ | ||
private $container; | ||
|
||
public function __construct(ContainerInterface $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(Request $request, ArgumentMetadata $argument) | ||
{ | ||
$controller = $request->attributes->get('_controller'); | ||
|
||
if (\is_array($controller) && \is_callable($controller, true) && \is_string($controller[0])) { | ||
$controller = $controller[0].'::'.$controller[1]; | ||
} elseif (!\is_string($controller) || '' === $controller) { | ||
return false; | ||
} | ||
|
||
if ('\\' === $controller[0]) { | ||
$controller = ltrim($controller, '\\'); | ||
} | ||
|
||
if (!$this->container->has($controller) && false !== $i = strrpos($controller, ':')) { | ||
$controller = substr($controller, 0, $i).strtolower(substr($controller, $i)); | ||
} | ||
|
||
return false === $this->container->has($controller); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve(Request $request, ArgumentMetadata $argument) | ||
{ | ||
if (\is_array($controller = $request->attributes->get('_controller'))) { | ||
$controller = $controller[0].'::'.$controller[1]; | ||
} | ||
|
||
if ('\\' === $controller[0]) { | ||
$controller = ltrim($controller, '\\'); | ||
} | ||
|
||
if (!$this->container->has($controller)) { | ||
$i = strrpos($controller, ':'); | ||
$controller = substr($controller, 0, $i).strtolower(substr($controller, $i)); | ||
} | ||
|
||
$what = sprintf('argument $%s of "%s()"', $argument->getName(), $controller); | ||
$message = sprintf('Could not resolve %s, maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"?', $what); | ||
|
||
throw new RuntimeException($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
...ent/HttpKernel/Tests/Controller/ArgumentResolver/NotTaggedControllerValueResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\ServiceLocator; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\NotTaggedControllerValueResolver; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
|
||
class NotTaggedControllerValueResolverTest extends TestCase | ||
{ | ||
public function testDoSupportWhenControllerDoNotExists() | ||
{ | ||
$resolver = new NotTaggedControllerValueResolver(new ServiceLocator([])); | ||
$argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null); | ||
$request = $this->requestWithAttributes(['_controller' => 'my_controller']); | ||
|
||
$this->assertTrue($resolver->supports($request, $argument)); | ||
} | ||
|
||
public function testDoNotSupportWhenControllerExists() | ||
{ | ||
$resolver = new NotTaggedControllerValueResolver(new ServiceLocator([ | ||
'App\\Controller\\Mine::method' => function () { | ||
return new ServiceLocator([ | ||
'dummy' => function () { | ||
return new \stdClass(); | ||
}, | ||
]); | ||
}, | ||
])); | ||
$argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null); | ||
$request = $this->requestWithAttributes(['_controller' => 'App\\Controller\\Mine::method']); | ||
|
||
$this->assertFalse($resolver->supports($request, $argument)); | ||
} | ||
|
||
public function testDoNotSupportEmptyController() | ||
{ | ||
$resolver = new NotTaggedControllerValueResolver(new ServiceLocator([])); | ||
$argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null); | ||
$request = $this->requestWithAttributes(['_controller' => '']); | ||
$this->assertFalse($resolver->supports($request, $argument)); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException | ||
* @expectedExceptionMessage Could not resolve argument $dummy of "App\Controller\Mine::method()", maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"? | ||
*/ | ||
public function testController() | ||
{ | ||
$resolver = new NotTaggedControllerValueResolver(new ServiceLocator([])); | ||
$argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null); | ||
$request = $this->requestWithAttributes(['_controller' => 'App\\Controller\\Mine::method']); | ||
$this->assertTrue($resolver->supports($request, $argument)); | ||
$resolver->resolve($request, $argument); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException | ||
* @expectedExceptionMessage Could not resolve argument $dummy of "App\Controller\Mine::method()", maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"? | ||
*/ | ||
public function testControllerWithATrailingBackSlash() | ||
{ | ||
$resolver = new NotTaggedControllerValueResolver(new ServiceLocator([])); | ||
$argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null); | ||
$request = $this->requestWithAttributes(['_controller' => '\\App\\Controller\\Mine::method']); | ||
$this->assertTrue($resolver->supports($request, $argument)); | ||
$resolver->resolve($request, $argument); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException | ||
* @expectedExceptionMessage Could not resolve argument $dummy of "App\Controller\Mine::method()", maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"? | ||
*/ | ||
public function testControllerWithMethodNameStartUppercase() | ||
{ | ||
$resolver = new NotTaggedControllerValueResolver(new ServiceLocator([])); | ||
$argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null); | ||
$request = $this->requestWithAttributes(['_controller' => 'App\\Controller\\Mine::Method']); | ||
$this->assertTrue($resolver->supports($request, $argument)); | ||
$resolver->resolve($request, $argument); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException | ||
* @expectedExceptionMessage Could not resolve argument $dummy of "App\Controller\Mine::method()", maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"? | ||
*/ | ||
public function testControllerNameIsAnArray() | ||
{ | ||
$resolver = new NotTaggedControllerValueResolver(new ServiceLocator([])); | ||
$argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null); | ||
$request = $this->requestWithAttributes(['_controller' => ['App\\Controller\\Mine', 'method']]); | ||
$this->assertTrue($resolver->supports($request, $argument)); | ||
$resolver->resolve($request, $argument); | ||
} | ||
|
||
private function requestWithAttributes(array $attributes) | ||
{ | ||
$request = Request::create('/'); | ||
foreach ($attributes as $name => $value) { | ||
$request->attributes->set($name, $value); | ||
} | ||
|
||
return $request; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters