|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Slim Framework (https://slimframework.com) |
|
4
|
|
|
* |
|
5
|
|
|
* @see https://github.com/slimphp/Slim |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Copyright (c) 2011-2017 Josh Lockhart |
|
8
|
|
|
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Slim; |
|
12
|
|
|
|
|
13
|
|
|
use RuntimeException; |
|
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
16
|
|
|
use UnexpectedValueException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Middleware |
|
20
|
|
|
* |
|
21
|
|
|
* This is an internal class that enables concentric middleware layers. This |
|
22
|
|
|
* class is an implementation detail and is used only inside of the Slim |
|
23
|
|
|
* application; it is not visible to—and should not be used by—end users |
|
24
|
|
|
*/ |
|
25
|
|
|
trait MiddlewareAwareTrait |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* middleware call stack |
|
29
|
|
|
* |
|
30
|
|
|
* @var callable |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $stack = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Middleware stack lock |
|
36
|
|
|
* |
|
37
|
|
|
* @var bool |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $middlewareLock = false; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Add middleware |
|
43
|
|
|
* |
|
44
|
|
|
* This method prepends new middleware to the application middleware stack |
|
45
|
|
|
* |
|
46
|
|
|
* @param callable $callable Any callable that accepts three arguments: |
|
47
|
|
|
* 1. A Request object |
|
48
|
|
|
* 2. A Response object |
|
49
|
|
|
* 3. A "next" middleware callable |
|
50
|
|
|
* |
|
51
|
|
|
* @return static |
|
52
|
|
|
* |
|
53
|
|
|
* @throws RuntimeException If middleware is added while the stack is dequeuing |
|
54
|
|
|
* @throws UnexpectedValueException If the middleware doesn't return a Psr\Http\Message\ResponseInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function addMiddleware(callable $callable) |
|
57
|
|
|
{ |
|
58
|
|
|
if ($this->middlewareLock) { |
|
59
|
|
|
throw new RuntimeException('Middleware can’t be added once the stack is dequeuing'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (empty($this->stack)) { |
|
63
|
|
|
$this->seedMiddlewareStack(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->stack[] = $callable; |
|
67
|
|
|
|
|
68
|
|
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Seed middleware stack with first callable |
|
73
|
|
|
* |
|
74
|
|
|
* @param callable $kernel The last item to run as middleware |
|
75
|
|
|
* |
|
76
|
|
|
* @throws RuntimeException if the stack is seeded more than once |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function seedMiddlewareStack(callable $kernel = null) |
|
79
|
|
|
{ |
|
80
|
|
|
if (!empty($this->stack)) { |
|
81
|
|
|
throw new RuntimeException('MiddlewareStack can only be seeded once.'); |
|
82
|
|
|
} |
|
83
|
|
|
if (null === $kernel) { |
|
84
|
|
|
$kernel = $this; |
|
85
|
|
|
} |
|
86
|
|
|
$this->stack[] = $kernel; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
protected function prepareStack() |
|
90
|
|
|
{ |
|
91
|
|
|
$handler = array_shift($this->stack); |
|
92
|
|
|
if (!empty($this->stack)) { |
|
93
|
|
|
if (isset($this->container) && isset($this->container->get('settings')['middlewareFifo']) && $this->container->get('settings')['middlewareFifo']) { |
|
|
|
|
|
|
94
|
|
|
$this->stack = array_reverse($this->stack); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
foreach ($this->stack as $callable) { |
|
98
|
|
|
$next = $handler; |
|
99
|
|
|
$handler = function ( |
|
100
|
|
|
ServerRequestInterface $request, |
|
101
|
|
|
ResponseInterface $response |
|
102
|
|
|
) use ( |
|
103
|
|
|
$callable, |
|
104
|
|
|
$next |
|
105
|
|
|
) { |
|
106
|
|
|
$result = call_user_func($callable, $request, $response, $next); |
|
107
|
|
|
if (false === $result instanceof ResponseInterface) { |
|
108
|
|
|
throw new UnexpectedValueException( |
|
109
|
|
|
'Middleware must return instance of \Psr\Http\Message\ResponseInterface' |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $result; |
|
114
|
|
|
}; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $handler; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Call middleware stack |
|
123
|
|
|
* |
|
124
|
|
|
* @param ServerRequestInterface $request A request object |
|
125
|
|
|
* @param ResponseInterface $response A response object |
|
126
|
|
|
* |
|
127
|
|
|
* @return ResponseInterface |
|
128
|
|
|
*/ |
|
129
|
|
|
public function callMiddlewareStack(ServerRequestInterface $request, ResponseInterface $response) |
|
130
|
|
|
{ |
|
131
|
|
|
if (empty($this->stack)) { |
|
132
|
|
|
$this->seedMiddlewareStack(); |
|
133
|
|
|
} |
|
134
|
|
|
/** @var callable $stack */ |
|
135
|
|
|
$stack = $this->prepareStack(); |
|
136
|
|
|
$this->middlewareLock = true; |
|
137
|
|
|
$response = $stack($request, $response); |
|
138
|
|
|
$this->middlewareLock = false; |
|
139
|
|
|
|
|
140
|
|
|
return $response; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: