1
|
|
|
<?php |
2
|
|
|
namespace izzum\statemachine\utils; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This serves as a very generic store/registry that is only |
6
|
|
|
* valid during one (1) single php process. |
7
|
|
|
* |
8
|
|
|
* It's main use it for it to be used so an external application |
9
|
|
|
* can set data that can be used in a Rule or Command to |
10
|
|
|
* check/execute a transition. |
11
|
|
|
* |
12
|
|
|
* This allows us to nicely encapsulate the data so we do not need to do |
13
|
|
|
* stuff like checking the $_REQUEST/$_SESSION etc in Rules/Commands during a |
14
|
|
|
* statemachine transition. |
15
|
|
|
* |
16
|
|
|
* example: |
17
|
|
|
* state A |
18
|
|
|
* state B |
19
|
|
|
* transition A->B (rule: t>7) |
20
|
|
|
* State C |
21
|
|
|
* transition A->C (rule: can always take place to 'force' a transition from A |
22
|
|
|
* to B |
23
|
|
|
* with step C in between. For example when we need to do |
24
|
|
|
* a manual transition in the context of a Controller Action) |
25
|
|
|
* We want this to be know via the HISTORY of the statemachine and |
26
|
|
|
* therefore we have a seperate transition for this |
27
|
|
|
* transition C->B (rule: can safely take place) |
28
|
|
|
* |
29
|
|
|
* Now when we use a cronjob where we loop over items the code would be like |
30
|
|
|
* this: |
31
|
|
|
* //pseudocode |
32
|
|
|
* $all = $service->getEntityIdsForState('A'); |
33
|
|
|
* for ($all as $id) do $statemachine->run($id) |
34
|
|
|
* //end pseudocode |
35
|
|
|
* |
36
|
|
|
* the problem with the code above is that there are two OUTGOING transitions |
37
|
|
|
* for state A and that transition A->B might fail because the rule |
38
|
|
|
* is not satisfied (for t=4), but then A->C would run since that is |
39
|
|
|
* essentially an empty rule. |
40
|
|
|
* |
41
|
|
|
* Note: this is only a problem when we use the 'run' method on the |
42
|
|
|
* statemachine. It is not a problem when we use the 'transition' method on the |
43
|
|
|
* sm since we provide a transition name to that method, forcing the sm to |
44
|
|
|
* execute only that transition if it can. But keep in mind that there are |
45
|
|
|
* situations where you would just want the sm to decide what it does by calling |
46
|
|
|
* 'run' multiple times in a row. |
47
|
|
|
* |
48
|
|
|
* What we need is to be able to set data that is checked in the Rule for |
49
|
|
|
* the transition from A->C. This data can be set by the client of the |
50
|
|
|
* state machine in exceptional circumstances, to PREVENT the use of empty |
51
|
|
|
* rules and to PREVENT unwanted state transitions and to PREVENT difficult |
52
|
|
|
* mechanisms for setting data in the client where the rule has to do lots |
53
|
|
|
* of work to retrieve it (eg: controller->form->database->rule) |
54
|
|
|
* |
55
|
|
|
* for exmample: \a cron job would not set data and the rule for transition A->B |
56
|
|
|
* would |
57
|
|
|
* be checked against $t > 7. A controller would set data and the Rule for |
58
|
|
|
* transition A->C would be able to run in the context of a controller |
59
|
|
|
* that sets data but NOT in the context of the cron |
60
|
|
|
* that does not set the contextual data. |
61
|
|
|
* |
62
|
|
|
* |
63
|
|
|
* example: |
64
|
|
|
* controllercode where the statemachine is used: |
65
|
|
|
* //set context as a flag variable |
66
|
|
|
* ExternalData::set(Data::CONTEXT_SOME_VALUE); |
67
|
|
|
* $sm->transition('a_to_c'); |
68
|
|
|
* |
69
|
|
|
* rule code where the context is checked: |
70
|
|
|
* protected function _applies() |
71
|
|
|
* { |
72
|
|
|
* //check external context and act upon it |
73
|
|
|
* return ExternalData::get() === Data::CONTEXT_SOME_VALUE; |
74
|
|
|
* } |
75
|
|
|
* |
76
|
|
|
* |
77
|
|
|
* @author Rolf Vreijdenberger |
78
|
|
|
* |
79
|
|
|
*/ |
80
|
|
|
class ExternalData { |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* a simple holder for any data we want, strings, domain models etc. |
84
|
|
|
* |
85
|
|
|
* @var mixed |
86
|
|
|
*/ |
87
|
|
|
private static $data; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* is there any external data set? |
91
|
|
|
* |
92
|
|
|
* @return boolean |
93
|
|
|
*/ |
94
|
1 |
|
static public function has() |
|
|
|
|
95
|
|
|
{ |
96
|
1 |
|
return self::$data !== null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* clear the external context |
101
|
|
|
*/ |
102
|
1 |
|
static public function clear() |
|
|
|
|
103
|
|
|
{ |
104
|
1 |
|
self::set(null); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* set the external data |
109
|
|
|
* |
110
|
|
|
* @param mixed $data |
111
|
|
|
*/ |
112
|
1 |
|
static public function set($data = null) |
|
|
|
|
113
|
|
|
{ |
114
|
1 |
|
self::$data = $data; |
115
|
1 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* get the external data |
119
|
|
|
* |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
1 |
|
static public function get() |
|
|
|
|
123
|
|
|
{ |
124
|
1 |
|
return self::$data; |
125
|
|
|
} |
126
|
|
|
} |