|
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 Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
17
|
|
|
|
|
18
|
|
|
class SymfonyContainerMiddlewareChain implements MiddlewareChain, ContainerAwareInterface |
|
19
|
|
|
{ |
|
20
|
|
|
use ContainerAwareTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string[] |
|
24
|
|
|
*/ |
|
25
|
|
|
private $middleware_ids = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $service |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public function registerService($service) |
|
31
|
|
|
{ |
|
32
|
1 |
|
$index = array_search($service, $this->middleware_ids); |
|
33
|
|
|
|
|
34
|
|
|
// move existing middleware to end of chain |
|
35
|
1 |
|
if ($index !== false) { |
|
36
|
1 |
|
unset($this->middleware_ids[$index]); |
|
37
|
|
|
// correct array indexes |
|
38
|
1 |
|
$this->middleware_ids = array_values($this->middleware_ids); |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
$this->middleware_ids[] = $service; |
|
42
|
1 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param mixed $message |
|
46
|
|
|
* |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
*/ |
|
49
|
3 |
|
public function run($message) |
|
50
|
|
|
{ |
|
51
|
3 |
|
return call_user_func($this->call(0), $message); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param int $index |
|
56
|
|
|
* |
|
57
|
|
|
* @return callable |
|
58
|
|
|
*/ |
|
59
|
3 |
|
private function call($index) |
|
60
|
|
|
{ |
|
61
|
3 |
|
$middleware = $this->lazyLoad($index); |
|
62
|
|
|
|
|
63
|
3 |
|
if (!($middleware instanceof Middleware)) { |
|
64
|
|
|
return function ($message) { |
|
65
|
3 |
|
return $message; |
|
66
|
3 |
|
}; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
return function ($message) use ($middleware, $index) { |
|
70
|
1 |
|
return $middleware->handle($message, $this->call($index + 1)); |
|
71
|
1 |
|
}; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param $index |
|
76
|
|
|
* |
|
77
|
|
|
* @return Middleware |
|
78
|
|
|
*/ |
|
79
|
3 |
|
private function lazyLoad($index) |
|
80
|
|
|
{ |
|
81
|
3 |
|
if ($this->container instanceof ContainerInterface && isset($this->middleware_ids[$index])) { |
|
82
|
1 |
|
$middleware = $this->container->get($this->middleware_ids[$index]); |
|
83
|
|
|
|
|
84
|
1 |
|
if ($middleware instanceof Middleware) { |
|
85
|
1 |
|
return $middleware; |
|
86
|
|
|
} |
|
87
|
1 |
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
return null; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|