|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System PHP 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\Reactor\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\Psr\Patterns\Structural\Provider\AbstractProvider; |
|
22
|
|
|
use O2System\Psr\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
|
|
|
public function __construct() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->register(new Middleware\Environment(), 'environment'); |
|
36
|
|
|
$this->register(new Middleware\Maintenance(), 'maintenance'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// ------------------------------------------------------------------------ |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Middleware::run |
|
43
|
|
|
* |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
|
|
public function run() |
|
47
|
|
|
{ |
|
48
|
|
|
if ($this->count()) { |
|
49
|
|
|
|
|
50
|
|
|
$request = server_request(); |
|
51
|
|
|
|
|
52
|
|
|
foreach ($this->registry as $offset => $handler) { |
|
53
|
|
|
$this->process($request, $handler); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Process an incoming server request |
|
60
|
|
|
* |
|
61
|
|
|
* Processes an incoming server request in order to produce a response. |
|
62
|
|
|
* If unable to produce the response itself, it may delegate to the provided |
|
63
|
|
|
* request handler to do so. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) |
|
66
|
|
|
{ |
|
67
|
|
|
$handler->handle($request); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// ------------------------------------------------------------------------ |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Middleware::validate |
|
74
|
|
|
* |
|
75
|
|
|
* @param mixed $handler |
|
76
|
|
|
* |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
|
|
public function validate($handler) |
|
80
|
|
|
{ |
|
81
|
|
|
if ($handler instanceof RequestHandlerInterface) { |
|
82
|
|
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
} |