ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
MiddlewareAwareTrait.php
Go to the documentation of this file.
1<?php
9namespace Slim;
10
11use RuntimeException;
14use UnexpectedValueException;
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;
118 $this->middlewareLock = false;
119 return $response;
120 }
121}
$result
foreach($paths as $path) $request
Definition: asyncclient.php:32
An exception for terminatinating execution or to throw for unit testing.
Representation of an outgoing, server-side response.
Representation of an incoming, server-side HTTP request.
Slim Framework (https://slimframework.com)
Definition: App.php:9
seedMiddlewareStack(callable $kernel=null)
Seed middleware stack with first callable.
trait MiddlewareAwareTrait
Middleware.
addMiddleware(callable $callable)
Add middleware.
$response
$start
Definition: bench.php:8