1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/laravel-versionable. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Overtrue\LaravelVersionable; |
12
|
|
|
|
13
|
|
|
use Illuminate\Database\Eloquent\Model; |
14
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
15
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne; |
16
|
|
|
|
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) |
129
|
|
|
{ |
130
|
|
|
if (!\property_exists($this, 'versionable')) { |
131
|
|
|
throw new \Exception('Property $versionable not exist.'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->versionable = $attributes; |
|
|
|
|
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param array $attributes |
141
|
|
|
* |
142
|
|
|
* @return $this |
143
|
|
|
* |
144
|
|
|
* @throws \Exception |
145
|
|
|
*/ |
146
|
|
|
public function setDontVersionable(array $attributes) |
147
|
|
|
{ |
148
|
|
|
if (!\property_exists($this, 'dontVersionable')) { |
149
|
|
|
throw new \Exception('Property $dontVersionable not exist.'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->dontVersionable = $attributes; |
|
|
|
|
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
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 |
169
|
|
|
{ |
170
|
|
|
return \property_exists($this, 'dontVersionable') ? $this->dontVersionable : []; |
|
|
|
|
171
|
|
|
} |
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 |
185
|
|
|
{ |
186
|
4 |
|
return config('versionable.keep_versions', 0); |
187
|
|
|
} |
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 |
197
|
|
|
{ |
198
|
4 |
|
if (count($this->getVersionable()) > 0) { |
199
|
4 |
|
return \array_intersect_key($attributes, array_flip($this->getVersionable())); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
if (count($this->getDontVersionable()) > 0) { |
203
|
|
|
return \array_diff_key($attributes, array_flip($this->getDontVersionable())); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $attributes; |
207
|
|
|
} |
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.