1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Codefocus\ManagedCache; |
4
|
|
|
|
5
|
|
|
use Codefocus\ManagedCache\Events\Event; |
6
|
|
|
use Codefocus\ManagedCache\Traits\IdentifiesEloquentModels; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Iterator; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* ConditionBuilder. |
12
|
|
|
*/ |
13
|
|
|
class ConditionBuilder implements Iterator |
14
|
|
|
{ |
15
|
|
|
use IdentifiesEloquentModels; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $conditions = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Adds a Condition that tags a cache to get invalidated |
24
|
|
|
* when a new Model of the specified class is created. |
25
|
|
|
* |
26
|
|
|
* @param string $modelClassName model class name |
27
|
|
|
* |
28
|
|
|
* @return self |
29
|
|
|
*/ |
30
|
|
|
public function modelCreated(string $modelClassName): self |
31
|
|
|
{ |
32
|
|
|
return $this->addEloquentEventCondition( |
33
|
|
|
Event::EVENT_ELOQUENT_CREATED, |
34
|
|
|
$modelClassName |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Adds a Condition that tags a cache to get invalidated |
40
|
|
|
* when the specified Model instance, or any Model of the specified class |
41
|
|
|
* is updated. |
42
|
|
|
* |
43
|
|
|
* @param mixed $model model instance or class name |
44
|
|
|
* @param int|null $modelId (default: null) The Model id |
45
|
|
|
* |
46
|
|
|
* @return self |
47
|
|
|
*/ |
48
|
|
|
public function modelUpdated($model, ?int $modelId = null): self |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
list($modelClassName, $modelId) = $this->getModelClassNameAndId($model); |
51
|
|
|
|
52
|
|
|
return $this->addEloquentEventCondition( |
53
|
|
|
Event::EVENT_ELOQUENT_UPDATED, |
54
|
|
|
$modelClassName, |
55
|
|
|
$modelId |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Adds a Condition that tags a cache to get invalidated |
61
|
|
|
* when the specified Model instance, or any Model of the specified class |
62
|
|
|
* is saved. |
63
|
|
|
* |
64
|
|
|
* @param mixed $model model instance or class name |
65
|
|
|
* @param int|null $modelId (default: null) The Model id |
66
|
|
|
* |
67
|
|
|
* @return self |
68
|
|
|
*/ |
69
|
|
|
public function modelSaved($model, ?int $modelId = null): self |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
list($modelClassName, $modelId) = $this->getModelClassNameAndId($model); |
72
|
|
|
|
73
|
|
|
return $this->addEloquentEventCondition( |
74
|
|
|
Event::EVENT_ELOQUENT_SAVED, |
75
|
|
|
$modelClassName, |
76
|
|
|
$modelId |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Adds a Condition that tags a cache to get invalidated |
82
|
|
|
* when the specified Model instance, or any Model of the specified class |
83
|
|
|
* is deleted. |
84
|
|
|
* |
85
|
|
|
* @param mixed $model model instance or class name |
86
|
|
|
* @param int|null $modelId (default: null) The Model id |
87
|
|
|
* |
88
|
|
|
* @return self |
89
|
|
|
*/ |
90
|
|
|
public function modelDeleted($model, ?int $modelId = null): self |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
list($modelClassName, $modelId) = $this->getModelClassNameAndId($model); |
93
|
|
|
|
94
|
|
|
return $this->addEloquentEventCondition( |
95
|
|
|
Event::EVENT_ELOQUENT_DELETED, |
96
|
|
|
$modelClassName, |
97
|
|
|
$modelId |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Adds a Condition that tags a cache to get invalidated |
103
|
|
|
* when the specified Model instance, or any Model of the specified class |
104
|
|
|
* is restored. |
105
|
|
|
* |
106
|
|
|
* @param mixed $model model instance or class name |
107
|
|
|
* @param int|null $modelId (default: null) The Model id |
108
|
|
|
* |
109
|
|
|
* @return self |
110
|
|
|
*/ |
111
|
|
|
public function modelRestored($model, ?int $modelId = null): self |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
list($modelClassName, $modelId) = $this->getModelClassNameAndId($model); |
114
|
|
|
|
115
|
|
|
return $this->addEloquentEventCondition( |
116
|
|
|
Event::EVENT_ELOQUENT_RESTORED, |
117
|
|
|
$modelClassName, |
118
|
|
|
$modelId |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Adds a Condition that tags a cache to get invalidated when |
124
|
|
|
* a related Model of the specified class is attached. |
125
|
|
|
* |
126
|
|
|
* @param mixed $model model instance or class name |
127
|
|
|
* @param int|null $modelId (default: null) the Model id, if $model is a class name |
128
|
|
|
* @param mixed $relatedModel the related Model instance or class name |
129
|
|
|
* @param int|null $relatedModelId (default: null) the related Model id |
130
|
|
|
* |
131
|
|
|
* @return self |
132
|
|
|
*/ |
133
|
|
|
public function relatedModelAttached( |
134
|
|
|
$model, |
135
|
|
|
?int $modelId = null, |
|
|
|
|
136
|
|
|
$relatedModel, |
137
|
|
|
?int $relatedModelId = null |
|
|
|
|
138
|
|
|
): self { |
139
|
|
|
list($modelClassName, $modelId) = $this->getModelClassNameAndId($model); |
140
|
|
|
list($relatedModelClassName, $relatedModelId) = $this->getModelClassNameAndId($relatedModel); |
141
|
|
|
$this->conditions[] = new Condition( |
142
|
|
|
Event::EVENT_ELOQUENT_ATTACHED, |
143
|
|
|
$modelClassName, |
144
|
|
|
$modelId, |
145
|
|
|
$relatedModelClassName, |
146
|
|
|
$relatedModelId |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Adds a Condition that tags a cache to get invalidated when |
154
|
|
|
* a related Model of the specified class is detached. |
155
|
|
|
* |
156
|
|
|
* @param mixed $model model instance or class name |
157
|
|
|
* @param int|null $modelId (default: null) the Model id, if $model is a class name |
158
|
|
|
* @param mixed $relatedModel the related Model instance or class name |
159
|
|
|
* @param int|null $relatedModelId (default: null) the related Model id |
160
|
|
|
* |
161
|
|
|
* @return self |
162
|
|
|
*/ |
163
|
|
|
public function relatedModelDetached( |
164
|
|
|
$model, |
165
|
|
|
?int $modelId = null, |
|
|
|
|
166
|
|
|
$relatedModel, |
167
|
|
|
?int $relatedModelId = null |
|
|
|
|
168
|
|
|
): self { |
169
|
|
|
list($modelClassName, $modelId) = $this->getModelClassNameAndId($model); |
170
|
|
|
list($relatedModelClassName, $relatedModelId) = $this->getModelClassNameAndId($relatedModel); |
171
|
|
|
$this->conditions[] = new Condition( |
172
|
|
|
Event::EVENT_ELOQUENT_DETACHED, |
173
|
|
|
$modelClassName, |
174
|
|
|
$modelId, |
175
|
|
|
$relatedModelClassName, |
176
|
|
|
$relatedModelId |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Adds a Condition that tags a cache to get invalidated when |
184
|
|
|
* a related Model of the specified class is updated. |
185
|
|
|
* |
186
|
|
|
* @param mixed $model model instance or class name |
187
|
|
|
* @param int|null $modelId (default: null) the Model id, if $model is a class name |
188
|
|
|
* @param mixed $relatedModel (default: null) the related Model instance or class name |
189
|
|
|
* @param int|null $relatedModelId (default: null) the related Model id |
190
|
|
|
* |
191
|
|
|
* @return self |
192
|
|
|
*/ |
193
|
|
|
public function relatedModelUpdated( |
194
|
|
|
$model, |
195
|
|
|
?int $modelId = null, |
|
|
|
|
196
|
|
|
$relatedModel, |
197
|
|
|
?int $relatedModelId = null |
|
|
|
|
198
|
|
|
): self { |
199
|
|
|
list($modelClassName, $modelId) = $this->getModelClassNameAndId($model); |
200
|
|
|
list($relatedModelClassName, $relatedModelId) = $this->getModelClassNameAndId($relatedModel); |
201
|
|
|
$this->conditions[] = new Condition( |
202
|
|
|
Event::EVENT_ELOQUENT_UPDATED, |
203
|
|
|
$modelClassName, |
204
|
|
|
$modelId, |
205
|
|
|
$relatedModelClassName, |
206
|
|
|
$relatedModelId |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Add an Eloquent event Condition. |
214
|
|
|
* |
215
|
|
|
* @param string $eventName |
216
|
|
|
* @param string|null $modelClassName (default: null) |
217
|
|
|
* @param int|null $modelId (default: null) |
218
|
|
|
* @param string|null $relatedModelClassName (default: null) |
219
|
|
|
* @param int|null $relatedModelId (default: null) |
220
|
|
|
* |
221
|
|
|
* @return self |
222
|
|
|
*/ |
223
|
|
|
private function addEloquentEventCondition( |
224
|
|
|
string $eventName, |
225
|
|
|
string $modelClassName, |
226
|
|
|
?int $modelId = null, |
227
|
|
|
?string $relatedModelClassName = null, |
228
|
|
|
?int $relatedModelId = null |
229
|
|
|
): self { |
230
|
|
|
$this->conditions[] = new Condition( |
231
|
|
|
$eventName, |
232
|
|
|
$modelClassName, |
233
|
|
|
$modelId, |
234
|
|
|
$relatedModelClassName, |
235
|
|
|
$relatedModelId |
236
|
|
|
); |
237
|
|
|
|
238
|
|
|
return $this; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Return the value of the current item in the conditions array. |
243
|
|
|
* |
244
|
|
|
* @return Condition|false |
245
|
|
|
*/ |
246
|
|
|
public function current() |
247
|
|
|
{ |
248
|
|
|
return current($this->conditions); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Return the key of the current item in the conditions array. |
253
|
|
|
* |
254
|
|
|
* @return mixed |
255
|
|
|
*/ |
256
|
|
|
public function key() |
257
|
|
|
{ |
258
|
|
|
return key($this->conditions); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Move the conditions array pointer to the next item. |
263
|
|
|
*/ |
264
|
|
|
public function next(): void |
265
|
|
|
{ |
266
|
|
|
next($this->conditions); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Move the conditions array pointer to the first item. |
271
|
|
|
*/ |
272
|
|
|
public function rewind(): void |
273
|
|
|
{ |
274
|
|
|
reset($this->conditions); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Returns whether the current item in the conditions array is valid. |
279
|
|
|
* |
280
|
|
|
* @return bool |
281
|
|
|
*/ |
282
|
|
|
public function valid(): bool |
283
|
|
|
{ |
284
|
|
|
return current($this->conditions) !== false; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.