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
|
|
|
* XML loader. accepts an xml string and loads a machine from it. |
11
|
|
|
* The xml string can contain one or more machine definitions. |
12
|
|
|
* The correct machine will be found from the xml structure. |
13
|
|
|
* |
14
|
|
|
* This class also provides a way to load xml from a file. |
15
|
|
|
* The format of the data to be loaded is specified via an xml schema definition. see getXSD |
16
|
|
|
* |
17
|
|
|
* @link https://en.wikipedia.org/wiki/XML |
18
|
|
|
* @author Rolf Vreijdenberger |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
class XML implements Loader { |
22
|
|
|
/** |
23
|
|
|
* an xml string |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $xml; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
* @param string $xml optional a valid xml string according to the schema |
31
|
|
|
*/ |
32
|
6 |
|
public function __construct($xml) |
33
|
|
|
{ |
34
|
6 |
|
$this->xml = $xml; |
35
|
6 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* creates an instance of this class with the data loaded from a file. |
39
|
|
|
* @param string $filename |
40
|
|
|
* @throws Exception |
41
|
|
|
* @return XML an instance of XML with the data from the file |
42
|
|
|
*/ |
43
|
8 |
|
public static function createFromFile($filename) |
44
|
|
|
{ |
45
|
8 |
|
if (!file_exists($filename)) { |
46
|
1 |
|
throw new Exception(sprintf('Failed to load xml from file %s. The file does not exist', $filename), Exception::BAD_LOADERDATA); |
47
|
|
|
} |
48
|
|
|
//suppress warning with @ operator, since we explicitely check the return value |
49
|
7 |
|
$xml = @file_get_contents($filename); |
50
|
7 |
|
if (false === $xml) { |
51
|
1 |
|
throw new Exception(sprintf('Failed to read xml data from file %s. Unknown error (permissions?)', $filename), Exception::BAD_LOADERDATA); |
52
|
|
|
} |
53
|
6 |
|
return new static($xml); |
54
|
|
|
} |
55
|
|
|
|
56
|
5 |
|
public function getXML() |
57
|
|
|
{ |
58
|
5 |
|
return $this->xml; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* gets the xsd used for the datastructure of the statemachine definitions |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
1 |
|
public function getXSD() |
66
|
|
|
{ |
67
|
1 |
|
$schema = '<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified"><xs:element name="machines"><xs:complexType><xs:sequence><xs:element type="xs:string" name="comment" minOccurs="0" /><xs:element name="machine" maxOccurs="unbounded" minOccurs="0"><xs:complexType><xs:sequence><xs:element name="name"><xs:annotation><xs:documentation>The machine name</xs:documentation></xs:annotation><xs:simpleType><xs:restriction base="xs:token"><xs:pattern value="([a-z0-9])+((-)?([a-z0-9])+)*" /></xs:restriction></xs:simpleType></xs:element><xs:element type="xs:string" name="factory" minOccurs="0" /><xs:element type="xs:string" name="description" /><xs:element name="states"><xs:complexType><xs:sequence><xs:element name="state" maxOccurs="unbounded" minOccurs="2"><xs:complexType><xs:sequence><xs:element name="name"><xs:simpleType><xs:restriction base="xs:token"><xs:pattern value="([a-z0-9])+((-)?([a-z0-9])+)|(not-)?regex:(.*)*" /></xs:restriction></xs:simpleType></xs:element><xs:element name="type"><xs:simpleType><xs:restriction base="xs:string"><xs:enumeration value="initial" /><xs:enumeration value="normal" /><xs:enumeration value="final" /><xs:enumeration value="regex" /></xs:restriction></xs:simpleType></xs:element><xs:element type="xs:string" name="entry_command" minOccurs="0" /><xs:element type="xs:string" name="exit_command" minOccurs="0" /><xs:element type="xs:string" name="entry_callable" minOccurs="0" /><xs:element type="xs:string" name="exit_callable" minOccurs="0" /><xs:element type="xs:string" name="description" minOccurs="0" /></xs:sequence></xs:complexType></xs:element></xs:sequence></xs:complexType></xs:element><xs:element name="transitions"><xs:complexType><xs:sequence><xs:element name="transitition" maxOccurs="unbounded" minOccurs="2"><xs:complexType><xs:sequence><xs:element name="state_from"><xs:simpleType><xs:restriction base="xs:token"><xs:pattern value="([a-z0-9])+((-)?([a-z0-9])+)*|(not-)?regex:(.*)" /></xs:restriction></xs:simpleType></xs:element><xs:element name="state_to"><xs:simpleType><xs:restriction base="xs:token"><xs:pattern value="([a-z0-9])+((-)?([a-z0-9])+)*|(not-)?regex:(.*)" /></xs:restriction></xs:simpleType></xs:element><xs:element name="event"><xs:simpleType><xs:restriction base="xs:token"><xs:pattern value="[a-zA-Z0-9]+" /></xs:restriction></xs:simpleType></xs:element><xs:element type="xs:string" name="rule" minOccurs="0" /><xs:element type="xs:string" name="command" minOccurs="0" /><xs:element type="xs:string" name="guard_callable" minOccurs="0" /><xs:element type="xs:string" name="transition_callable" minOccurs="0" /><xs:element type="xs:string" name="description" minOccurs="0" /></xs:sequence></xs:complexType></xs:element></xs:sequence></xs:complexType></xs:element></xs:sequence></xs:complexType></xs:element></xs:sequence></xs:complexType></xs:element></xs:schema>'; |
68
|
1 |
|
return $schema; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritDoc} |
73
|
|
|
*/ |
74
|
5 |
|
public function load(StateMachine $stateMachine) |
75
|
|
|
{ |
76
|
|
|
//load the xml in a php object structure. suppres warning with @ operator since we explicitely check the return value |
77
|
5 |
|
$xml = @simplexml_load_string($this->getXML()); |
78
|
5 |
|
if ($xml === false) { |
79
|
|
|
//could not load |
80
|
1 |
|
throw new Exception(sprintf('could not load xml data. check the xml format'), Exception::BAD_LOADERDATA); |
81
|
|
|
} |
82
|
4 |
|
$name = $stateMachine->getContext()->getMachine(); |
83
|
4 |
|
$found = false; |
84
|
4 |
|
$data = null; |
85
|
4 |
|
foreach ($xml->machine as $data) { |
86
|
3 |
|
if ((string) @$data->name === $name) { |
87
|
3 |
|
$found = true; |
88
|
3 |
|
break; |
89
|
|
|
} |
90
|
4 |
|
} |
91
|
4 |
|
if (!$found) { |
92
|
|
|
//no name match found |
93
|
1 |
|
throw new Exception(sprintf('no machine data found for %s in xml. seems like a wrong configuration.', $name), Exception::BAD_LOADERDATA); |
94
|
|
|
} |
95
|
|
|
//accessing xml as an object with the @ error suppresion operator ('shut the fuck up' operator) |
96
|
|
|
//allows you to get properties, even if they do not exist, without notices. |
97
|
|
|
//this let's us be a littlebit lazy since we know some nonessential properties could not be there |
98
|
3 |
|
$states = array(); |
99
|
3 |
|
foreach ($data->states->state as $state) { |
100
|
3 |
|
$tmp = new State((string) $state->name, (string) $state->type, (string) @$state->entry_command, (string) @$state->exit_command, (string) @$state->entry_callable, (string) @$state->exit_callable); |
101
|
3 |
|
$tmp->setDescription((string) @$state->description); |
102
|
3 |
|
$states [$tmp->getName()] = $tmp; |
103
|
3 |
|
} |
104
|
|
|
|
105
|
3 |
|
$transitions = array(); |
106
|
3 |
|
foreach ($data->transitions->transition as $transition) { |
107
|
3 |
|
$tmp = new Transition($states [(string) @$transition->state_from], $states [(string) @$transition->state_to], (string) @$transition->event, (string) @$transition->rule, (string) @$transition->command, (string) @$transition->guard_callable, (string) @$transition->transition_callable); |
108
|
3 |
|
$tmp->setDescription((string) @$transition->description); |
109
|
3 |
|
$transitions [] = $tmp; |
110
|
3 |
|
} |
111
|
|
|
//delegate to loader |
112
|
3 |
|
$loader = new LoaderArray($transitions); |
113
|
3 |
|
return $loader->load($stateMachine); |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
public function toString() |
117
|
|
|
{ |
118
|
2 |
|
return get_class($this); |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
public function __toString() |
122
|
|
|
{ |
123
|
1 |
|
return $this->toString(); |
124
|
|
|
} |
125
|
|
|
} |