ControllerResolver.php 5.44 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Controller\ControllerResolver.
6 7
 */

8
namespace Drupal\Core\Controller;
9

10
use Drupal\Core\Routing\RouteMatch;
11
use Psr\Log\LoggerInterface;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpKernel\Controller\ControllerResolver as BaseControllerResolver;
14
use Drupal\Core\DependencyInjection\ClassResolverInterface;
15 16 17 18

/**
 * ControllerResolver to enhance controllers beyond Symfony's basic handling.
 *
19 20 21 22 23 24 25 26 27 28 29
 * It adds two behaviors:
 *
 *  - When creating a new object-based controller that implements
 *    ContainerAwareInterface, inject the container into it. While not always
 *    necessary, that allows a controller to vary the services it needs at
 *    runtime.
 *
 *  - By default, a controller name follows the class::method notation. This
 *    class adds the possibility to use a service from the container as a
 *    controller by using a service:method notation (Symfony uses the same
 *    convention).
30
 */
31
class ControllerResolver extends BaseControllerResolver implements ControllerResolverInterface {
32

33 34 35 36 37 38 39
  /**
   * The PSR-3 logger. (optional)
   *
   * @var \Psr\Log\LoggerInterface;
   */
  protected $logger;

40 41 42 43 44 45 46
  /**
   * The class resolver.
   *
   * @var \Drupal\Core\DependencyInjection\ClassResolverInterface
   */
  protected $classResolver;

47 48 49
  /**
   * Constructs a new ControllerResolver.
   *
50 51
   * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
   *   The class resolver.
52
   * @param \Psr\Log\LoggerInterface $logger
53 54
   *   (optional) A LoggerInterface instance.
   */
55 56 57
  public function __construct(ClassResolverInterface $class_resolver, LoggerInterface $logger = NULL) {
    $this->classResolver = $class_resolver;

58 59 60
    parent::__construct($logger);
  }

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  /**
   * {@inheritdoc}
   */
  public function getControllerFromDefinition($controller, $path = '') {
    if (is_array($controller) || (is_object($controller) && method_exists($controller, '__invoke'))) {
      return $controller;
    }

    if (strpos($controller, ':') === FALSE) {
      if (method_exists($controller, '__invoke')) {
        return new $controller;
      }
      elseif (function_exists($controller)) {
        return $controller;
      }
    }

    $callable = $this->createController($controller);

    if (!is_callable($callable)) {
      throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable.', $path));
    }

    return $callable;
  }


  /**
   * {@inheritdoc}
   */
  public function getController(Request $request) {
    if (!$controller = $request->attributes->get('_controller')) {
      if ($this->logger !== NULL) {
        $this->logger->warning('Unable to look for the controller as the "_controller" parameter is missing');
      }

      return FALSE;
    }
    return $this->getControllerFromDefinition($controller, $request->getPathInfo());
  }

102 103 104 105 106 107 108 109
  /**
   * Returns a callable for the given controller.
   *
   * @param string $controller
   *   A Controller string.
   *
   * @return mixed
   *   A PHP callable.
110 111 112 113 114 115
   *
   * @throws \LogicException
   *   If the controller cannot be parsed
   *
   * @throws \InvalidArgumentException
   *   If the controller class does not exist
116 117
   */
  protected function createController($controller) {
118 119 120
    // Controller in the service:method notation.
    $count = substr_count($controller, ':');
    if ($count == 1) {
121
      list($class_or_service, $method) = explode(':', $controller, 2);
122 123
    }
    // Controller in the class::method notation.
124 125
    elseif (strpos($controller, '::') !== FALSE) {
      list($class_or_service, $method) = explode('::', $controller, 2);
126 127 128
    }
    else {
      throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller));
129
    }
130

131
    $controller = $this->classResolver->getInstanceFromDefinition($class_or_service);
132

133
    return array($controller, $method);
134
  }
135

136 137 138 139
  /**
   * {@inheritdoc}
   */
  protected function doGetArguments(Request $request, $controller, array $parameters) {
140
    $attributes = $request->attributes->all();
141
    $raw_parameters = $request->attributes->has('_raw_variables') ? $request->attributes->get('_raw_variables') : [];
142 143 144 145 146
    $arguments = array();
    foreach ($parameters as $param) {
      if (array_key_exists($param->name, $attributes)) {
        $arguments[] = $attributes[$param->name];
      }
147 148 149
      elseif (array_key_exists($param->name, $raw_parameters)) {
        $arguments[] = $attributes[$param->name];
      }
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
      elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
        $arguments[] = $request;
      }
      elseif ($param->getClass() && ($param->getClass()->name == 'Drupal\Core\Routing\RouteMatchInterface' || is_subclass_of($param->getClass()->name, 'Drupal\Core\Routing\RouteMatchInterface'))) {
        $arguments[] = RouteMatch::createFromRequest($request);
      }
      elseif ($param->isDefaultValueAvailable()) {
        $arguments[] = $param->getDefaultValue();
      }
      else {
        if (is_array($controller)) {
          $repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]);
        }
        elseif (is_object($controller)) {
          $repr = get_class($controller);
        }
        else {
          $repr = $controller;
        }

        throw new \RuntimeException(sprintf('Controller "%s" requires that you provide a value for the "$%s" argument (because there is no default value or because there is a non optional argument after this one).', $repr, $param->name));
      }
    }
173 174 175
    return $arguments;
  }

176
}