Verzeichnisstruktur phpBB-3.1.0
- Veröffentlicht
- 27.10.2014
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 * @api
027 */
028 class ControllerResolver implements ControllerResolverInterface
029 {
030 private $logger;
031
032 /**
033 * Constructor.
034 *
035 * @param LoggerInterface $logger A LoggerInterface instance
036 */
037 public function __construct(LoggerInterface $logger = null)
038 {
039 $this->logger = $logger;
040 }
041
042 /**
043 * Returns the Controller instance associated with a Request.
044 *
045 * This method looks for a '_controller' request attribute that represents
046 * the controller name (a string like ClassName::MethodName).
047 *
048 * @param Request $request A Request instance
049 *
050 * @return mixed|bool A PHP callable representing the Controller,
051 * or false if this resolver is not able to determine the controller
052 *
053 * @throws \InvalidArgumentException|\LogicException If the controller can't be found
054 *
055 * @api
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 new $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 * Returns the arguments to pass to the controller.
098 *
099 * @param Request $request A Request instance
100 * @param mixed $controller A PHP callable
101 *
102 * @return array
103 *
104 * @throws \RuntimeException When value for argument given is not provided
105 *
106 * @api
107 */
108 public function getArguments(Request $request, $controller)
109 {
110 if (is_array($controller)) {
111 $r = new \ReflectionMethod($controller[0], $controller[1]);
112 } elseif (is_object($controller) && !$controller instanceof \Closure) {
113 $r = new \ReflectionObject($controller);
114 $r = $r->getMethod('__invoke');
115 } else {
116 $r = new \ReflectionFunction($controller);
117 }
118
119 return $this->doGetArguments($request, $controller, $r->getParameters());
120 }
121
122 protected function doGetArguments(Request $request, $controller, array $parameters)
123 {
124 $attributes = $request->attributes->all();
125 $arguments = array();
126 foreach ($parameters as $param) {
127 if (array_key_exists($param->name, $attributes)) {
128 $arguments[] = $attributes[$param->name];
129 } elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
130 $arguments[] = $request;
131 } elseif ($param->isDefaultValueAvailable()) {
132 $arguments[] = $param->getDefaultValue();
133 } else {
134 if (is_array($controller)) {
135 $repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]);
136 } elseif (is_object($controller)) {
137 $repr = get_class($controller);
138 } else {
139 $repr = $controller;
140 }
141
142 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));
143 }
144 }
145
146 return $arguments;
147 }
148
149 /**
150 * Returns a callable for the given controller.
151 *
152 * @param string $controller A Controller string
153 *
154 * @return mixed A PHP callable
155 *
156 * @throws \InvalidArgumentException
157 */
158 protected function createController($controller)
159 {
160 if (false === strpos($controller, '::')) {
161 throw new \InvalidArgumentException(sprintf('Unable to find controller "%s".', $controller));
162 }
163
164 list($class, $method) = explode('::', $controller, 2);
165
166 if (!class_exists($class)) {
167 throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
168 }
169
170 return array(new $class(), $method);
171 }
172 }
173