1 | <?php |
||
2 | /** |
||
3 | * This file is part of the O2System Framework package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @author Steeve Andrian Salim |
||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
10 | */ |
||
11 | |||
12 | // ------------------------------------------------------------------------ |
||
13 | |||
14 | namespace O2System\Framework\Http; |
||
15 | |||
16 | // ------------------------------------------------------------------------ |
||
17 | |||
18 | use O2System\Psr\Http\Message\ServerRequestInterface; |
||
19 | use O2System\Psr\Http\Server\MiddlewareInterface; |
||
20 | use O2System\Psr\Http\Server\RequestHandlerInterface; |
||
21 | use O2System\Spl\Patterns\Structural\Provider\AbstractProvider; |
||
22 | use O2System\Spl\Patterns\Structural\Provider\ValidationInterface; |
||
23 | |||
24 | /** |
||
25 | * Class Middleware |
||
26 | * |
||
27 | * @package O2System |
||
28 | */ |
||
29 | class Middleware extends AbstractProvider implements |
||
30 | ValidationInterface, |
||
31 | MiddlewareInterface |
||
32 | { |
||
33 | /** |
||
34 | * Middleware::__construct |
||
35 | */ |
||
36 | public function __construct() |
||
37 | { |
||
38 | $this->register(new Middleware\Environment(), 'environment'); |
||
39 | $this->register(new Middleware\Maintenance(), 'maintenance'); |
||
40 | $this->register(new Middleware\Csrf(), 'csrf'); |
||
41 | $this->register(new Middleware\SignOn(), 'sign-on'); |
||
42 | $this->register(new Middleware\Cache(), 'cache'); |
||
43 | } |
||
44 | |||
45 | // ------------------------------------------------------------------------ |
||
46 | |||
47 | /** |
||
48 | * Middleware::run |
||
49 | * |
||
50 | * @return void |
||
51 | */ |
||
52 | public function run() |
||
53 | { |
||
54 | if ($this->count()) { |
||
55 | |||
56 | $request = server_request(); |
||
57 | |||
58 | foreach ($this->registry as $offset => $handler) { |
||
59 | $this->process($request, $handler); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
60 | } |
||
61 | } |
||
62 | } |
||
63 | |||
64 | // ------------------------------------------------------------------------ |
||
65 | |||
66 | /** |
||
67 | * Middleware::process |
||
68 | * |
||
69 | * Process an incoming server request |
||
70 | * |
||
71 | * Processes an incoming server request in order to produce a response. |
||
72 | * If unable to produce the response itself, it may delegate to the provided |
||
73 | * request handler to do so. |
||
74 | * |
||
75 | * @param ServerRequestInterface $request |
||
76 | * @param RequestHandlerInterface $handler |
||
77 | */ |
||
78 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) |
||
79 | { |
||
80 | $handler->handle($request); |
||
81 | } |
||
82 | |||
83 | // ------------------------------------------------------------------------ |
||
84 | |||
85 | /** |
||
86 | * Middleware::validate |
||
87 | * |
||
88 | * Validate if the handler is an instanceof RequestHandlerInterface. |
||
89 | * |
||
90 | * @param mixed $handler |
||
91 | * |
||
92 | * @return bool |
||
93 | */ |
||
94 | public function validate($handler) |
||
95 | { |
||
96 | if ($handler instanceof RequestHandlerInterface) { |
||
97 | return true; |
||
98 | } |
||
99 | |||
100 | return false; |
||
101 | } |
||
102 | } |