1
|
|
|
<?php |
2
|
|
|
namespace izzum\statemachine\loader; |
3
|
|
|
use izzum\statemachine\loader\Loader; |
4
|
|
|
use izzum\statemachine\StateMachine; |
5
|
|
|
use izzum\statemachine\State; |
6
|
|
|
use izzum\statemachine\Transition; |
7
|
|
|
use izzum\statemachine\Exception; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* YAML loader. accepts a yaml string and loads a machine from it. |
11
|
|
|
* The yaml string can contain one or more machine definitions. |
12
|
|
|
* The correct machine will be found from the yaml structure. |
13
|
|
|
* |
14
|
|
|
* This class provides a way to load yaml from a file on your file system (fast access). |
15
|
|
|
* |
16
|
|
|
* This class needs the php yaml module to operate, which can be found via php.net |
17
|
|
|
* |
18
|
|
|
* |
19
|
|
|
* @link https://en.wikipedia.org/wiki/YAML |
20
|
|
|
* @link https://php.net/manual/en/intro.yaml.php |
21
|
|
|
* @link http://pecl.php.net/package/yaml the needed yaml library |
22
|
|
|
* @author Rolf Vreijdenberger |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
class YAML implements Loader { |
26
|
|
|
/** |
27
|
|
|
* an undecoded yaml string |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $yaml; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* @param string $yaml optional a valid yaml string as specified in assets/yaml/example.yaml |
35
|
|
|
*/ |
36
|
5 |
|
public function __construct($yaml) |
37
|
|
|
{ |
38
|
5 |
|
$this->yaml = $yaml; |
39
|
5 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* creates an instance of this class with the data loaded from a file. |
43
|
|
|
* @param string $filename eg: __DIR__ . '/../../configuration.yaml' |
44
|
|
|
* @return YAML an instance of YAML with the data read from the file |
45
|
|
|
* @throws Exception |
46
|
|
|
*/ |
47
|
6 |
|
public static function createFromFile($filename) |
48
|
|
|
{ |
49
|
6 |
|
if (!file_exists($filename)) { |
50
|
1 |
|
throw new Exception(sprintf('Failed to load yaml from file "%s". The file does not exist', $filename), Exception::BAD_LOADERDATA); |
51
|
|
|
} |
52
|
|
|
//suppres warning with @ operator. we are explicitely testing the return value. |
53
|
5 |
|
$yaml = @file_get_contents ($filename); |
54
|
5 |
|
if (false === $yaml) { |
55
|
1 |
|
throw new Exception(sprintf('Failed to read yaml data from file "%s". Unknown error (permissions?)', $filename), Exception::BAD_LOADERDATA); |
56
|
|
|
} |
57
|
4 |
|
return new static($yaml); |
58
|
|
|
} |
59
|
5 |
|
public function getYAML() |
60
|
|
|
{ |
61
|
5 |
|
return $this->yaml; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritDoc} |
67
|
|
|
*/ |
68
|
5 |
|
public function load(StateMachine $stateMachine) |
69
|
|
|
{ |
70
|
|
|
//decode the json in a php object structure |
71
|
5 |
|
$decoded = \yaml_parse($this->getYaml(), false); |
72
|
|
|
|
73
|
|
|
//yaml decoding returns a php array. |
74
|
5 |
|
$name = $stateMachine->getContext()->getMachine(); |
75
|
5 |
|
$found = false; |
76
|
5 |
|
$data = null; |
77
|
5 |
|
if(is_array(@$decoded['machines'])) { |
78
|
3 |
|
foreach ($decoded['machines'] as $data) { |
79
|
3 |
|
if ($data['name'] === $name) { |
80
|
3 |
|
$found = true; |
81
|
3 |
|
break; |
82
|
|
|
} |
83
|
3 |
|
} |
84
|
3 |
|
} |
85
|
5 |
|
if (!$found) { |
86
|
|
|
//no name match found |
87
|
2 |
|
throw new Exception(sprintf('no machine data found for "%s" in yaml. seems like a wrong configuration.', $name), Exception::BAD_LOADERDATA); |
88
|
|
|
} |
89
|
|
|
//accessing an array with an @ error suppresion operator ('shut the fuck up' operator), |
90
|
|
|
//allows you to get properties, even if they do not exist, without notices. |
91
|
|
|
//this lets us be a little lazy in mapping the array properties to the state and transition properties |
92
|
3 |
|
$states = array(); |
93
|
3 |
|
foreach ($data['states'] as $state) { |
94
|
3 |
|
$tmp = new State($state['name'], $state['type'], @$state['entry_command'], @$state['exit_command'], @$state['entry_callable'], @$state['exit_callable']); |
95
|
3 |
|
$tmp->setDescription(@$state['description']); |
96
|
3 |
|
$states [$tmp->getName()] = $tmp; |
97
|
3 |
|
} |
98
|
|
|
|
99
|
3 |
|
$transitions = array(); |
100
|
3 |
|
foreach ($data['transitions'] as $transition) { |
101
|
3 |
|
$tmp = new Transition($states [$transition['state_from']], $states [$transition['state_to']], @$transition['event'], @$transition['rule'], @$transition['command'], @$transition['guard_callable'], @$transition['transition_callable']); |
102
|
3 |
|
$tmp->setDescription(@$transition['description']); |
103
|
3 |
|
$transitions [] = $tmp; |
104
|
3 |
|
} |
105
|
|
|
|
106
|
|
|
//delegate to loader |
107
|
3 |
|
$loader = new LoaderArray($transitions); |
108
|
3 |
|
return $loader->load($stateMachine); |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
public function toString() |
112
|
|
|
{ |
113
|
1 |
|
return get_class($this); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
public function __toString() |
117
|
|
|
{ |
118
|
1 |
|
return $this->toString(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|