|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Milkmeowo\Framework\Base\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Milkmeowo\Framework\Base\Models\Observers\BaseModelObserver; |
|
6
|
|
|
use Milkmeowo\Framework\Base\Repositories\Interfaces\BaseRepositoryEventsInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Whenever a new model is saved for the first time, |
|
10
|
|
|
* the creating and created events will fire. |
|
11
|
|
|
* If a model already existed in the database and the save method is called, |
|
12
|
|
|
* the updating / updated events will fire. |
|
13
|
|
|
* However, in both cases, the saving / saved events will fire. |
|
14
|
|
|
* |
|
15
|
|
|
* @CREATE: saving > creating > created > saved |
|
16
|
|
|
* @UPDATE: saving > updating > updated > saved |
|
17
|
|
|
* @DELETE: deleting > deleted |
|
18
|
|
|
* @RESTORE: restoring > restored |
|
19
|
|
|
*/ |
|
20
|
|
|
trait BaseModelEventsTrait |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var BaseRepositoryEventsInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
public static $repository; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* booted. observe the base model event with priority 99. |
|
29
|
|
|
* |
|
30
|
|
|
* @return mixed |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function bootBaseModelEventsTrait() |
|
33
|
|
|
{ |
|
34
|
|
|
// Setup event bindings |
|
35
|
|
|
$priority = 99; |
|
36
|
|
|
|
|
37
|
|
|
static::observe(new BaseModelObserver(), $priority); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* set the related RepositoryEventsInterface. |
|
42
|
|
|
* |
|
43
|
|
|
* @param BaseRepositoryEventsInterface $repository |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setRepository(BaseRepositoryEventsInterface $repository) |
|
46
|
|
|
{ |
|
47
|
|
|
self::$repository = $repository; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* get the related RepositoryEventsInterface. |
|
52
|
|
|
* |
|
53
|
|
|
* @return BaseRepositoryEventsInterface |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getRepository() |
|
56
|
|
|
{ |
|
57
|
|
|
return self::$repository; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Listen a creating model event. |
|
62
|
|
|
* |
|
63
|
|
|
* @return mixed the function result |
|
64
|
|
|
*/ |
|
65
|
|
|
public function onCreating() |
|
66
|
|
|
{ |
|
67
|
|
|
|
|
68
|
|
|
// auto set related user id |
|
69
|
|
|
if ($this->autoRelatedUserId && empty($this->{static::RELATED_USER_ID}) && $this->hasTableColumn(static::RELATED_USER_ID)) { |
|
|
|
|
|
|
70
|
|
|
$user_id = $this->getAuthUserId(); |
|
|
|
|
|
|
71
|
|
|
if ($user_id > 0) { |
|
72
|
|
|
$this->{static::RELATED_USER_ID} = $user_id; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$repository = $this->getRepository(); |
|
77
|
|
|
if ($repository instanceof BaseRepositoryEventsInterface) { |
|
78
|
|
|
$repository->onCreating(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Listen a created model event. |
|
84
|
|
|
* |
|
85
|
|
|
* @return mixed the function result |
|
86
|
|
|
*/ |
|
87
|
|
|
public function onCreated() |
|
88
|
|
|
{ |
|
89
|
|
|
$repository = $this->getRepository(); |
|
90
|
|
|
if ($repository instanceof BaseRepositoryEventsInterface) { |
|
91
|
|
|
$repository->onCreated(); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Listen a updating model event. |
|
97
|
|
|
* |
|
98
|
|
|
* @return mixed the function result |
|
99
|
|
|
*/ |
|
100
|
|
|
public function onUpdating() |
|
101
|
|
|
{ |
|
102
|
|
|
$repository = $this->getRepository(); |
|
103
|
|
|
if ($repository instanceof BaseRepositoryEventsInterface) { |
|
104
|
|
|
$repository->onUpdating(); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Listen a updated model event. |
|
110
|
|
|
* |
|
111
|
|
|
* @return mixed the function result |
|
112
|
|
|
*/ |
|
113
|
|
|
public function onUpdated() |
|
114
|
|
|
{ |
|
115
|
|
|
$repository = $this->getRepository(); |
|
116
|
|
|
if ($repository instanceof BaseRepositoryEventsInterface) { |
|
117
|
|
|
$repository->onUpdated(); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Listen a saving model event. |
|
123
|
|
|
* |
|
124
|
|
|
* @return mixed the function result |
|
125
|
|
|
*/ |
|
126
|
|
|
public function onSaving() |
|
127
|
|
|
{ |
|
128
|
|
|
|
|
129
|
|
|
// update ipstamps if true |
|
130
|
|
|
if ($this->ipstamps) { |
|
|
|
|
|
|
131
|
|
|
$this->updateIps(); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
// update userstamps if true |
|
135
|
|
|
if ($this->userstamps) { |
|
|
|
|
|
|
136
|
|
|
$this->updateUsers(); |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$repository = $this->getRepository(); |
|
140
|
|
|
if ($repository instanceof BaseRepositoryEventsInterface) { |
|
141
|
|
|
$repository->onSaving(); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Listen a saved model event. |
|
147
|
|
|
* |
|
148
|
|
|
* @return mixed the function result |
|
149
|
|
|
*/ |
|
150
|
|
|
public function onSaved() |
|
151
|
|
|
{ |
|
152
|
|
|
$repository = $this->getRepository(); |
|
153
|
|
|
if ($repository instanceof BaseRepositoryEventsInterface) { |
|
154
|
|
|
$repository->onSaved(); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Listen a deleting model event. |
|
160
|
|
|
* |
|
161
|
|
|
* @return mixed the function result |
|
162
|
|
|
*/ |
|
163
|
|
|
public function onDeleting() |
|
164
|
|
|
{ |
|
165
|
|
|
if (static::usingSoftDeletes()) { |
|
166
|
|
|
if ($this->hasTableColumn(static::DELETED_BY)) { |
|
|
|
|
|
|
167
|
|
|
$this->{static::DELETED_BY} = $this->getAuthUserId(); |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
if ($this->hasTableColumn(static::DELETED_IP)) { |
|
|
|
|
|
|
171
|
|
|
$this->{static::DELETED_IP} = smart_get_client_ip(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$this->flushEventListeners(); |
|
|
|
|
|
|
175
|
|
|
$this->save(); |
|
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$repository = $this->getRepository(); |
|
179
|
|
|
if ($repository instanceof BaseRepositoryEventsInterface) { |
|
180
|
|
|
$repository->onDeleting(); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Listen a deleted model event. |
|
186
|
|
|
* |
|
187
|
|
|
* @return mixed the function result |
|
188
|
|
|
*/ |
|
189
|
|
|
public function onDeleted() |
|
190
|
|
|
{ |
|
191
|
|
|
$repository = $this->getRepository(); |
|
192
|
|
|
if ($repository instanceof BaseRepositoryEventsInterface) { |
|
193
|
|
|
$repository->onDeleted(); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Listen a restoring model event. |
|
199
|
|
|
* |
|
200
|
|
|
* @return mixed the function result |
|
201
|
|
|
*/ |
|
202
|
|
|
public function onRestoring() |
|
203
|
|
|
{ |
|
204
|
|
|
if ($this->hasTableColumn(static::DELETED_BY)) { |
|
|
|
|
|
|
205
|
|
|
$this->{static::DELETED_BY} = null; |
|
206
|
|
|
} |
|
207
|
|
|
if ($this->hasTableColumn(static::DELETED_IP)) { |
|
|
|
|
|
|
208
|
|
|
$this->{static::DELETED_IP} = null; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
$repository = $this->getRepository(); |
|
212
|
|
|
if ($repository instanceof BaseRepositoryEventsInterface) { |
|
213
|
|
|
$repository->onRestoring(); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Listen a restored model event. |
|
219
|
|
|
* |
|
220
|
|
|
* @return mixed the function result |
|
221
|
|
|
*/ |
|
222
|
|
|
public function onRestored() |
|
223
|
|
|
{ |
|
224
|
|
|
$repository = $this->getRepository(); |
|
225
|
|
|
if ($repository instanceof BaseRepositoryEventsInterface) { |
|
226
|
|
|
$repository->onRestored(); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Has the model loaded the SoftDeletes trait. |
|
232
|
|
|
* |
|
233
|
|
|
* @return bool |
|
234
|
|
|
*/ |
|
235
|
|
|
public static function usingSoftDeletes() |
|
236
|
|
|
{ |
|
237
|
|
|
static $usingSoftDeletes; |
|
238
|
|
|
|
|
239
|
|
|
if (is_null($usingSoftDeletes)) { |
|
240
|
|
|
return $usingSoftDeletes = in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses(get_called_class())); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
return $usingSoftDeletes; |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
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: