8000 [FrameworkBundle] refactor ControllerNameParser by Tobion · Pull Request #6297 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] refactor ControllerNameParser #6297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function __construct(KernelInterface $kernel)
* Converts a short notation a:b:c to a class::method.
*
* @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)
{
Expand All @@ -47,39 +52,22 @@ public function parse($controller)

list($bundle, $controller, $action) = $parts;
$controller = str_replace('/', '\\', $controller);
$class = null;
$logs = array();
$bundles = array();

// this throws an exception if there is no such bundle
foreach ($this->kernel->getBundle($bundle, false) as $b) {
$try = $b->getNamespace().'\\Controller\\'.$controller.'Controller';
if (!class_exists($try)) {
$logs[] = sprintf('Unable to find controller "%s:%s" - class "%s" does not exist.', $bundle, $controller, $try);
} else {
$class = $try;

break;
if (class_exists($try)) {
return $try.'::'.$action.'Action';
}
}

if (null === $class) {
$this->handleControllerNotFoundException($bundle, $controller, $logs);
$bundles[] = $b->getName();
$msg = sprintf('Unable to find controller "%s:%s" - class "%s" does not exist.', $bundle, $controller, $try);
}

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();
if (count($bundles) > 1) {
$msg = sprintf('Unable to find controller "%s:%s" in bundles %s.', $bundle, $controller, implode(', ', $bundles));
}
$msg = sprintf('Unable to find controller "%s:%s" in bundles %s.', $bundle, $controller, implode(', ', $names));

throw new \InvalidArgumentException($msg);
}
Expand Down
0