|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sebdesign\SM\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Symfony\Component\Console\Helper\TableSeparator; |
|
7
|
|
|
|
|
8
|
|
|
class Debug extends Command |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The name and signature of the console command. |
|
12
|
|
|
* |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $signature = 'winzou:state-machine:debug {graph? : A state machine graph}'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The console command description. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $description = 'Show states and transitions of state machine graphs'; |
|
23
|
|
|
|
|
24
|
|
|
protected $config; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Create a new command instance. |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $config |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(array $config) |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct(); |
|
34
|
|
|
|
|
35
|
|
|
$this->config = $config; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Execute the console command. |
|
40
|
|
|
* |
|
41
|
|
|
* @return mixed |
|
42
|
|
|
*/ |
|
43
|
|
|
public function handle() |
|
44
|
|
|
{ |
|
45
|
|
|
if (empty($this->config)) { |
|
46
|
|
|
$this->error('There are no state machines configured.'); |
|
47
|
|
|
|
|
48
|
|
|
return 1; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (! $this->argument('graph')) { |
|
52
|
|
|
$this->askForGraph(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$graph = $this->argument('graph'); |
|
56
|
|
|
|
|
57
|
|
|
if (! array_key_exists($graph, $this->config)) { |
|
58
|
|
|
$this->error('The provided state machine graph is not configured.'); |
|
59
|
|
|
|
|
60
|
|
|
return 1; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$config = $this->config[$graph]; |
|
64
|
|
|
|
|
65
|
|
|
$this->printStates($config['states']); |
|
66
|
|
|
$this->printTransitions($config['transitions']); |
|
67
|
|
|
|
|
68
|
|
|
if (isset($config['callbacks'])) { |
|
69
|
|
|
$this->printCallbacks($config['callbacks']); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return 0; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Ask for a graph name if one was not provided as argument. |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function askForGraph() |
|
79
|
|
|
{ |
|
80
|
|
|
$choices = array_map(function ($name, $config) { |
|
81
|
|
|
return $name."\t(".$config['class'].' - '.$config['graph'].')'; |
|
82
|
|
|
}, array_keys($this->config), $this->config); |
|
83
|
|
|
|
|
84
|
|
|
$choice = $this->choice('Which state machine would you like to know about?', $choices, 0); |
|
85
|
|
|
|
|
86
|
|
|
$choice = substr($choice, 0, strpos($choice, "\t")); |
|
87
|
|
|
|
|
88
|
|
|
$this->info('You have just selected: '.$choice); |
|
89
|
|
|
|
|
90
|
|
|
$this->input->setArgument('graph', $choice); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Display the graph states on a table. |
|
95
|
|
|
* |
|
96
|
|
|
* @param array $states |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function printStates(array $states) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->table(['Configured States:', 'Metadata:'], array_map(function ($state) { |
|
101
|
|
|
if (is_string($state)) { |
|
102
|
|
|
return [$state, null]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if (isset($state['metadata'])) { |
|
106
|
|
|
$metadata = implode(PHP_EOL, array_map(function ($value, $key) { |
|
107
|
|
|
$value = is_array($value) ? 'Array' : $value; |
|
108
|
|
|
if (is_bool($value)) { |
|
109
|
|
|
$value = $value ? 'true' : 'false'; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return vsprintf('%s: %s', [$key, $value]); |
|
113
|
|
|
}, $state['metadata'], array_keys($state['metadata']))); |
|
114
|
|
|
} else { |
|
115
|
|
|
$metadata = null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return [$state['name'], $metadata]; |
|
119
|
|
|
}, $states)); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Display the graph transitions on a table. |
|
124
|
|
|
* |
|
125
|
|
|
* @param array $transitions |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function printTransitions(array $transitions) |
|
128
|
|
|
{ |
|
129
|
|
|
end($transitions); |
|
130
|
|
|
|
|
131
|
|
|
$lastTransition = key($transitions); |
|
132
|
|
|
|
|
133
|
|
|
reset($transitions); |
|
134
|
|
|
|
|
135
|
|
|
$rows = []; |
|
136
|
|
|
|
|
137
|
|
|
foreach ($transitions as $name => $transition) { |
|
138
|
|
|
$rows[] = [$name, implode(PHP_EOL, $transition['from']), $transition['to']]; |
|
139
|
|
|
|
|
140
|
|
|
if ($name !== $lastTransition) { |
|
141
|
|
|
$rows[] = new TableSeparator(); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$this->table(['Transition', 'From(s)', 'To'], $rows); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Display the graph callbacks on a table. |
|
150
|
|
|
* |
|
151
|
|
|
* @param array $allCallbacks |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function printCallbacks(array $allCallbacks) |
|
154
|
|
|
{ |
|
155
|
|
|
foreach ($allCallbacks as $position => $callbacks) { |
|
156
|
|
|
$rows = []; |
|
157
|
|
|
foreach ($callbacks as $name => $specs) { |
|
158
|
|
|
$rows[] = [ |
|
159
|
|
|
$name, |
|
160
|
|
|
$this->formatSatisfies($specs), |
|
161
|
|
|
$this->formatCallable($specs), |
|
162
|
|
|
$this->formatClause($specs, 'args'), |
|
163
|
|
|
]; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$this->table([ucfirst($position).' Callbacks', 'Satisfies', 'Do', 'Args'], $rows); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Format the clauses that satisfy the callback. |
|
172
|
|
|
* |
|
173
|
|
|
* @param array $specs |
|
174
|
|
|
* @return string |
|
175
|
|
|
*/ |
|
176
|
|
|
protected function formatSatisfies(array $specs) |
|
177
|
|
|
{ |
|
178
|
|
|
$clauses = array_map(function ($clause) use ($specs) { |
|
179
|
|
|
if ($result = $this->formatClause($specs, $clause)) { |
|
180
|
|
|
return vsprintf('%s: %s', [ |
|
181
|
|
|
ucfirst(str_replace('_', ' ', $clause)), |
|
182
|
|
|
$result, |
|
183
|
|
|
]); |
|
184
|
|
|
} |
|
185
|
|
|
}, ['from', 'excluded_from', 'on', 'excluded_on', 'to', 'excluded_to']); |
|
186
|
|
|
|
|
187
|
|
|
return implode(PHP_EOL, array_filter($clauses)); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Format the callback clause. |
|
192
|
|
|
* |
|
193
|
|
|
* @param array $specs |
|
194
|
|
|
* @param string $clause |
|
195
|
|
|
* @return string |
|
196
|
|
|
*/ |
|
197
|
|
|
protected function formatClause(array $specs, $clause) |
|
198
|
|
|
{ |
|
199
|
|
|
if (isset($specs[$clause])) { |
|
200
|
|
|
return implode(', ', (array) $specs[$clause]); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return ''; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Format the callable callable. |
|
208
|
|
|
* |
|
209
|
|
|
* @param array $specs |
|
210
|
|
|
* @return string |
|
211
|
|
|
*/ |
|
212
|
|
|
protected function formatCallable(array $specs) |
|
213
|
|
|
{ |
|
214
|
|
|
if (isset($specs['can'])) { |
|
215
|
|
|
$callback = json_encode($specs['can']); |
|
216
|
|
|
|
|
217
|
|
|
return "Gate::check({$callback})"; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
if (! isset($specs['do'])) { |
|
221
|
|
|
return ''; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
$callback = $specs['do']; |
|
225
|
|
|
|
|
226
|
|
|
if ($callback instanceof \Closure) { |
|
227
|
|
|
return 'Closure'; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
if (is_string($callback)) { |
|
231
|
|
|
if (strpos($callback, '@') !== false) { |
|
232
|
|
|
$callback = explode('@', $callback); |
|
233
|
|
|
} else { |
|
234
|
|
|
return $callback.'()'; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
if (is_array($callback)) { |
|
239
|
|
|
return implode('::', $callback).'()'; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
return $callback; |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|