|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* GpsLab component. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Peter Gribanov <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace GpsLab\Component\Middleware\Chain; |
|
12
|
|
|
|
|
13
|
|
|
use GpsLab\Component\Middleware\Middleware; |
|
14
|
|
|
use Psr\Container\ContainerInterface; |
|
15
|
|
|
|
|
16
|
|
|
class ContainerMiddlewareChain implements MiddlewareChain |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var ContainerInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $container; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $middleware_ids = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param ContainerInterface $container |
|
30
|
|
|
*/ |
|
31
|
2 |
|
public function __construct(ContainerInterface $container) |
|
32
|
|
|
{ |
|
33
|
2 |
|
$this->container = $container; |
|
34
|
2 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $service |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function registerService($service) |
|
40
|
|
|
{ |
|
41
|
1 |
|
$index = array_search($service, $this->middleware_ids); |
|
42
|
|
|
|
|
43
|
|
|
// move existing middleware to end of chain |
|
44
|
1 |
|
if ($index !== false) { |
|
45
|
1 |
|
unset($this->middleware_ids[$index]); |
|
46
|
|
|
// correct array indexes |
|
47
|
1 |
|
$this->middleware_ids = array_values($this->middleware_ids); |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
$this->middleware_ids[] = $service; |
|
51
|
1 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param mixed $message |
|
55
|
|
|
* |
|
56
|
|
|
* @return mixed |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public function run($message) |
|
59
|
|
|
{ |
|
60
|
2 |
|
return call_user_func($this->call(0), $message); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param int $index |
|
65
|
|
|
* |
|
66
|
|
|
* @return callable |
|
67
|
|
|
*/ |
|
68
|
2 |
|
private function call($index) |
|
69
|
|
|
{ |
|
70
|
2 |
|
$middleware = $this->lazyLoad($index); |
|
71
|
|
|
|
|
72
|
2 |
|
if (!($middleware instanceof Middleware)) { |
|
73
|
|
|
return function ($message) { |
|
74
|
2 |
|
return $message; |
|
75
|
2 |
|
}; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
return function ($message) use ($middleware, $index) { |
|
79
|
1 |
|
return $middleware->handle($message, $this->call($index + 1)); |
|
80
|
1 |
|
}; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param $index |
|
85
|
|
|
* |
|
86
|
|
|
* @return Middleware |
|
87
|
|
|
*/ |
|
88
|
2 |
|
private function lazyLoad($index) |
|
89
|
|
|
{ |
|
90
|
2 |
|
if (isset($this->middleware_ids[$index])) { |
|
91
|
1 |
|
$middleware = $this->container->get($this->middleware_ids[$index]); |
|
92
|
|
|
|
|
93
|
1 |
|
if ($middleware instanceof Middleware) { |
|
94
|
1 |
|
return $middleware; |
|
95
|
|
|
} |
|
96
|
1 |
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
return null; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|