1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\orm\tree_traversal; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class BaseNode |
7
|
|
|
* |
8
|
|
|
* Represents a task to be done when traversing a model object tree to identify all the dependent model objects. |
9
|
|
|
* See the concrete classes for details, but the basic structure is you have a starter ModelObjNode that wraps the |
10
|
|
|
* model object whose dependent related objects you want to identify, it has a list of RelationNodes (one for each of |
11
|
|
|
* its model's relations); each of those builds a list of ModelObjNodes for each of the related model objects, |
12
|
|
|
* recursively. |
13
|
|
|
* Client code creates a ModelObjNode, provides it with a model object, and repeatedly `visit()` on it until all of its |
14
|
|
|
* related model objects are identified. |
15
|
|
|
* |
16
|
|
|
* When traversing the tree of objects, the pseudo code is this: |
17
|
|
|
* |
18
|
|
|
* Start off with a model object, and a budget of how many model objects we want to discover |
19
|
|
|
* For each of its relations: |
20
|
|
|
* count the number of related model objects |
21
|
|
|
* then fetch some of them from the DB (no more than what's in our budget) |
22
|
|
|
* record how many we fetched, and compare it to our budget |
23
|
|
|
* if we're at the budget's limit, stop. |
24
|
|
|
* otherwise, for each of them: |
25
|
|
|
* start again with it being the root model object. |
26
|
|
|
* |
27
|
|
|
* @package Event Espresso |
28
|
|
|
* @author Mike Nelson |
29
|
|
|
* @since $VID:$ |
30
|
|
|
* |
31
|
|
|
*/ |
32
|
|
|
abstract class BaseNode |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var boolean |
36
|
|
|
*/ |
37
|
|
|
protected $complete; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array of model names we don't want to traverse |
42
|
|
|
*/ |
43
|
|
|
protected $dont_traverse_models; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Whether this item has already been initialized |
47
|
|
|
*/ |
48
|
|
|
abstract protected function isDiscovered(); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Determines if the work is done yet or not. Requires you to have first discovered what work exists by calling |
52
|
|
|
* discover(). |
53
|
|
|
* @since $VID:$ |
54
|
|
|
* @return boolean |
55
|
|
|
*/ |
56
|
|
|
abstract public function isComplete(); |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Discovers what work needs to be done to complete traversing this node and its children. |
60
|
|
|
* Note that this is separate from the constructor, so we can create child nodes without |
61
|
|
|
* discovering them immediately. |
62
|
|
|
* @since $VID:$ |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
abstract protected function discover(); |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Identifies model objects, up to the limit $model_objects_to_identify. |
69
|
|
|
* @since $VID:$ |
70
|
|
|
* @param int $model_objects_to_identify |
71
|
|
|
* @return int units of work done |
72
|
|
|
*/ |
73
|
|
|
abstract protected function work($model_objects_to_identify); |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Shows the entity/relation node as an array. |
77
|
|
|
* @since $VID:$ |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
abstract public function toArray(); |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Discovers how much work there is to do, double-checks the work isn't already finished, and then does the work. |
84
|
|
|
* Note: do not call when site is in maintenance mode level 2. |
85
|
|
|
* |
86
|
|
|
* @since $VID:$ |
87
|
|
|
* @param $model_objects_to_identify |
88
|
|
|
* @return int number of model objects we want to identify during this call. On subsequent calls we'll continue |
89
|
|
|
* where we left off. |
90
|
|
|
*/ |
91
|
|
|
public function visit($model_objects_to_identify) |
92
|
|
|
{ |
93
|
|
|
if (! $this->isDiscovered()) { |
94
|
|
|
$this->discover(); |
95
|
|
|
} |
96
|
|
|
if ($this->isComplete()) { |
97
|
|
|
return 0; |
98
|
|
|
} |
99
|
|
|
return $this->work($model_objects_to_identify); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Gets the IDs of completely identified model objects that can be deleted. |
104
|
|
|
* @since $VID:$ |
105
|
|
|
* @return mixed |
106
|
|
|
*/ |
107
|
|
|
abstract public function getIds(); |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Make sure we encode whether its complete or not, but don't use such a long name. |
111
|
|
|
* @since $VID:$ |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
public function __sleep() |
115
|
|
|
{ |
116
|
|
|
$this->c = $this->complete; |
|
|
|
|
117
|
|
|
$this->dtm = $this->dont_traverse_models; |
|
|
|
|
118
|
|
|
return [ |
119
|
|
|
'c', |
120
|
|
|
'dtm' |
121
|
|
|
]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Use the dynamic property to set the "complete" property. |
126
|
|
|
* @since $VID:$ |
127
|
|
|
*/ |
128
|
|
|
public function __wakeup() |
129
|
|
|
{ |
130
|
|
|
$this->complete = $this->c; |
131
|
|
|
$this->dont_traverse_models = $this->dtm; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
// End of file BaseNode.php |
135
|
|
|
// Location: EventEspresso\core\services\orm\tree_traversal/BaseNode.php |
136
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: