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\Handler\MiddlewareHandler; |
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_handler_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 |
View Code Duplication |
public function registerService($service) |
|
|
|
|
40
|
|
|
{ |
41
|
1 |
|
$index = array_search($service, $this->middleware_handler_ids); |
42
|
|
|
|
43
|
|
|
// move existing middleware to end of chain |
44
|
1 |
|
if ($index !== false) { |
45
|
1 |
|
unset($this->middleware_handler_ids[$index]); |
46
|
|
|
// correct array indexes |
47
|
1 |
|
$this->middleware_handler_ids = array_values($this->middleware_handler_ids); |
48
|
1 |
|
} |
49
|
|
|
|
50
|
1 |
|
$this->middleware_handler_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 |
View Code Duplication |
private function call($index) |
|
|
|
|
69
|
|
|
{ |
70
|
2 |
|
$middleware = $this->lazyLoad($index); |
71
|
|
|
|
72
|
2 |
|
if (!($middleware instanceof MiddlewareHandler)) { |
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 MiddlewareHandler |
87
|
|
|
*/ |
88
|
2 |
View Code Duplication |
private function lazyLoad($index) |
|
|
|
|
89
|
|
|
{ |
90
|
2 |
|
if (isset($this->middleware_handler_ids[$index])) { |
91
|
1 |
|
$handler = $this->container->get($this->middleware_handler_ids[$index]); |
92
|
|
|
|
93
|
1 |
|
if ($handler instanceof MiddlewareHandler) { |
94
|
1 |
|
return $handler; |
95
|
|
|
} |
96
|
1 |
|
} |
97
|
|
|
|
98
|
2 |
|
return null; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.