vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.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\Log\LoggerInterface;
       
    15 use Symfony\Component\HttpKernel\Controller\ControllerResolver as BaseControllerResolver;
       
    16 use Symfony\Component\DependencyInjection\ContainerInterface;
       
    17 use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
       
    18 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
       
    19 
       
    20 /**
       
    21  * ControllerResolver.
       
    22  *
       
    23  * @author Fabien Potencier <fabien@symfony.com>
       
    24  */
       
    25 class ControllerResolver extends BaseControllerResolver
       
    26 {
       
    27     protected $container;
       
    28     protected $parser;
       
    29 
       
    30     /**
       
    31      * Constructor.
       
    32      *
       
    33      * @param ContainerInterface   $container A ContainerInterface instance
       
    34      * @param ControllerNameParser $parser    A ControllerNameParser instance
       
    35      * @param LoggerInterface      $logger    A LoggerInterface instance
       
    36      */
       
    37     public function __construct(ContainerInterface $container, ControllerNameParser $parser, LoggerInterface $logger = null)
       
    38     {
       
    39         $this->container = $container;
       
    40         $this->parser = $parser;
       
    41 
       
    42         parent::__construct($logger);
       
    43     }
       
    44 
       
    45     /**
       
    46      * Returns a callable for the given controller.
       
    47      *
       
    48      * @param string $controller A Controller string
       
    49      *
       
    50      * @return mixed A PHP callable
       
    51      */
       
    52     protected function createController($controller)
       
    53     {
       
    54         if (false === strpos($controller, '::')) {
       
    55             $count = substr_count($controller, ':');
       
    56             if (2 == $count) {
       
    57                 // controller in the a:b:c notation then
       
    58                 $controller = $this->parser->parse($controller);
       
    59             } elseif (1 == $count) {
       
    60                 // controller in the service:method notation
       
    61                 list($service, $method) = explode(':', $controller);
       
    62 
       
    63                 return array($this->container->get($service), $method);
       
    64             } else {
       
    65                 throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller));
       
    66             }
       
    67         }
       
    68 
       
    69         list($class, $method) = explode('::', $controller);
       
    70 
       
    71         if (!class_exists($class)) {
       
    72             throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
       
    73         }
       
    74 
       
    75         $controller = new $class();
       
    76         if ($controller instanceof ContainerAwareInterface) {
       
    77             $controller->setContainer($this->container);
       
    78         }
       
    79 
       
    80         return array($controller, $method);
       
    81     }
       
    82 }