[ Index ] |
PHP Cross Reference of phpBB-3.1.12-deutsch |
[Summary view] [Print] [Text view]
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 * Constructor. 32 * 33 * @param LoggerInterface $logger A LoggerInterface instance 34 */ 35 public function __construct(LoggerInterface $logger = null) 36 { 37 $this->logger = $logger; 38 } 39 40 /** 41 * Returns the Controller instance associated with a Request. 42 * 43 * This method looks for a '_controller' request attribute that represents 44 * the controller name (a string like ClassName::MethodName). 45 * 46 * @param Request $request A Request instance 47 * 48 * @return mixed|bool A PHP callable representing the Controller, 49 * or false if this resolver is not able to determine the controller 50 * 51 * @throws \InvalidArgumentException|\LogicException If the controller can't be found 52 */ 53 public function getController(Request $request) 54 { 55 if (!$controller = $request->attributes->get('_controller')) { 56 if (null !== $this->logger) { 57 $this->logger->warning('Unable to look for the controller as the "_controller" parameter is missing'); 58 } 59 60 return false; 61 } 62 63 if (is_array($controller)) { 64 return $controller; 65 } 66 67 if (is_object($controller)) { 68 if (method_exists($controller, '__invoke')) { 69 return $controller; 70 } 71 72 throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', get_class($controller), $request->getPathInfo())); 73 } 74 75 if (false === strpos($controller, ':')) { 76 if (method_exists($controller, '__invoke')) { 77 return new $controller(); 78 } elseif (function_exists($controller)) { 79 return $controller; 80 } 81 } 82 83 $callable = $this->createController($controller); 84 85 if (!is_callable($callable)) { 86 throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', $controller, $request->getPathInfo())); 87 } 88 89 return $callable; 90 } 91 92 /** 93 * Returns the arguments to pass to the controller. 94 * 95 * @param Request $request A Request instance 96 * @param mixed $controller A PHP callable 97 * 98 * @return array 99 * 100 * @throws \RuntimeException When value for argument given is not provided 101 */ 102 public function getArguments(Request $request, $controller) 103 { 104 if (is_array($controller)) { 105 $r = new \ReflectionMethod($controller[0], $controller[1]); 106 } elseif (is_object($controller) && !$controller instanceof \Closure) { 107 $r = new \ReflectionObject($controller); 108 $r = $r->getMethod('__invoke'); 109 } else { 110 $r = new \ReflectionFunction($controller); 111 } 112 113 return $this->doGetArguments($request, $controller, $r->getParameters()); 114 } 115 116 protected function doGetArguments(Request $request, $controller, array $parameters) 117 { 118 $attributes = $request->attributes->all(); 119 $arguments = array(); 120 foreach ($parameters as $param) { 121 if (array_key_exists($param->name, $attributes)) { 122 if (PHP_VERSION_ID >= 50600 && $param->isVariadic() && is_array($attributes[$param->name])) { 123 $arguments = array_merge($arguments, array_values($attributes[$param->name])); 124 } else { 125 $arguments[] = $attributes[$param->name]; 126 } 127 } elseif ($param->getClass() && $param->getClass()->isInstance($request)) { 128 $arguments[] = $request; 129 } elseif ($param->isDefaultValueAvailable()) { 130 $arguments[] = $param->getDefaultValue(); 131 } else { 132 if (is_array($controller)) { 133 $repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]); 134 } elseif (is_object($controller)) { 135 $repr = get_class($controller); 136 } else { 137 $repr = $controller; 138 } 139 140 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)); 141 } 142 } 143 144 return $arguments; 145 } 146 147 /** 148 * Returns a callable for the given controller. 149 * 150 * @param string $controller A Controller string 151 * 152 * @return callable A PHP callable 153 * 154 * @throws \InvalidArgumentException 155 */ 156 protected function createController($controller) 157 { 158 if (false === strpos($controller, '::')) { 159 throw new \InvalidArgumentException(sprintf('Unable to find controller "%s".', $controller)); 160 } 161 162 list($class, $method) = explode('::', $controller, 2); 163 164 if (!class_exists($class)) { 165 throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class)); 166 } 167 168 return array(new $class(), $method); 169 } 170 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 11 00:25:41 2018 | Cross-referenced by PHPXref 0.7.1 |