Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
ControllerResolver.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\HttpKernel\Controller;
013
014 use Psr\Log\LoggerInterface;
015 use Symfony\Component\HttpFoundation\Request;
016
017 /**
018 * ControllerResolver.
019 *
020 * This implementation uses the '_controller' request attribute to determine
021 * the controller to execute and uses the request attributes to determine
022 * the controller method arguments.
023 *
024 * @author Fabien Potencier <fabien@symfony.com>
025 */
026 class ControllerResolver implements ControllerResolverInterface
027 {
028 private $logger;
029
030 /**
031 * If the ...$arg functionality is available.
032 *
033 * Requires at least PHP 5.6.0 or HHVM 3.9.1
034 *
035 * @var bool
036 */
037 private $supportsVariadic;
038
039 /**
040 * Constructor.
041 *
042 * @param LoggerInterface $logger A LoggerInterface instance
043 */
044 public function __construct(LoggerInterface $logger = null)
045 {
046 $this->logger = $logger;
047
048 $this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic');
049 }
050
051 /**
052 * {@inheritdoc}
053 *
054 * This method looks for a '_controller' request attribute that represents
055 * the controller name (a string like ClassName::MethodName).
056 */
057 public function getController(Request $request)
058 {
059 if (!$controller = $request->attributes->get('_controller')) {
060 if (null !== $this->logger) {
061 $this->logger->warning('Unable to look for the controller as the "_controller" parameter is missing.');
062 }
063
064 return false;
065 }
066
067 if (is_array($controller)) {
068 return $controller;
069 }
070
071 if (is_object($controller)) {
072 if (method_exists($controller, '__invoke')) {
073 return $controller;
074 }
075
076 throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', get_class($controller), $request->getPathInfo()));
077 }
078
079 if (false === strpos($controller, ':')) {
080 if (method_exists($controller, '__invoke')) {
081 return $this->instantiateController($controller);
082 } elseif (function_exists($controller)) {
083 return $controller;
084 }
085 }
086
087 $callable = $this->createController($controller);
088
089 if (!is_callable($callable)) {
090 throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', $controller, $request->getPathInfo()));
091 }
092
093 return $callable;
094 }
095
096 /**
097 * {@inheritdoc}
098 */
099 public function getArguments(Request $request, $controller)
100 {
101 if (is_array($controller)) {
102 $r = new \ReflectionMethod($controller[0], $controller[1]);
103 } elseif (is_object($controller) && !$controller instanceof \Closure) {
104 $r = new \ReflectionObject($controller);
105 $r = $r->getMethod('__invoke');
106 } else {
107 $r = new \ReflectionFunction($controller);
108 }
109
110 return $this->doGetArguments($request, $controller, $r->getParameters());
111 }
112
113 /**
114 * @param Request $request
115 * @param callable $controller
116 * @param \ReflectionParameter[] $parameters
117 *
118 * @return array The arguments to use when calling the action
119 */
120 protected function doGetArguments(Request $request, $controller, array $parameters)
121 {
122 $attributes = $request->attributes->all();
123 $arguments = array();
124 foreach ($parameters as $param) {
125 if (array_key_exists($param->name, $attributes)) {
126 if ($this->supportsVariadic && $param->isVariadic() && is_array($attributes[$param->name])) {
127 $arguments = array_merge($arguments, array_values($attributes[$param->name]));
128 } else {
129 $arguments[] = $attributes[$param->name];
130 }
131 } elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
132 $arguments[] = $request;
133 } elseif ($param->isDefaultValueAvailable()) {
134 $arguments[] = $param->getDefaultValue();
135 } elseif ($param->allowsNull()) {
136 $arguments[] = null;
137 } else {
138 if (is_array($controller)) {
139 $repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]);
140 } elseif (is_object($controller)) {
141 $repr = get_class($controller);
142 } else {
143 $repr = $controller;
144 }
145
146 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));
147 }
148 }
149
150 return $arguments;
151 }
152
153 /**
154 * Returns a callable for the given controller.
155 *
156 * @param string $controller A Controller string
157 *
158 * @return callable A PHP callable
159 *
160 * @throws \InvalidArgumentException
161 */
162 protected function createController($controller)
163 {
164 if (false === strpos($controller, '::')) {
165 throw new \InvalidArgumentException(sprintf('Unable to find controller "%s".', $controller));
166 }
167
168 list($class, $method) = explode('::', $controller, 2);
169
170 if (!class_exists($class)) {
171 throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
172 }
173
174 return array($this->instantiateController($class), $method);
175 }
176
177 /**
178 * Returns an instantiated controller.
179 *
180 * @param string $class A class name
181 *
182 * @return object
183 */
184 protected function instantiateController($class)
185 {
186 return new $class();
187 }
188 }
189