Вход Регистрация
Файл: symfony-2.7/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php
Строк: 169
<?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 SymfonyBundleFrameworkBundleController;

use 
PsrLogLoggerInterface;
use 
SymfonyComponentHttpKernelControllerControllerResolver as BaseControllerResolver;
use 
SymfonyComponentDependencyInjectionContainerInterface;
use 
SymfonyComponentDependencyInjectionContainerAwareInterface;

/**
 * ControllerResolver.
 *
 * @author Fabien Potencier <fabien@symfony.com>
 */
class ControllerResolver extends BaseControllerResolver
{
    protected 
$container;
    protected 
$parser;

    
/**
     * Constructor.
     *
     * @param ContainerInterface   $container A ContainerInterface instance
     * @param ControllerNameParser $parser    A ControllerNameParser instance
     * @param LoggerInterface      $logger    A LoggerInterface instance
     */
    
public function __construct(ContainerInterface $container, ControllerNameParser $parser, LoggerInterface $logger = null)
    {
        
$this->container = $container;
        
$this->parser = $parser;

        
parent::__construct($logger);
    }

    
/**
     * Returns a callable for the given controller.
     *
     * @param string $controller A Controller string
     *
     * @return mixed A PHP callable
     *
     * @throws LogicException           When the name could not be parsed
     * @throws InvalidArgumentException When the controller class does not exist
     */
    
protected function createController($controller)
    {
        if (
false === strpos($controller, '::')) {
            
$count = substr_count($controller, ':');
            if (
2 == $count) {
                
// controller in the a:b:c notation then
                
$controller = $this->parser->parse($controller);
            } elseif (
1 == $count) {
                
// controller in the service:method notation
                
list($service, $method) = explode(':', $controller, 2);

                return array(
$this->container->get($service), $method);
            } elseif (
$this->container->has($controller) && method_exists($service = $this->container->get($controller), '__invoke')) {
                return 
$service;
            } else {
                throw new 
LogicException(sprintf('Unable to parse the controller name "%s".', $controller));
            }
        }

        list(
$class, $method) = explode('::', $controller, 2);

        if (!
class_exists($class)) {
            throw new 
InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
        }

        
$controller = new $class();
        if (
$controller instanceof ContainerAwareInterface) {
            
$controller->setContainer($this->container);
        }

        return array(
$controller, $method);
    }
}
Онлайн: 0
Реклама