ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Route.php
Go to the documentation of this file.
1 <?php
9 namespace Slim;
10 
17 
21 class Route extends Routable implements RouteInterface
22 {
24 
30  protected $methods = [];
31 
37  protected $identifier;
38 
44  protected $name;
45 
51  protected $groups;
52 
53  private $finalized = false;
54 
62  protected $outputBuffering = 'append';
63 
69  protected $arguments = [];
70 
76  protected $savedArguments = [];
77 
83  protected $callable;
84 
94  public function __construct($methods, $pattern, $callable, $groups = [], $identifier = 0)
95  {
96  $this->methods = is_string($methods) ? [$methods] : $methods;
97  $this->pattern = $pattern;
98  $this->callable = $callable;
99  $this->groups = $groups;
100  $this->identifier = 'route' . $identifier;
101  }
102 
106  public function finalize()
107  {
108  if ($this->finalized) {
109  return;
110  }
111 
112  $groupMiddleware = [];
113  foreach ($this->getGroups() as $group) {
114  $groupMiddleware = array_merge($group->getMiddleware(), $groupMiddleware);
115  }
116 
117  $this->middleware = array_merge($this->middleware, $groupMiddleware);
118 
119  foreach ($this->getMiddleware() as $middleware) {
120  $this->addMiddleware($middleware);
121  }
122 
123  $this->finalized = true;
124  }
125 
131  public function getCallable()
132  {
133  return $this->callable;
134  }
135 
141  public function setCallable($callable)
142  {
143  $this->callable = $callable;
144  }
145 
151  public function getMethods()
152  {
153  return $this->methods;
154  }
155 
161  public function getGroups()
162  {
163  return $this->groups;
164  }
165 
171  public function getName()
172  {
173  return $this->name;
174  }
175 
181  public function getIdentifier()
182  {
183  return $this->identifier;
184  }
185 
191  public function getOutputBuffering()
192  {
193  return $this->outputBuffering;
194  }
195 
207  public function setOutputBuffering($mode)
208  {
209  if (!in_array($mode, [false, 'prepend', 'append'], true)) {
210  throw new InvalidArgumentException('Unknown output buffering mode');
211  }
212  $this->outputBuffering = $mode;
213  return $this;
214  }
215 
225  public function setName($name)
226  {
227  if (!is_string($name)) {
228  throw new InvalidArgumentException('Route name must be a string');
229  }
230  $this->name = $name;
231  return $this;
232  }
233 
243  public function setArgument($name, $value, $includeInSavedArguments = true)
244  {
245  if ($includeInSavedArguments) {
246  $this->savedArguments[$name] = $value;
247  }
248  $this->arguments[$name] = $value;
249  return $this;
250  }
251 
260  public function setArguments(array $arguments, $includeInSavedArguments = true)
261  {
262  if ($includeInSavedArguments) {
263  $this->savedArguments = $arguments;
264  }
265  $this->arguments = $arguments;
266  return $this;
267  }
268 
274  public function getArguments()
275  {
276  return $this->arguments;
277  }
278 
287  public function getArgument($name, $default = null)
288  {
289  if (array_key_exists($name, $this->arguments)) {
290  return $this->arguments[$name];
291  }
292  return $default;
293  }
294 
295  /********************************************************************************
296  * Route Runner
297  *******************************************************************************/
298 
305  public function prepare(ServerRequestInterface $request, array $arguments)
306  {
307  // Remove temp arguments
308  $this->setArguments($this->savedArguments);
309 
310  // Add the route arguments
311  foreach ($arguments as $k => $v) {
312  $this->setArgument($k, $v, false);
313  }
314  }
315 
329  {
330  // Finalise route now that we are about to run it
331  $this->finalize();
332 
333  // Traverse middleware stack and fetch updated response
334  return $this->callMiddlewareStack($request, $response);
335  }
336 
350  {
351  $this->callable = $this->resolveCallable($this->callable);
352 
354  $handler = isset($this->container) ? $this->container->get('foundHandler') : new RequestResponse();
355 
356  $newResponse = $handler($this->callable, $request, $response, $this->arguments);
357 
358  if ($newResponse instanceof ResponseInterface) {
359  // if route callback returns a ResponseInterface, then use it
360  $response = $newResponse;
361  } elseif (is_string($newResponse)) {
362  // if route callback returns a string, then append it to the response
363  if ($response->getBody()->isWritable()) {
364  $response->getBody()->write($newResponse);
365  }
366  }
367 
368  return $response;
369  }
370 }
prepare(ServerRequestInterface $request, array $arguments)
Prepare the route for use.
Definition: Route.php:305
setName($name)
Set route name.
Definition: Route.php:225
setOutputBuffering($mode)
Set output buffering mode.
Definition: Route.php:207
getCallable()
Get route callable.
Definition: Route.php:131
Route.
Definition: Route.php:21
Representation of an incoming, server-side HTTP request.
trait MiddlewareAwareTrait
Middleware.
setArgument($name, $value, $includeInSavedArguments=true)
Set a route argument.
Definition: Route.php:243
__construct($methods, $pattern, $callable, $groups=[], $identifier=0)
Create new route.
Definition: Route.php:94
getArgument($name, $default=null)
Retrieve a specific route argument.
Definition: Route.php:287
foreach($paths as $path) $request
Definition: asyncclient.php:32
getMethods()
Get route methods.
Definition: Route.php:151
getName()
Get route name.
Definition: Route.php:171
getOutputBuffering()
Get output buffering mode.
Definition: Route.php:191
$identifier
Definition: Route.php:37
run(ServerRequestInterface $request, ResponseInterface $response)
Run route.
Definition: Route.php:328
setArguments(array $arguments, $includeInSavedArguments=true)
Replace route arguments.
Definition: Route.php:260
getArguments()
Retrieve route arguments.
Definition: Route.php:274
getIdentifier()
Get route identifier.
Definition: Route.php:181
addMiddleware(callable $callable)
Add middleware.
getBody()
Gets the body of the message.
finalize()
Finalize the route in preparation for dispatching.
Definition: Route.php:106
Representation of an outgoing, server-side response.
$default
Definition: build.php:20
Default route callback strategy with route parameters as an array of arguments.
Slim Framework (https://slimframework.com)
Definition: App.php:9
setCallable($callable)
This method enables you to override the Route&#39;s callable.
Definition: Route.php:141
$response
$handler
getGroups()
Get parent route groups.
Definition: Route.php:161