123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?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\Bundle\FrameworkBundle\Controller;
- use Symfony\Component\HttpKernel\KernelInterface;
- use Symfony\Component\HttpKernel\Log\LoggerInterface;
- /**
- * ControllerNameParser converts controller from the short notation a:b:c
- * (BlogBundle:Post:index) to a fully-qualified class::method string
- * (Bundle\BlogBundle\Controller\PostController::indexAction).
- *
- * @author Fabien Potencier <fabien@symfony.com>
- */
- class ControllerNameParser
- {
- protected $kernel;
- protected $logger;
- /**
- * Constructor.
- *
- * @param KernelInterface $kernel A KernelInterface instance
- * @param LoggerInterface $logger A LoggerInterface instance
- */
- public function __construct(KernelInterface $kernel, LoggerInterface $logger = null)
- {
- $this->kernel = $kernel;
- $this->logger = $logger;
- }
- /**
- * Converts a short notation a:b:c to a class::method.
- *
- * @param string $controller A short notation controller (a:b:c)
- *
- * @param string A controller (class::method)
- */
- public function parse($controller)
- {
- if (3 != count($parts = explode(':', $controller))) {
- throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid a:b:c controller string.', $controller));
- }
- list($bundle, $controller, $action) = $parts;
- $class = null;
- $logs = array();
- foreach ($this->kernel->getBundle($bundle, false) as $b) {
- $try = $b->getNamespace().'\\Controller\\'.$controller.'Controller';
- if (!class_exists($try)) {
- if (null !== $this->logger) {
- $logs[] = sprintf('Unable to find controller "%s:%s" - class "%s" does not exist.', $bundle, $controller, $try);
- }
- } else {
- $class = $try;
- break;
- }
- }
- if (null === $class) {
- $this->handleControllerNotFoundException($bundle, $controller, $logs);
- }
- 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);
- }
- }
|