1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
*/ |
4
|
|
|
|
5
|
|
|
App::uses('String', 'Utility'); |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Dot Shell Class |
9
|
|
|
* |
10
|
|
|
* Simple dot generation of PNG files from state-machine |
11
|
|
|
*/ |
12
|
|
|
class DotShell extends Shell { |
|
|
|
|
13
|
|
|
|
14
|
|
|
public $Model; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* DotShell::main() |
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
public function main() { |
22
|
|
|
$this->out('Hello world.'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This function generates a png file from a given dot. A dot can be generated wit *toDot functions |
27
|
|
|
* |
28
|
|
|
* @param string $dot The contents for graphviz |
29
|
|
|
* @param string $destFile Name with full path to where file is to be created |
30
|
|
|
* @return bool|string Returns whatever shell_exec returns |
31
|
|
|
*/ |
32
|
|
|
protected function _generatePng($dot, $destFile) { |
33
|
|
|
if (!isset($dot)) { |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
$dotExec = "echo '%s' | dot -Tpng -o%s"; |
37
|
|
|
$command = sprintf($dotExec, $dot, $destFile); |
38
|
|
|
return shell_exec($command); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* DotShell::generate() |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function generate() { |
47
|
|
|
$this->out('Generate files'); |
48
|
|
|
switch ($this->args[1]) { |
49
|
|
|
case 'all': |
50
|
|
|
$this->out('Load Model:' . $this->args[0]); |
51
|
|
|
$this->loadModel($this->args[0]); |
52
|
|
|
$this->Model = new $this->args[0](1); |
53
|
|
|
|
54
|
|
|
$rolesForPopping = $this->Model->roles; |
55
|
|
|
$timesToPop = count($rolesForPopping); |
56
|
|
|
do { |
57
|
|
|
$dot = $this->Model->createDotFileForRoles($rolesForPopping, array( |
58
|
|
|
'color' => 'lightgrey', |
59
|
|
|
'activeColor' => 'green' |
60
|
|
|
)); |
61
|
|
|
$this->_generatePng($dot, TMP . implode('_', $this->Model->getAllRoles($rolesForPopping)) . '_' . $this->args[2]); |
62
|
|
|
array_pop($rolesForPopping); |
63
|
|
|
$timesToPop--; |
64
|
|
|
} while ($timesToPop > 0); |
65
|
|
|
|
66
|
|
|
foreach ($this->Model->roles as $role => $options) { |
67
|
|
|
$dot = $this->Model->createDotFileForRoles(array($role => $this->Model->roles[$role]), array( |
68
|
|
|
'color' => 'lightgrey', |
69
|
|
|
'activeColor' => 'green' |
70
|
|
|
)); |
71
|
|
|
$this->_generatePng($dot, TMP . $role . '_' . $this->args[2]); |
72
|
|
|
} |
73
|
|
|
# code... |
74
|
|
|
break; |
75
|
|
|
|
76
|
|
|
default: |
77
|
|
|
# code... |
78
|
|
|
break; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.