1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\Workflow\Wrappers; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use ArrayAccess; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
use ReflectionParameter; |
9
|
|
|
use Illuminate\Contracts\Bus\Dispatcher; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Wrapper to marshal commands before dispatching them. |
13
|
|
|
* |
14
|
|
|
* @author Andrea Marco Sartori |
15
|
|
|
*/ |
16
|
|
|
class MarshalDispatcher implements DispatcherInterface |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Andrea Marco Sartori |
21
|
|
|
* @var Illuminate\Contracts\Bus\Dispatcher $dispatcher Bus dispatcher. |
22
|
|
|
*/ |
23
|
|
|
protected $dispatcher; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Andrea Marco Sartori |
27
|
|
|
* @var string $command Command to dispatch |
28
|
|
|
*/ |
29
|
|
|
protected $command; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @author Andrea Marco Sartori |
33
|
|
|
* @var \ArrayAccess $values Parameters values |
34
|
|
|
*/ |
35
|
|
|
protected $values; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set the dependencies. |
39
|
|
|
* |
40
|
|
|
* @author Andrea Marco Sartori |
41
|
|
|
* @param Illuminate\Contracts\Bus\Dispatcher $dispatcher |
42
|
|
|
* @return void |
|
|
|
|
43
|
|
|
*/ |
44
|
|
|
public function __construct(Dispatcher $dispatcher) |
45
|
|
|
{ |
46
|
|
|
$this->dispatcher = $dispatcher; |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Set the pipes commands should be piped through before dispatching. |
51
|
|
|
* |
52
|
|
|
* @author Andrea Marco Sartori |
53
|
|
|
* @param array $pipes |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
public function pipeThrough(array $pipes) |
57
|
|
|
{ |
58
|
|
|
$this->dispatcher->pipeThrough($pipes); |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Marshal a command and dispatch it. |
65
|
|
|
* |
66
|
|
|
* @author Andrea Marco Sartori |
67
|
|
|
* @param mixed $command |
68
|
|
|
* @param \ArrayAccess $source |
69
|
|
|
* @param array $extras |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function dispatchFrom($command, ArrayAccess $source, array $extras = []) |
73
|
|
|
{ |
74
|
|
|
$this->command = $command; |
75
|
|
|
|
76
|
|
|
$this->values = array_merge((array) $source, $extras); |
|
|
|
|
77
|
|
|
|
78
|
|
|
return $this->dispatcher->dispatch($this->marshal()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Marshal the command to dispatch. |
83
|
|
|
* |
84
|
|
|
* @author Andrea Marco Sartori |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
|
|
protected function marshal() |
88
|
|
|
{ |
89
|
|
|
$reflection = new ReflectionClass($this->command); |
90
|
|
|
|
91
|
|
|
$constructor = $reflection->getConstructor(); |
92
|
|
|
|
93
|
|
|
$params = $this->getParamsToInject($constructor->getParameters()); |
94
|
|
|
|
95
|
|
|
return $reflection->newInstanceArgs($params); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Retrieve the arguments to inject into the command constructor. |
100
|
|
|
* |
101
|
|
|
* @author Andrea Marco Sartori |
102
|
|
|
* @param array $parameters |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
protected function getParamsToInject(array $parameters) |
106
|
|
|
{ |
107
|
|
|
return array_map(function ($parameter) |
108
|
|
|
{ |
109
|
|
|
return $this->grabParameter($parameter); |
110
|
|
|
|
111
|
|
|
}, $parameters); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get a parameter value for a marshaled command. |
116
|
|
|
* |
117
|
|
|
* @author Andrea Marco Sartori |
118
|
|
|
* @param \ReflectionParameter $parameter |
119
|
|
|
* @return mixed |
120
|
|
|
*/ |
121
|
|
|
protected function grabParameter(ReflectionParameter $parameter) |
122
|
|
|
{ |
123
|
|
|
if (isset($this->values[$parameter->name])) |
124
|
|
|
{ |
125
|
|
|
return $this->values[$parameter->name]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if ($parameter->isDefaultValueAvailable()) |
129
|
|
|
{ |
130
|
|
|
return $parameter->getDefaultValue(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
throw new Exception("Unable to map parameter [{$parameter->name}] to command [{$this->command}]"); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.