4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Controller;
14 use Psr\Log\LoggerInterface;
15 use Symfony\Component\HttpFoundation\Request;
20 * This implementation uses the '_controller' request attribute to determine
21 * the controller to execute and uses the request attributes to determine
22 * the controller method arguments.
24 * @author Fabien Potencier <fabien@symfony.com>
26 class ControllerResolver implements ControllerResolverInterface
31 * If the ...$arg functionality is available.
33 * Requires at least PHP 5.6.0 or HHVM 3.9.1
37 private $supportsVariadic;
40 * If scalar types exists.
44 private $supportsScalarTypes;
49 * @param LoggerInterface $logger A LoggerInterface instance
51 public function __construct(LoggerInterface $logger = null)
53 $this->logger = $logger;
55 $this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic');
56 $this->supportsScalarTypes = method_exists('ReflectionParameter', 'getType');
62 * This method looks for a '_controller' request attribute that represents
63 * the controller name (a string like ClassName::MethodName).
65 public function getController(Request $request)
67 if (!$controller = $request->attributes->get('_controller')) {
68 if (null !== $this->logger) {
69 $this->logger->warning('Unable to look for the controller as the "_controller" parameter is missing.');
75 if (is_array($controller)) {
79 if (is_object($controller)) {
80 if (method_exists($controller, '__invoke')) {
84 throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', get_class($controller), $request->getPathInfo()));
87 if (false === strpos($controller, ':')) {
88 if (method_exists($controller, '__invoke')) {
89 return $this->instantiateController($controller);
90 } elseif (function_exists($controller)) {
95 $callable = $this->createController($controller);
97 if (!is_callable($callable)) {
98 throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', $controller, $request->getPathInfo()));
107 public function getArguments(Request $request, $controller)
109 if (is_array($controller)) {
110 $r = new \ReflectionMethod($controller[0], $controller[1]);
111 } elseif (is_object($controller) && !$controller instanceof \Closure) {
112 $r = new \ReflectionObject($controller);
113 $r = $r->getMethod('__invoke');
115 $r = new \ReflectionFunction($controller);
118 return $this->doGetArguments($request, $controller, $r->getParameters());
122 * @param Request $request
123 * @param callable $controller
124 * @param \ReflectionParameter[] $parameters
126 * @return array The arguments to use when calling the action
128 protected function doGetArguments(Request $request, $controller, array $parameters)
130 $attributes = $request->attributes->all();
131 $arguments = array();
132 foreach ($parameters as $param) {
133 if (array_key_exists($param->name, $attributes)) {
134 if ($this->supportsVariadic && $param->isVariadic() && is_array($attributes[$param->name])) {
135 $arguments = array_merge($arguments, array_values($attributes[$param->name]));
137 $arguments[] = $attributes[$param->name];
139 } elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
140 $arguments[] = $request;
141 } elseif ($param->isDefaultValueAvailable()) {
142 $arguments[] = $param->getDefaultValue();
143 } elseif ($this->supportsScalarTypes && $param->hasType() && $param->allowsNull()) {
146 if (is_array($controller)) {
147 $repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]);
148 } elseif (is_object($controller)) {
149 $repr = get_class($controller);
154 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));
162 * Returns a callable for the given controller.
164 * @param string $controller A Controller string
166 * @return callable A PHP callable
168 * @throws \InvalidArgumentException
170 protected function createController($controller)
172 if (false === strpos($controller, '::')) {
173 throw new \InvalidArgumentException(sprintf('Unable to find controller "%s".', $controller));
176 list($class, $method) = explode('::', $controller, 2);
178 if (!class_exists($class)) {
179 throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
182 return array($this->instantiateController($class), $method);
186 * Returns an instantiated controller.
188 * @param string $class A class name
192 protected function instantiateController($class)