vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerNameParser.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bundle\FrameworkBundle\Controller;
       
    13 
       
    14 use Symfony\Component\HttpKernel\KernelInterface;
       
    15 use Symfony\Component\HttpKernel\Log\LoggerInterface;
       
    16 
       
    17 /**
       
    18  * ControllerNameParser converts controller from the short notation a:b:c
       
    19  * (BlogBundle:Post:index) to a fully-qualified class::method string
       
    20  * (Bundle\BlogBundle\Controller\PostController::indexAction).
       
    21  *
       
    22  * @author Fabien Potencier <fabien@symfony.com>
       
    23  */
       
    24 class ControllerNameParser
       
    25 {
       
    26     protected $kernel;
       
    27 
       
    28     /**
       
    29      * Constructor.
       
    30      *
       
    31      * @param KernelInterface $kernel A KernelInterface instance
       
    32      */
       
    33     public function __construct(KernelInterface $kernel)
       
    34     {
       
    35         $this->kernel = $kernel;
       
    36     }
       
    37 
       
    38     /**
       
    39      * Converts a short notation a:b:c to a class::method.
       
    40      *
       
    41      * @param string $controller A short notation controller (a:b:c)
       
    42      *
       
    43      * @param string A controller (class::method)
       
    44      */
       
    45     public function parse($controller)
       
    46     {
       
    47         if (3 != count($parts = explode(':', $controller))) {
       
    48             throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid a:b:c controller string.', $controller));
       
    49         }
       
    50 
       
    51         list($bundle, $controller, $action) = $parts;
       
    52         $class = null;
       
    53         $logs = array();
       
    54         foreach ($this->kernel->getBundle($bundle, false) as $b) {
       
    55             $try = $b->getNamespace().'\\Controller\\'.$controller.'Controller';
       
    56             if (!class_exists($try)) {
       
    57                 $logs[] = sprintf('Unable to find controller "%s:%s" - class "%s" does not exist.', $bundle, $controller, $try);
       
    58             } else {
       
    59                 $class = $try;
       
    60 
       
    61                 break;
       
    62             }
       
    63         }
       
    64 
       
    65         if (null === $class) {
       
    66             $this->handleControllerNotFoundException($bundle, $controller, $logs);
       
    67         }
       
    68 
       
    69         return $class.'::'.$action.'Action';
       
    70     }
       
    71 
       
    72     private function handleControllerNotFoundException($bundle, $controller, array $logs)
       
    73     {
       
    74         // just one log, return it as the exception
       
    75         if (1 == count($logs)) {
       
    76             throw new \InvalidArgumentException($logs[0]);
       
    77         }
       
    78 
       
    79         // many logs, use a message that mentions each searched bundle
       
    80         $names = array();
       
    81         foreach ($this->kernel->getBundle($bundle, false) as $b) {
       
    82             $names[] = $b->getName();
       
    83         }
       
    84         $msg = sprintf('Unable to find controller "%s:%s" in bundles %s.', $bundle, $controller, implode(', ', $names));
       
    85 
       
    86         throw new \InvalidArgumentException($msg);
       
    87     }
       
    88 }