1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sebdesign\SM\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
|
9
|
|
|
class Visualize extends Command |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The name and signature of the console command. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $signature = 'winzou:state-machine:visualize {graph? : A state machine graph} {--output=./graph.jpg} {--format=jpg} {--direction=TB} {--shape=circle} {--dot-path=/usr/local/bin/dot}'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The console command description. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $description = 'Generates an image of the states and transitions of state machine graphs'; |
24
|
|
|
|
25
|
|
|
protected $config; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create a new command instance. |
29
|
|
|
* |
30
|
|
|
* @param array $config |
31
|
|
|
*/ |
32
|
|
|
public function __construct(array $config) |
33
|
|
|
{ |
34
|
|
|
parent::__construct(); |
35
|
|
|
|
36
|
|
|
$this->config = $config; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Execute the console command. |
41
|
|
|
* |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
|
|
public function handle() |
45
|
|
|
{ |
46
|
|
|
if (empty($this->config)) { |
47
|
|
|
$this->error('There are no state machines configured.'); |
48
|
|
|
|
49
|
|
|
return 1; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (!$this->argument('graph')) { |
53
|
|
|
$this->askForGraph(); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$graph = $this->argument('graph'); |
57
|
|
|
|
58
|
|
|
if (!array_key_exists($graph, $this->config)) { |
59
|
|
|
$this->error('The provided state machine graph is not configured.'); |
60
|
|
|
|
61
|
|
|
return 1; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$config = $this->config[$graph]; |
65
|
|
|
|
66
|
|
|
$this->stateMachineInDotFormat($config); |
67
|
|
|
|
68
|
|
|
return 0; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function stateMachineInDotFormat(array $config) |
72
|
|
|
{ |
73
|
|
|
// Output image mime types. |
74
|
|
|
$mimeTypes = [ |
75
|
|
|
'png' => 'image/png', |
76
|
|
|
'jpg' => 'image/jpeg', |
77
|
|
|
'gif' => 'image/gif', |
78
|
|
|
'svg' => 'image/svg+xml', |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
$format = $this->option('format'); |
82
|
|
|
|
83
|
|
|
if (empty($mimeTypes[$format])) { |
84
|
|
|
throw new \Exception(sprintf("Format '%s' is not supported", $format)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$dotPath = $this->option('dot-path'); |
88
|
|
|
|
89
|
|
|
// Temporary files. |
90
|
|
|
$dotFile = tempnam(sys_get_temp_dir(), 'smv'); |
91
|
|
|
$outputImage = $this->option('output'); |
92
|
|
|
|
93
|
|
|
// Display settings |
94
|
|
|
$layout = $this->option('direction'); |
95
|
|
|
$layout = $layout === 'TB' ? 'TB' : 'LR'; |
96
|
|
|
|
97
|
|
|
$nodeShape = $this->option('shape'); |
98
|
|
|
|
99
|
|
|
// Build dot file content. |
100
|
|
|
$result = []; |
101
|
|
|
$result[] = 'digraph finite_state_machine {'; |
102
|
|
|
$result[] = "rankdir=$layout;"; |
103
|
|
|
$result[] = 'node [shape = point]; _start_'; // Input node |
104
|
|
|
|
105
|
|
|
// Use first value from 'states' as start. |
106
|
|
|
$start = $config['states'][0]['name']; |
107
|
|
|
$result[] = "node [shape = $nodeShape];"; // Default nodes |
108
|
|
|
$result[] = '_start_ -> ' . $start . ';'; // Input node -> starting node. |
109
|
|
|
|
110
|
|
|
foreach ($config['transitions'] as $name => $transition) { |
111
|
|
|
foreach ($transition['from'] as $from) { |
112
|
|
|
$result[] = $from . ' -> ' . $transition['to'] . '[ label = "' . $name . '" ];'; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$result[] = '}'; |
117
|
|
|
|
118
|
|
|
$result = implode(PHP_EOL, $result); |
119
|
|
|
|
120
|
|
|
// Save dot file for input. |
121
|
|
|
file_put_contents($dotFile, $result); |
122
|
|
|
|
123
|
|
|
// Dot command. |
124
|
|
|
$cmd = sprintf( |
|
|
|
|
125
|
|
|
"%s -T%s -o %s %s", |
126
|
|
|
$dotPath, |
127
|
|
|
$format, |
128
|
|
|
$outputImage, // Output file |
129
|
|
|
$dotFile // Input file |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
$process = new Process([$dotPath, '-T' . $format, '-o', $outputImage, $dotFile]); |
134
|
|
|
$process->run(); |
135
|
|
|
|
136
|
|
|
// executes after the command finishes |
137
|
|
|
if (!$process->isSuccessful()) { |
138
|
|
|
throw new ProcessFailedException($process); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: