Chris@0: httpMessageFactory = $http_message_factory; Chris@0: $this->classResolver = $class_resolver; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getControllerFromDefinition($controller, $path = '') { Chris@0: if (is_array($controller) || (is_object($controller) && method_exists($controller, '__invoke'))) { Chris@0: return $controller; Chris@0: } Chris@0: Chris@0: if (strpos($controller, ':') === FALSE) { Chris@0: if (function_exists($controller)) { Chris@0: return $controller; Chris@0: } Chris@18: return $this->classResolver->getInstanceFromDefinition($controller); Chris@0: } Chris@0: Chris@0: $callable = $this->createController($controller); Chris@0: Chris@0: if (!is_callable($callable)) { Chris@0: throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable.', $path)); Chris@0: } Chris@0: Chris@0: return $callable; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getController(Request $request) { Chris@0: if (!$controller = $request->attributes->get('_controller')) { Chris@0: return FALSE; Chris@0: } Chris@0: return $this->getControllerFromDefinition($controller, $request->getPathInfo()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a callable for the given controller. Chris@0: * Chris@0: * @param string $controller Chris@0: * A Controller string. Chris@0: * Chris@0: * @return mixed Chris@0: * A PHP callable. Chris@0: * Chris@0: * @throws \LogicException Chris@0: * If the controller cannot be parsed. Chris@0: * Chris@0: * @throws \InvalidArgumentException Chris@0: * If the controller class does not exist. Chris@0: */ Chris@0: protected function createController($controller) { Chris@0: // Controller in the service:method notation. Chris@0: $count = substr_count($controller, ':'); Chris@0: if ($count == 1) { Chris@0: list($class_or_service, $method) = explode(':', $controller, 2); Chris@0: } Chris@0: // Controller in the class::method notation. Chris@0: elseif (strpos($controller, '::') !== FALSE) { Chris@0: list($class_or_service, $method) = explode('::', $controller, 2); Chris@0: } Chris@0: else { Chris@0: throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller)); Chris@0: } Chris@0: Chris@0: $controller = $this->classResolver->getInstanceFromDefinition($class_or_service); Chris@0: Chris@0: return [$controller, $method]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doGetArguments(Request $request, $controller, array $parameters) { Chris@17: // Note this duplicates the deprecation message of Chris@17: // Symfony\Component\HttpKernel\Controller\ControllerResolver::getArguments() Chris@17: // to ensure it is removed in Drupal 9. Chris@17: @trigger_error(sprintf('%s is deprecated as of 8.6.0 and will be removed in 9.0. Inject the "http_kernel.controller.argument_resolver" service instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED); Chris@0: $attributes = $request->attributes->all(); Chris@0: $raw_parameters = $request->attributes->has('_raw_variables') ? $request->attributes->get('_raw_variables') : []; Chris@0: $arguments = []; Chris@0: foreach ($parameters as $param) { Chris@0: if (array_key_exists($param->name, $attributes)) { Chris@0: $arguments[] = $attributes[$param->name]; Chris@0: } Chris@0: elseif (array_key_exists($param->name, $raw_parameters)) { Chris@0: $arguments[] = $attributes[$param->name]; Chris@0: } Chris@0: elseif ($param->getClass() && $param->getClass()->isInstance($request)) { Chris@0: $arguments[] = $request; Chris@0: } Chris@0: elseif ($param->getClass() && $param->getClass()->name === ServerRequestInterface::class) { Chris@0: $arguments[] = $this->httpMessageFactory->createRequest($request); Chris@0: } Chris@0: elseif ($param->getClass() && ($param->getClass()->name == RouteMatchInterface::class || is_subclass_of($param->getClass()->name, RouteMatchInterface::class))) { Chris@0: $arguments[] = RouteMatch::createFromRequest($request); Chris@0: } Chris@0: elseif ($param->isDefaultValueAvailable()) { Chris@0: $arguments[] = $param->getDefaultValue(); Chris@0: } Chris@0: else { Chris@0: if (is_array($controller)) { Chris@0: $repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]); Chris@0: } Chris@0: elseif (is_object($controller)) { Chris@0: $repr = get_class($controller); Chris@0: } Chris@0: else { Chris@0: $repr = $controller; Chris@0: } Chris@0: Chris@0: 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)); Chris@0: } Chris@0: } Chris@0: return $arguments; Chris@0: } Chris@0: Chris@0: }