ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
MiddlewareAwareTrait.php
Go to the documentation of this file.
1 <?php
9 namespace Slim;
10 
15 
24 {
30  protected $tip;
31 
37  protected $middlewareLock = false;
38 
53  protected function addMiddleware(callable $callable)
54  {
55  if ($this->middlewareLock) {
56  throw new RuntimeException('Middleware can’t be added once the stack is dequeuing');
57  }
58 
59  if (is_null($this->tip)) {
60  $this->seedMiddlewareStack();
61  }
62  $next = $this->tip;
63  $this->tip = function (
66  ) use (
67  $callable,
68  $next
69  ) {
70  $result = call_user_func($callable, $request, $response, $next);
71  if ($result instanceof ResponseInterface === false) {
72  throw new UnexpectedValueException(
73  'Middleware must return instance of \Psr\Http\Message\ResponseInterface'
74  );
75  }
76 
77  return $result;
78  };
79 
80  return $this;
81  }
82 
90  protected function seedMiddlewareStack(callable $kernel = null)
91  {
92  if (!is_null($this->tip)) {
93  throw new RuntimeException('MiddlewareStack can only be seeded once.');
94  }
95  if ($kernel === null) {
96  $kernel = $this;
97  }
98  $this->tip = $kernel;
99  }
100 
109  public function callMiddlewareStack(ServerRequestInterface $request, ResponseInterface $response)
110  {
111  if (is_null($this->tip)) {
112  $this->seedMiddlewareStack();
113  }
115  $start = $this->tip;
116  $this->middlewareLock = true;
117  $response = $start($request, $response);
118  $this->middlewareLock = false;
119  return $response;
120  }
121 }
Representation of an incoming, server-side HTTP request.
$result
foreach($paths as $path) $request
Definition: asyncclient.php:32
$start
Definition: bench.php:8
addMiddleware(callable $callable)
Add middleware.
Representation of an outgoing, server-side response.
Slim Framework (https://slimframework.com)
Definition: App.php:9
seedMiddlewareStack(callable $kernel=null)
Seed middleware stack with first callable.
$response