[FrameworkBundle] Improve performance of ControllerNameParser

This commit is contained in:
Jáchym Toušek 2016-11-01 12:50:51 +01:00
parent 620ea20f49
commit cf333f32c5
2 changed files with 15 additions and 11 deletions

View File

@ -46,11 +46,12 @@ class ControllerNameParser
*/ */
public function parse($controller) public function parse($controller)
{ {
$originalController = $controller; $parts = explode(':', $controller);
if (3 !== count($parts = explode(':', $controller))) { if (3 !== count($parts) || in_array('', $parts, true)) {
throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid "a:b:c" controller string.', $controller)); throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid "a:b:c" controller string.', $controller));
} }
$originalController = $controller;
list($bundle, $controller, $action) = $parts; list($bundle, $controller, $action) = $parts;
$controller = str_replace('/', '\\', $controller); $controller = str_replace('/', '\\', $controller);
$bundles = array(); $bundles = array();

View File

@ -21,7 +21,7 @@ use Psr\Log\LoggerInterface;
* DelegatingLoader delegates route loading to other loaders using a loader resolver. * DelegatingLoader delegates route loading to other loaders using a loader resolver.
* *
* This implementation resolves the _controller attribute from the short notation * This implementation resolves the _controller attribute from the short notation
* to the fully-qualified form (from a:b:c to class:method). * to the fully-qualified form (from a:b:c to class::method).
* *
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
*/ */
@ -85,15 +85,18 @@ class DelegatingLoader extends BaseDelegatingLoader
$this->loading = false; $this->loading = false;
foreach ($collection->all() as $route) { foreach ($collection->all() as $route) {
if ($controller = $route->getDefault('_controller')) { $controller = $route->getDefault('_controller');
try { if (!$controller) {
$controller = $this->parser->parse($controller); continue;
} catch (\InvalidArgumentException $e) {
// unable to optimize unknown notation
}
$route->setDefault('_controller', $controller);
} }
try {
$controller = $this->parser->parse($controller);
} catch (\InvalidArgumentException $e) {
// unable to optimize unknown notation
}
$route->setDefault('_controller', $controller);
} }
return $collection; return $collection;