merged branch Tobion/controllernameparser (PR #6297)

This PR was submitted for the master branch but it was merged into the 2.1 branch instead (closes #6297).

Commits
-------

444fea4 [FrameworkBundle] refactor ControllerNameParser

Discussion
----------

[FrameworkBundle] refactor ControllerNameParser

bc break: no

---------------------------------------------------------------------------

by Tobion at 2012-12-12T16:38:12Z

Maybe merge into 2.1 instead? So master and 2.1 do not drift apart by pure refactorings? It also includes a phpdoc fix.
This commit is contained in:
Fabien Potencier 2012-12-13 15:04:21 +01:00
commit 6e499a368b

View File

@ -38,6 +38,11 @@ class ControllerNameParser
* Converts a short notation a:b:c to a class::method. * Converts a short notation a:b:c to a class::method.
* *
* @param string $controller A short notation controller (a:b:c) * @param string $controller A short notation controller (a:b:c)
*
* @return string A string with class::method
*
* @throws \InvalidArgumentException when the specified bundle is not enabled
* or the controller cannot be found
*/ */
public function parse($controller) public function parse($controller)
{ {
@ -47,40 +52,23 @@ class ControllerNameParser
list($bundle, $controller, $action) = $parts; list($bundle, $controller, $action) = $parts;
$controller = str_replace('/', '\\', $controller); $controller = str_replace('/', '\\', $controller);
$class = null; $bundles = array();
$logs = array();
// this throws an exception if there is no such bundle
foreach ($this->kernel->getBundle($bundle, false) as $b) { foreach ($this->kernel->getBundle($bundle, false) as $b) {
$try = $b->getNamespace().'\\Controller\\'.$controller.'Controller'; $try = $b->getNamespace().'\\Controller\\'.$controller.'Controller';
if (!class_exists($try)) { if (class_exists($try)) {
$logs[] = sprintf('Unable to find controller "%s:%s" - class "%s" does not exist.', $bundle, $controller, $try); return $try.'::'.$action.'Action';
} else {
$class = $try;
break;
} }
$bundles[] = $b->getName();
$msg = sprintf('Unable to find controller "%s:%s" - class "%s" does not exist.', $bundle, $controller, $try);
} }
if (null === $class) { if (count($bundles) > 1) {
$this->handleControllerNotFoundException($bundle, $controller, $logs); $msg = sprintf('Unable to find controller "%s:%s" in bundles %s.', $bundle, $controller, implode(', ', $bundles));
} }
return $class.'::'.$action.'Action';
}
private function handleControllerNotFoundException($bundle, $controller, array $logs)
{
// just one log, return it as the exception
if (1 == count($logs)) {
throw new \InvalidArgumentException($logs[0]);
}
// many logs, use a message that mentions each searched bundle
$names = array();
foreach ($this->kernel->getBundle($bundle, false) as $b) {
$names[] = $b->getName();
}
$msg = sprintf('Unable to find controller "%s:%s" in bundles %s.', $bundle, $controller, implode(', ', $names));
throw new \InvalidArgumentException($msg); throw new \InvalidArgumentException($msg);
} }
} }