1
|
|
|
<?php |
2
|
|
|
namespace Mpociot\Versionable; |
3
|
|
|
|
4
|
|
|
use Exception; |
5
|
|
|
use Illuminate\Support\Facades\Auth; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class VersionableTrait |
10
|
|
|
* @package Mpociot\Versionable |
11
|
|
|
*/ |
12
|
|
|
trait VersionableTrait |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Private variable to detect if this is an update |
17
|
|
|
* or an insert |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
private $updating; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Contains all dirty data that is valid for versioning |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $versionableDirtyData; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Optional reason, why this version was created |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $reason; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Flag that determines if the model allows versioning at all |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
protected $versioningEnabled = true; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return $this |
43
|
|
|
*/ |
44
|
|
|
public function enableVersioning() |
45
|
|
|
{ |
46
|
|
|
$this->versioningEnabled = true; |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
|
|
public function disableVersioning() |
54
|
|
|
{ |
55
|
|
|
$this->versioningEnabled = false; |
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Attribute mutator for "reason" |
61
|
|
|
* Prevent "reason" to become a database attribute of model |
62
|
|
|
* |
63
|
|
|
* @param string $value |
64
|
|
|
*/ |
65
|
|
|
public function setReasonAttribute($value) |
66
|
|
|
{ |
67
|
|
|
$this->reason = $value; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Initialize model events |
72
|
|
|
*/ |
73
|
|
|
public static function bootVersionableTrait() |
74
|
|
|
{ |
75
|
|
|
static::saving(function ($model) { |
76
|
|
|
$model->versionablePreSave(); |
77
|
|
|
}); |
78
|
|
|
|
79
|
|
|
static::saved(function ($model) { |
80
|
|
|
$model->versionablePostSave(); |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return all versions of the model |
87
|
|
|
* @return MorphMany |
88
|
|
|
*/ |
89
|
|
|
public function versions() |
90
|
|
|
{ |
91
|
|
|
return $this->morphMany(Version::class, 'versionable'); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns the latest version available |
96
|
|
|
* @return Version |
97
|
|
|
*/ |
98
|
|
|
public function currentVersion() |
99
|
|
|
{ |
100
|
|
|
return $this->versions()->orderBy(Version::CREATED_AT, 'DESC')->first(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns the previous version |
105
|
|
|
* @return Version |
106
|
|
|
*/ |
107
|
|
|
public function previousVersion() |
108
|
|
|
{ |
109
|
|
|
return $this->versions()->orderBy(Version::CREATED_AT, 'DESC')->limit(1)->offset(1)->first(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get a model based on the version id |
114
|
|
|
* |
115
|
|
|
* @param $version_id |
116
|
|
|
* |
117
|
|
|
* @return $this|null |
118
|
|
|
*/ |
119
|
|
|
public function getVersionModel($version_id) |
120
|
|
|
{ |
121
|
|
|
$version = $this->versions()->where("version_id", "=", $version_id)->first(); |
122
|
|
|
if (!is_null($version)) { |
123
|
|
|
return $version->getModel(); |
124
|
|
|
} |
125
|
|
|
return null; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Pre save hook to determine if versioning is enabled and if we're updating |
130
|
|
|
* the model |
131
|
|
|
* @return void |
132
|
|
|
*/ |
133
|
|
|
protected function versionablePreSave() |
134
|
|
|
{ |
135
|
|
|
if ($this->versioningEnabled === true) { |
136
|
|
|
$this->versionableDirtyData = $this->getDirty(); |
|
|
|
|
137
|
|
|
$this->updating = $this->exists; |
|
|
|
|
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Save a new version. |
143
|
|
|
* @return void |
144
|
|
|
*/ |
145
|
|
|
protected function versionablePostSave() |
146
|
|
|
{ |
147
|
|
|
/** |
148
|
|
|
* We'll save new versions on updating and first creation |
149
|
|
|
*/ |
150
|
|
|
if ( |
151
|
|
|
( $this->versioningEnabled === true && $this->updating && $this->isValidForVersioning() ) || |
152
|
|
|
( $this->versioningEnabled === true && !$this->updating ) |
153
|
|
|
) { |
154
|
|
|
// Save a new version |
155
|
|
|
$version = new Version(); |
156
|
|
|
$version->versionable_id = $this->getKey(); |
|
|
|
|
157
|
|
|
$version->versionable_type = get_class($this); |
|
|
|
|
158
|
|
|
$version->user_id = $this->getAuthUserId(); |
|
|
|
|
159
|
|
|
$version->model_data = serialize($this->getAttributes()); |
|
|
|
|
160
|
|
|
|
161
|
|
|
if (!empty( $this->reason )) { |
162
|
|
|
$version->reason = $this->reason; |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$version->save(); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Determine if a new version should be created for this model. |
171
|
|
|
* |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
|
|
private function isValidForVersioning() |
175
|
|
|
{ |
176
|
|
|
$dontVersionFields = isset( $this->dontVersionFields ) ? $this->dontVersionFields : []; |
|
|
|
|
177
|
|
|
$removeableKeys = array_merge($dontVersionFields, [$this->getUpdatedAtColumn()]); |
|
|
|
|
178
|
|
|
|
179
|
|
|
if (method_exists($this, 'getDeletedAtColumn')) { |
180
|
|
|
$removeableKeys[] = $this->getDeletedAtColumn(); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return ( count(array_diff_key($this->versionableDirtyData, array_flip($removeableKeys))) > 0 ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return int|null |
188
|
|
|
*/ |
189
|
|
|
protected function getAuthUserId() |
190
|
|
|
{ |
191
|
|
|
try { |
192
|
|
|
if (class_exists($class = '\Cartalyst\Sentry\Facades\Laravel\Sentry') |
193
|
|
|
|| class_exists($class = '\Cartalyst\Sentinel\Laravel\Facades\Sentinel') |
194
|
|
|
) { |
195
|
|
|
return ($class::check()) ? $class::getUser()->id : null; |
196
|
|
|
} elseif (Auth::check()) { |
197
|
|
|
return Auth::id(); |
198
|
|
|
} |
199
|
|
|
} catch (Exception $e) { |
200
|
|
|
return null; |
201
|
|
|
} |
202
|
|
|
return null; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
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.