* * 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 */ 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('Failed finding controller "%s:%s" from namespace "%s" (%s)', $bundle, $controller, $b->getNamespace(), $try); } } else { $class = $try; break; } } if (null === $class) { if (null !== $this->logger) { foreach ($logs as $log) { $this->logger->info($log); } } throw new \InvalidArgumentException(sprintf('Unable to find controller "%s:%s".', $bundle, $controller)); } return $class.'::'.$action.'Action'; } }