|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
MIT License |
|
4
|
|
|
Copyright (c) 2010 - 2018 Peter Petermann |
|
5
|
|
|
|
|
6
|
|
|
Permission is hereby granted, free of charge, to any person |
|
7
|
|
|
obtaining a copy of this software and associated documentation |
|
8
|
|
|
files (the "Software"), to deal in the Software without |
|
9
|
|
|
restriction, including without limitation the rights to use, |
|
10
|
|
|
copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
copies of the Software, and to permit persons to whom the |
|
12
|
|
|
Software is furnished to do so, subject to the following |
|
13
|
|
|
conditions: |
|
14
|
|
|
|
|
15
|
|
|
The above copyright notice and this permission notice shall be |
|
16
|
|
|
included in all copies or substantial portions of the Software. |
|
17
|
|
|
|
|
18
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
19
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
20
|
|
|
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
21
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
22
|
|
|
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
23
|
|
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
24
|
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
25
|
|
|
OTHER DEALINGS IN THE SOFTWARE. |
|
26
|
|
|
|
|
27
|
|
|
*/ |
|
28
|
|
|
namespace King23\Http; |
|
29
|
|
|
|
|
30
|
|
|
use King23\DI\ContainerInterface; |
|
31
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
32
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
33
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
34
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
35
|
|
|
use Psr\Log\LoggerInterface; |
|
36
|
|
|
|
|
37
|
|
|
class MiddlewareQueue implements MiddlewareQueueInterface, RequestHandlerInterface |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* @var LoggerInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $log; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string[] |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $queue = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var ContainerInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $container; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var ResponseInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
private $response; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param LoggerInterface $log |
|
61
|
|
|
* @param ContainerInterface $container |
|
62
|
|
|
* @param ResponseInterface $response |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct(LoggerInterface $log, ContainerInterface $container, ResponseInterface $response) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->log = $log; |
|
67
|
|
|
$this->container = $container; |
|
68
|
|
|
$this->response = $response; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* register a classname as part of the middleware queue |
|
73
|
|
|
* |
|
74
|
|
|
* @param $className |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
|
|
public function addMiddleware($className) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->queue[] = $className; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* execute the queue |
|
84
|
|
|
* |
|
85
|
|
|
* @param ServerRequestInterface $request |
|
86
|
|
|
* @return ResponseInterface |
|
87
|
|
|
* @throws \King23\DI\Exception\NotFoundException |
|
88
|
|
|
* @throws MiddlewareDoesNotImplementInterfaceException |
|
89
|
|
|
*/ |
|
90
|
|
|
public function handle(ServerRequestInterface $request) : ResponseInterface { |
|
91
|
|
|
return $this($request); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param ServerRequestInterface $request |
|
96
|
|
|
* @return ResponseInterface |
|
97
|
|
|
* @throws \King23\DI\Exception\NotFoundException |
|
98
|
|
|
* @throws MiddlewareDoesNotImplementInterfaceException |
|
99
|
|
|
*/ |
|
100
|
|
|
public function __invoke( |
|
101
|
|
|
ServerRequestInterface $request |
|
102
|
|
|
) { |
|
103
|
|
|
// if the queue is empty, we have reached the lowest level |
|
104
|
|
|
if(count($this->queue) == 0) { |
|
105
|
|
|
// @todo check if we need a default error response here |
|
106
|
|
|
return $this->response; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** @var MiddlewareInterface $middleware */ |
|
110
|
|
|
$middleware = $this->container->getInstanceOf(array_shift($this->queue)); |
|
111
|
|
|
|
|
112
|
|
|
if (!($middleware instanceof MiddlewareInterface)) { |
|
113
|
|
|
throw new MiddlewareDoesNotImplementInterfaceException(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $middleware->process($request, $this); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|