W3cubDocs

/Drupal 8

protected function ControllerResolver::doGetArguments

protected ControllerResolver::doGetArguments(Request $request, $controller, array $parameters)

Overrides ControllerResolver::doGetArguments

File

core/lib/Drupal/Core/Controller/ControllerResolver.php, line 131

Class

ControllerResolver
ControllerResolver to enhance controllers beyond Symfony's basic handling.

Namespace

Drupal\Core\Controller

Code

protected function doGetArguments(Request $request, $controller, array $parameters) {
  $attributes = $request->attributes->all();
  $raw_parameters = $request->attributes->has('_raw_variables') ? $request->attributes->get('_raw_variables') : [];
  $arguments = array();
  foreach ($parameters as $param) {
    if (array_key_exists($param->name, $attributes)) {
      $arguments[] = $attributes[$param->name];
    }
    elseif (array_key_exists($param->name, $raw_parameters)) {
      $arguments[] = $attributes[$param->name];
    }
    elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
      $arguments[] = $request;
    }
    elseif ($param->getClass() && $param->getClass()->name === ServerRequestInterface::class) {
      $arguments[] = $this->httpMessageFactory->createRequest($request);
    }
    elseif ($param->getClass() && ($param->getClass()->name == RouteMatchInterface::class || is_subclass_of($param->getClass()->name, RouteMatchInterface::class))) {
      $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));
    }
  }
  return $arguments;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Controller!ControllerResolver.php/function/ControllerResolver::doGetArguments/8.1.x