1 | <?php |
||
17 | trait Versionable |
||
18 | { |
||
19 | // You can add these properties to you versionable model |
||
20 | //protected $versionable = []; |
||
21 | //protected $dontVersionable = ['*']; |
||
22 | |||
23 | 4 | public static function bootVersionable() |
|
24 | { |
||
25 | static::saved(function (Model $model) { |
||
26 | 4 | self::createVersionForModel($model); |
|
27 | 4 | }); |
|
28 | |||
29 | static::deleted(function (Model $model) { |
||
30 | if ($model->forceDeleting) { |
||
31 | $model->removeAllVersions(); |
||
32 | } else { |
||
33 | self::createVersionForModel($model); |
||
34 | } |
||
35 | 4 | }); |
|
36 | 4 | } |
|
37 | |||
38 | 4 | private static function createVersionForModel(Model $model): void |
|
39 | { |
||
40 | 4 | if ($model->shouldVersioning()) { |
|
41 | 4 | Version::createForModel($model); |
|
42 | 4 | $model->removeOldVersions($model->getKeepVersionsCount()); |
|
43 | } |
||
44 | 4 | } |
|
45 | |||
46 | /** |
||
47 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
48 | */ |
||
49 | 4 | public function versions(): MorphMany |
|
50 | { |
||
51 | 4 | return $this->morphMany(\config('versionable.version_model'), 'versionable')->latest('id'); |
|
|
|||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @return \Illuminate\Database\Eloquent\Relations\MorphOne |
||
56 | */ |
||
57 | 2 | public function lastVersion(): MorphOne |
|
58 | { |
||
59 | 2 | return $this->morphOne(\config('versionable.version_model'), 'versionable')->latest('id'); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param int $id |
||
64 | * |
||
65 | * @return \Illuminate\Database\Eloquent\Model|null |
||
66 | */ |
||
67 | 1 | public function getVersion($id) |
|
68 | { |
||
69 | 1 | return $this->versions()->find($id); |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param int $id |
||
74 | * |
||
75 | * @return mixed |
||
76 | */ |
||
77 | 1 | public function revertToVersion($id) |
|
78 | { |
||
79 | 1 | return $this->versions()->findOrFail($id)->revert(); |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param int $keep |
||
84 | */ |
||
85 | 4 | public function removeOldVersions(int $keep): void |
|
86 | { |
||
87 | 4 | if ($keep <= 0) { |
|
88 | 3 | return; |
|
89 | } |
||
90 | |||
91 | 1 | $this->versions()->skip($keep)->take($keep)->get()->each->delete(); |
|
92 | 1 | } |
|
93 | |||
94 | 1 | public function removeAllVersions() |
|
95 | { |
||
96 | 1 | $this->versions->each->delete(); |
|
97 | 1 | } |
|
98 | |||
99 | /** |
||
100 | * @return bool |
||
101 | */ |
||
102 | 4 | public function shouldVersioning(): bool |
|
103 | { |
||
104 | 4 | return !empty($this->getVersionableAttributes()); |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * @return array |
||
109 | */ |
||
110 | 4 | public function getVersionableAttributes(): array |
|
111 | { |
||
112 | 4 | $changes = $this->getDirty(); |
|
113 | |||
114 | 4 | if (empty($changes)) { |
|
115 | return []; |
||
116 | } |
||
117 | |||
118 | 4 | return $this->versionableFromArray($changes); |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param array $attributes |
||
123 | * |
||
124 | * @return $this |
||
125 | * |
||
126 | * @throws \Exception |
||
127 | */ |
||
128 | public function setVersionable(array $attributes) |
||
138 | |||
139 | /** |
||
140 | * @param array $attributes |
||
141 | * |
||
142 | * @return $this |
||
143 | * |
||
144 | * @throws \Exception |
||
145 | */ |
||
146 | public function setDontVersionable(array $attributes) |
||
156 | |||
157 | /** |
||
158 | * @return array |
||
159 | */ |
||
160 | 4 | public function getVersionable(): array |
|
161 | { |
||
162 | 4 | return \property_exists($this, 'versionable') ? $this->versionable : []; |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * @return array |
||
167 | */ |
||
168 | public function getDontVersionable(): array |
||
172 | |||
173 | /** |
||
174 | * @return string |
||
175 | */ |
||
176 | 4 | public function getVersionModel(): string |
|
177 | { |
||
178 | 4 | return config('versionable.version_model'); |
|
179 | } |
||
180 | |||
181 | /** |
||
182 | * @return string |
||
183 | */ |
||
184 | 4 | public function getKeepVersionsCount(): string |
|
188 | |||
189 | /** |
||
190 | * Get the versionable attributes of a given array. |
||
191 | * |
||
192 | * @param array $attributes |
||
193 | * |
||
194 | * @return array |
||
195 | */ |
||
196 | 4 | public function versionableFromArray(array $attributes): array |
|
208 | } |
||
209 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.