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