1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
*/
namespace Slim;
use Closure;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Handlers\Strategies\RequestResponse;
use Slim\Interfaces\InvocationStrategyInterface;
use Slim\Interfaces\RouteInterface;
class Route extends Routable implements RouteInterface
{
use MiddlewareAwareTrait;
/**
* HTTP methods supported by this route
*
* @var string[]
*/
protected $methods = [];
/**
* Route identifier
*
* @var string
*/
protected $identifier;
/**
* Route name
*
* @var null|string
*/
protected $name;
/**
* Parent route groups
*
* @var RouteGroup[]
*/
protected $groups;
private $finalized = false;
/**
* Output buffering mode
*
* One of: false, 'prepend' or 'append'
*
* @var boolean|string
*/
protected $outputBuffering = 'append';
/**
* Route parameters
*
* @var array
*/
protected $arguments = [];
/**
* Route arguments parameters
*
* @var null|array
*/
protected $savedArguments = [];
/**
* @param string|string[] $methods The route HTTP methods
* @param string $pattern The route pattern
* @param callable $callable The route callable
* @param RouteGroup[] $groups The parent route groups
* @param int $identifier The route identifier
*/
public function __construct($methods, $pattern, $callable, $groups = [], $identifier = 0)
{
parent::__construct($pattern, $callable);
$this->methods = is_string($methods) ? [$methods] : $methods;
$this->groups = $groups;
$this->identifier = 'route' . $identifier;
}
public function finalize()
{
if ($this->finalized) {
return;
}
$groupMiddleware = [];
foreach ($this->getGroups() as $group) {
$groupMiddleware = array_merge($group->getMiddleware(), $groupMiddleware);
}
$this->middleware = array_merge($this->middleware, $groupMiddleware);
foreach ($this->getMiddleware() as $middleware) {
$this->addMiddleware($middleware);
}
$this->finalized = true;
}
/**
* Get route callable
*
* @return callable
*/
public function getCallable()
{
return $this->callable;
}
/**
* This method enables you to override the Route's callable
*
* @param string|Closure $callable
*/
public function setCallable($callable)
{
$this->callable = $callable;
}
/**
* Get route methods
*
* @return string[]
*/
public function getMethods()
{
return $this->methods;
}
/**
* Get parent route groups
*
* @return RouteGroup[]
*/
public function getGroups()
{
return $this->groups;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}
/**
* Get route identifier
*
* @return string
*/
public function getIdentifier()
{
return $this->identifier;
}
/**
* Get output buffering mode
*
* @return boolean|string
*/
public function getOutputBuffering()
{
return $this->outputBuffering;
}
/**
* {@inheritdoc}
*/
public function setOutputBuffering($mode)
{
if (!in_array($mode, [false, 'prepend', 'append'], true)) {
throw new InvalidArgumentException('Unknown output buffering mode');
}
$this->outputBuffering = $mode;
return $this;
}
/**
* {@inheritdoc}
*/
public function setName($name)
{
if (!is_string($name)) {
throw new InvalidArgumentException('Route name must be a string');
}
$this->name = $name;
return $this;
}
/**
* {@inheritdoc}
*/
public function setArgument($name, $value, $includeInSavedArguments = true)
{
if ($includeInSavedArguments) {
$this->savedArguments[$name] = $value;
}
$this->arguments[$name] = $value;
return $this;
}
/**
* {@inheritdoc}
*/
public function setArguments(array $arguments, $includeInSavedArguments = true)
{
if ($includeInSavedArguments) {
$this->savedArguments = $arguments;
}
$this->arguments = $arguments;
return $this;
}
/**
* {@inheritdoc}
*/
public function getArguments()
{
return $this->arguments;
}
/**
* {@inheritdoc}
*/
public function getArgument($name, $default = null)
{
if (array_key_exists($name, $this->arguments)) {
return $this->arguments[$name];
}
return $default;
}
/**
* {@inheritdoc}
*/
public function prepare(ServerRequestInterface $request, array $arguments)
{
// Remove temp arguments
$this->setArguments($this->savedArguments);
// Add the route arguments
foreach ($arguments as $k => $v) {
$this->setArgument($k, $v, false);
}
}
/**
* {@inheritdoc}
*/
public function run(ServerRequestInterface $request, ResponseInterface $response)
{
// Finalise route now that we are about to run it
$this->finalize();
// Traverse middleware stack and fetch updated response
return $this->callMiddlewareStack($request, $response);
}
/**
* {@inheritdoc}
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
{
$this->callable = $this->resolveCallable($this->callable);
/** @var InvocationStrategyInterface $handler */
$handler = isset($this->container) ? $this->container->get('foundHandler') : new RequestResponse();
$newResponse = $handler($this->callable, $request, $response, $this->arguments);
if ($newResponse instanceof ResponseInterface) {
// if route callback returns a ResponseInterface, then use it
$response = $newResponse;
} elseif (is_string($newResponse)) {
// if route callback returns a string, then append it to the response
if ($response->getBody()->isWritable()) {
$response->getBody()->write($newResponse);
}
}
return $response;
}
}
|