|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hiapi\components; |
|
4
|
|
|
|
|
5
|
|
|
use League\Tactician\Handler\CommandHandlerMiddleware; |
|
6
|
|
|
use League\Tactician\CommandBus as Worker; |
|
7
|
|
|
use Yii; |
|
8
|
|
|
|
|
9
|
|
|
class CommandBus extends \yii\base\Component |
|
10
|
|
|
{ |
|
11
|
|
|
public $locator; |
|
12
|
|
|
public $extractor; |
|
13
|
|
|
public $inflector; |
|
14
|
|
|
public $middlewares = []; |
|
15
|
|
|
|
|
16
|
|
|
protected $handler; |
|
17
|
|
|
protected $worker; |
|
18
|
|
|
|
|
19
|
|
|
public function handle($command) |
|
20
|
|
|
{ |
|
21
|
|
|
return $this->getWorker()->handle($command); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function getWorker() |
|
25
|
|
|
{ |
|
26
|
|
|
if ($this->worker === null) { |
|
27
|
|
|
$this->worker = $this->buildWorker(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
return $this->worker; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected function buildWorker() |
|
34
|
|
|
{ |
|
35
|
|
|
return new Worker($this->getMiddlewares()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getMiddlewares() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->middlewares = $this->prepareMiddlewares($this->middlewares); |
|
41
|
|
|
if (!$this->holdsHandler($this->middlewares)) { |
|
42
|
|
|
$this->middlewares[] = $this->getHandler(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this->middlewares; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function prepareMiddlewares($middlewares) |
|
49
|
|
|
{ |
|
50
|
|
|
foreach ($middlewares as &$middleware) { |
|
51
|
|
|
if (!is_object($middleware)) { |
|
52
|
|
|
$middleware = Yii::createObject($middleware); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $middlewares; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getHandler() |
|
60
|
|
|
{ |
|
61
|
|
|
if ($this->handler === null) { |
|
62
|
|
|
$this->handler = $this->buildHandler(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->handler; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function buildHandler() |
|
69
|
|
|
{ |
|
70
|
|
|
return Yii::createObject(CommandHandlerMiddleware::class, [ |
|
71
|
|
|
$this->getExtractor(), |
|
72
|
|
|
$this->getLocator(), |
|
73
|
|
|
$this->getInflector(), |
|
74
|
|
|
]); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getExtractor() |
|
78
|
|
|
{ |
|
79
|
|
|
if (!is_object($this->extractor)) { |
|
80
|
|
|
$this->extractor = Yii::createObject($this->extractor); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->extractor; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getLocator() |
|
87
|
|
|
{ |
|
88
|
|
|
if (!is_object($this->locator)) { |
|
89
|
|
|
$this->locator = Yii::createObject($this->locator); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $this->locator; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getInflector() |
|
96
|
|
|
{ |
|
97
|
|
|
if (!is_object($this->inflector)) { |
|
98
|
|
|
$this->inflector = Yii::createObject($this->inflector); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $this->inflector; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function holdsHandler($middlewares) |
|
105
|
|
|
{ |
|
106
|
|
|
foreach ($middlewares as $middleware) { |
|
107
|
|
|
if ($middleware instanceof CommandHandlerMiddleware) { |
|
108
|
|
|
return true; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return false; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|