a1cff53535dc1a155941d00971cfb1bb6e99f994
[yaffs-website] / vendor / symfony / http-kernel / Controller / ControllerResolver.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Controller;
13
14 use Psr\Log\LoggerInterface;
15 use Symfony\Component\HttpFoundation\Request;
16
17 /**
18  * ControllerResolver.
19  *
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.
23  *
24  * @author Fabien Potencier <fabien@symfony.com>
25  */
26 class ControllerResolver implements ControllerResolverInterface
27 {
28     private $logger;
29
30     /**
31      * If the ...$arg functionality is available.
32      *
33      * Requires at least PHP 5.6.0 or HHVM 3.9.1
34      *
35      * @var bool
36      */
37     private $supportsVariadic;
38
39     /**
40      * If scalar types exists.
41      *
42      * @var bool
43      */
44     private $supportsScalarTypes;
45
46     /**
47      * Constructor.
48      *
49      * @param LoggerInterface $logger A LoggerInterface instance
50      */
51     public function __construct(LoggerInterface $logger = null)
52     {
53         $this->logger = $logger;
54
55         $this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic');
56         $this->supportsScalarTypes = method_exists('ReflectionParameter', 'getType');
57     }
58
59     /**
60      * {@inheritdoc}
61      *
62      * This method looks for a '_controller' request attribute that represents
63      * the controller name (a string like ClassName::MethodName).
64      */
65     public function getController(Request $request)
66     {
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.');
70             }
71
72             return false;
73         }
74
75         if (is_array($controller)) {
76             return $controller;
77         }
78
79         if (is_object($controller)) {
80             if (method_exists($controller, '__invoke')) {
81                 return $controller;
82             }
83
84             throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', get_class($controller), $request->getPathInfo()));
85         }
86
87         if (false === strpos($controller, ':')) {
88             if (method_exists($controller, '__invoke')) {
89                 return $this->instantiateController($controller);
90             } elseif (function_exists($controller)) {
91                 return $controller;
92             }
93         }
94
95         $callable = $this->createController($controller);
96
97         if (!is_callable($callable)) {
98             throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', $controller, $request->getPathInfo()));
99         }
100
101         return $callable;
102     }
103
104     /**
105      * {@inheritdoc}
106      */
107     public function getArguments(Request $request, $controller)
108     {
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');
114         } else {
115             $r = new \ReflectionFunction($controller);
116         }
117
118         return $this->doGetArguments($request, $controller, $r->getParameters());
119     }
120
121     /**
122      * @param Request                $request
123      * @param callable               $controller
124      * @param \ReflectionParameter[] $parameters
125      *
126      * @return array The arguments to use when calling the action
127      */
128     protected function doGetArguments(Request $request, $controller, array $parameters)
129     {
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]));
136                 } else {
137                     $arguments[] = $attributes[$param->name];
138                 }
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()) {
144                 $arguments[] = null;
145             } else {
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);
150                 } else {
151                     $repr = $controller;
152                 }
153
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));
155             }
156         }
157
158         return $arguments;
159     }
160
161     /**
162      * Returns a callable for the given controller.
163      *
164      * @param string $controller A Controller string
165      *
166      * @return callable A PHP callable
167      *
168      * @throws \InvalidArgumentException
169      */
170     protected function createController($controller)
171     {
172         if (false === strpos($controller, '::')) {
173             throw new \InvalidArgumentException(sprintf('Unable to find controller "%s".', $controller));
174         }
175
176         list($class, $method) = explode('::', $controller, 2);
177
178         if (!class_exists($class)) {
179             throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
180         }
181
182         return array($this->instantiateController($class), $method);
183     }
184
185     /**
186      * Returns an instantiated controller.
187      *
188      * @param string $class A class name
189      *
190      * @return object
191      */
192     protected function instantiateController($class)
193     {
194         return new $class();
195     }
196 }