83 lines
2.5 KiB
PHP
83 lines
2.5 KiB
PHP
|
<?php
|
||
|
|
||
|
/*
|
||
|
* This file is part of the Symfony package.
|
||
|
*
|
||
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.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\Kernel;
|
||
|
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.potencier@symfony-project.com>
|
||
|
*/
|
||
|
class ControllerNameParser
|
||
|
{
|
||
|
protected $kernel;
|
||
|
protected $logger;
|
||
|
|
||
|
/**
|
||
|
* Constructor.
|
||
|
*
|
||
|
* @param Kernel $kernel A Kernel instance
|
||
|
* @param LoggerInterface $logger A LoggerInterface instance
|
||
|
*/
|
||
|
public function __construct(Kernel $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 controler (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';
|
||
|
}
|
||
|
}
|