|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Joselfonseca\LaravelTactician; |
|
4
|
|
|
|
|
5
|
|
|
use ReflectionClass; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use League\Tactician\CommandBus; |
|
8
|
|
|
use League\Tactician\Plugins\LockingMiddleware; |
|
9
|
|
|
use League\Tactician\Handler\CommandHandlerMiddleware; |
|
10
|
|
|
use Joselfonseca\LaravelTactician\Locator\LocatorInterface; |
|
11
|
|
|
use League\Tactician\Handler\MethodNameInflector\MethodNameInflector; |
|
12
|
|
|
use League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The default Command bus Using Tactician, this is an implementation to dispatch commands to their handlers trough a middleware stack, every class is resolved from the laravel's service container. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Joselfonseca\LaravelTactician |
|
18
|
|
|
*/ |
|
19
|
|
|
class Bus implements CommandBusInterface |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var CommandBus |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $bus; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var CommandNameExtractor |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $CommandNameExtractor; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var MethodNameInflector |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $MethodNameInflector; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var LocatorInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $HandlerLocator; |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param MethodNameInflector $MethodNameInflector |
|
42
|
|
|
* @param CommandNameExtractor $CommandNameExtractor |
|
43
|
|
|
* @param LocatorInterface $HandlerLocator |
|
44
|
|
|
*/ |
|
45
|
7 |
|
public function __construct( |
|
46
|
|
|
MethodNameInflector $MethodNameInflector, |
|
47
|
|
|
CommandNameExtractor $CommandNameExtractor, |
|
48
|
|
|
LocatorInterface $HandlerLocator |
|
49
|
|
|
) { |
|
50
|
7 |
|
$this->MethodNameInflector = $MethodNameInflector; |
|
51
|
7 |
|
$this->CommandNameExtractor = $CommandNameExtractor; |
|
52
|
7 |
|
$this->HandlerLocator = $HandlerLocator; |
|
53
|
7 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Dispatch a command |
|
57
|
|
|
* |
|
58
|
|
|
* @param object $command Command to be dispatched |
|
59
|
|
|
* @param array $input Array of input to map to the command |
|
60
|
|
|
* @param array $middleware Array of middleware class name to add to the stack, they are resolved from the laravel container |
|
61
|
|
|
* @return mixed |
|
62
|
|
|
*/ |
|
63
|
7 |
|
public function dispatch($command, array $input = [], array $middleware = []) |
|
64
|
|
|
{ |
|
65
|
7 |
|
return $this->handleTheCommand($command, $input, $middleware); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Add the Command Handler |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $command Class name of the command |
|
72
|
|
|
* @param string $handler Class name of the handler to be resolved from the Laravel Container |
|
73
|
|
|
* @return mixed |
|
74
|
|
|
*/ |
|
75
|
7 |
|
public function addHandler($command, $handler) |
|
76
|
|
|
{ |
|
77
|
7 |
|
$this->HandlerLocator->addHandler($handler, $command); |
|
78
|
7 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Handle the command |
|
82
|
|
|
* |
|
83
|
|
|
* @param $command |
|
84
|
|
|
* @param $input |
|
85
|
|
|
* @param $middleware |
|
86
|
|
|
* @return mixed |
|
87
|
|
|
*/ |
|
88
|
7 |
|
protected function handleTheCommand($command, $input, array $middleware) |
|
89
|
|
|
{ |
|
90
|
7 |
|
$this->bus = new CommandBus( |
|
91
|
7 |
|
array_merge( |
|
92
|
7 |
|
[new LockingMiddleware()], |
|
93
|
7 |
|
$this->resolveMiddleware($middleware), |
|
94
|
7 |
|
[new CommandHandlerMiddleware($this->CommandNameExtractor, $this->HandlerLocator, $this->MethodNameInflector)] |
|
95
|
|
|
) |
|
96
|
|
|
); |
|
97
|
7 |
|
return $this->bus->handle($this->mapInputToCommand($command, $input)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Resolve the middleware stack from the laravel container |
|
102
|
|
|
* |
|
103
|
|
|
* @param $middleware |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
7 |
|
protected function resolveMiddleware(array $middleware) |
|
107
|
|
|
{ |
|
108
|
7 |
|
$m = []; |
|
109
|
7 |
|
foreach ($middleware as $class) { |
|
110
|
3 |
|
$m[] = app($class); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
7 |
|
return $m; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Map the input to the command |
|
118
|
|
|
* |
|
119
|
|
|
* @param $command |
|
120
|
|
|
* @param $input |
|
121
|
|
|
* @return object |
|
122
|
|
|
*/ |
|
123
|
7 |
|
protected function mapInputToCommand($command, $input) |
|
124
|
|
|
{ |
|
125
|
7 |
|
if (is_object($command)) { |
|
126
|
1 |
|
return $command; |
|
127
|
|
|
} |
|
128
|
6 |
|
$dependencies = []; |
|
129
|
6 |
|
$class = new ReflectionClass($command); |
|
130
|
6 |
|
foreach ($class->getConstructor()->getParameters() as $parameter) { |
|
131
|
6 |
|
if ( $parameter->getPosition() == 0 && $parameter->isArray() ) { |
|
132
|
2 |
|
if ($input !== []){ |
|
133
|
1 |
|
$dependencies[] = $input; |
|
134
|
|
|
} else { |
|
135
|
2 |
|
$dependencies[] = $this->getDefaultValueOrFail($parameter); |
|
136
|
|
|
} |
|
137
|
|
|
} else { |
|
138
|
4 |
|
$name = $parameter->getName(); |
|
139
|
4 |
|
if ( array_key_exists($name, $input) ){ |
|
140
|
1 |
|
$dependencies[] = $input[$name]; |
|
141
|
|
|
} else { |
|
142
|
6 |
|
$dependencies[] = $this->getDefaultValueOrFail($parameter); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
5 |
|
return $class->newInstanceArgs($dependencies); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Returns Default Value for parameter if it exists Or Fail |
|
152
|
|
|
* |
|
153
|
|
|
* @ReflectionParameter $parameter |
|
154
|
|
|
* @return mixed |
|
155
|
|
|
*/ |
|
156
|
5 |
|
private function getDefaultValueOrFail($parameter) |
|
157
|
|
|
{ |
|
158
|
5 |
|
$isDefaultValueAvailable = $parameter->isDefaultValueAvailable(); |
|
159
|
5 |
|
if ( ! $isDefaultValueAvailable ) |
|
160
|
1 |
|
throw new InvalidArgumentException("Unable to map input to command: {$parameter->getName()}"); |
|
161
|
|
|
|
|
162
|
4 |
|
return $parameter->getDefaultValue(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
|