|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Behaviors; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Prime\Mapper\Builder\FieldBuilder; |
|
6
|
|
|
use Bdf\Prime\Repository\RepositoryEventsSubscriberInterface; |
|
7
|
|
|
use Bdf\Prime\Repository\RepositoryInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Versionable |
|
11
|
|
|
* |
|
12
|
|
|
* The versionable behavior allows you to keep an history of your model objects. |
|
13
|
|
|
* |
|
14
|
|
|
* @template E as object |
|
15
|
|
|
* @extends Behavior<E> |
|
16
|
|
|
*/ |
|
17
|
|
|
class Versionable extends Behavior |
|
18
|
|
|
{ |
|
19
|
|
|
const COLUMN_NAME = 'version'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The version repository className |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $versionClass; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Allow version deletion |
|
30
|
|
|
* |
|
31
|
|
|
* @var boolean |
|
|
|
|
|
|
32
|
|
|
*/ |
|
33
|
|
|
protected $allowDeletion; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Version table name |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $tableName; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Versionable constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $versionClass |
|
46
|
|
|
* @param bool $allowDeletion |
|
|
|
|
|
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function __construct($versionClass, $allowDeletion = false) |
|
49
|
|
|
{ |
|
50
|
1 |
|
$this->versionClass = $versionClass; |
|
51
|
1 |
|
$this->allowDeletion = $allowDeletion; |
|
52
|
1 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
|
|
|
|
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function changeSchema(FieldBuilder $builder): void |
|
58
|
|
|
{ |
|
59
|
1 |
|
$builder->integer(self::COLUMN_NAME, 0); |
|
60
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Before insert |
|
64
|
|
|
* |
|
65
|
|
|
* we increment version number on entity |
|
|
|
|
|
|
66
|
|
|
* |
|
67
|
|
|
* @param E $entity |
|
|
|
|
|
|
68
|
|
|
* @param RepositoryInterface<E> $repository |
|
|
|
|
|
|
69
|
|
|
*/ |
|
70
|
6 |
|
public function beforeInsert($entity, $repository) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
6 |
|
$this->incrementVersion($entity, $repository); |
|
73
|
6 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* After insert |
|
77
|
|
|
* |
|
78
|
|
|
* we historicize entity |
|
|
|
|
|
|
79
|
|
|
* |
|
80
|
|
|
* @param E $entity |
|
81
|
|
|
* @param RepositoryInterface<E> $repository |
|
|
|
|
|
|
82
|
|
|
* @param integer $count |
|
|
|
|
|
|
83
|
|
|
*/ |
|
84
|
6 |
|
public function afterInsert($entity, $repository, $count) |
|
|
|
|
|
|
85
|
|
|
{ |
|
86
|
6 |
|
if ($count != 0) { |
|
87
|
6 |
|
$this->insertVersion($entity, $repository); |
|
88
|
|
|
} |
|
89
|
6 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Before update |
|
93
|
|
|
* |
|
94
|
|
|
* we increment version number on entity |
|
|
|
|
|
|
95
|
|
|
* |
|
96
|
|
|
* @param E $entity |
|
97
|
|
|
* @param RepositoryInterface<E> $repository |
|
|
|
|
|
|
98
|
|
|
* @param null|\ArrayObject $attributes |
|
99
|
|
|
*/ |
|
100
|
5 |
|
public function beforeUpdate($entity, $repository, $attributes) |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
5 |
|
if ($attributes !== null) { |
|
103
|
1 |
|
$attributes[] = self::COLUMN_NAME; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
5 |
|
$this->incrementVersion($entity, $repository); |
|
107
|
5 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* After update |
|
111
|
|
|
* |
|
112
|
|
|
* we historicize entity |
|
|
|
|
|
|
113
|
|
|
* |
|
114
|
|
|
* @param E $entity |
|
115
|
|
|
* @param RepositoryInterface<E> $repository |
|
|
|
|
|
|
116
|
|
|
* @param integer $count |
|
|
|
|
|
|
117
|
|
|
*/ |
|
118
|
5 |
|
public function afterUpdate($entity, $repository, $count) |
|
|
|
|
|
|
119
|
|
|
{ |
|
120
|
5 |
|
if ($count != 0) { |
|
121
|
5 |
|
$this->insertVersion($entity, $repository); |
|
122
|
|
|
} |
|
123
|
5 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Remove entity versions |
|
127
|
|
|
* |
|
128
|
|
|
* @param E $entity |
|
129
|
|
|
* @param RepositoryInterface<E> $repository |
|
|
|
|
|
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function deleteAllVersions($entity, $repository) |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
1 |
|
$queries = $repository->repository($this->versionClass)->queries(); |
|
134
|
1 |
|
$criteria = $repository->mapper()->primaryCriteria($entity); |
|
135
|
|
|
|
|
136
|
1 |
|
if ($query = $queries->keyValue($criteria)) { |
|
|
|
|
|
|
137
|
1 |
|
$query->delete(); |
|
138
|
|
|
} else { |
|
139
|
|
|
$queries->builder()->where($criteria)->delete(); |
|
140
|
|
|
} |
|
141
|
1 |
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
|
|
|
|
|
144
|
|
|
* {@inheritdoc} |
|
145
|
|
|
*/ |
|
146
|
1 |
|
public function subscribe(RepositoryEventsSubscriberInterface $notifier): void |
|
147
|
|
|
{ |
|
148
|
1 |
|
$notifier->inserting([$this, 'beforeInsert']); |
|
149
|
1 |
|
$notifier->inserted([$this, 'afterInsert']); |
|
150
|
|
|
|
|
151
|
1 |
|
$notifier->updating([$this, 'beforeUpdate']); |
|
152
|
1 |
|
$notifier->updated([$this, 'afterUpdate']); |
|
153
|
|
|
|
|
154
|
1 |
|
if ($this->allowDeletion) { |
|
155
|
1 |
|
$notifier->deleted([$this, 'deleteAllVersions']); |
|
156
|
|
|
} |
|
157
|
1 |
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Increment version number on entity |
|
161
|
|
|
* |
|
162
|
|
|
* @param E $entity |
|
163
|
|
|
* @param RepositoryInterface<E> $repository |
|
|
|
|
|
|
164
|
|
|
*/ |
|
165
|
6 |
|
protected function incrementVersion($entity, $repository) |
|
|
|
|
|
|
166
|
|
|
{ |
|
167
|
6 |
|
$mapper = $repository->mapper(); |
|
168
|
|
|
|
|
169
|
6 |
|
$mapper->hydrateOne( |
|
170
|
6 |
|
$entity, |
|
171
|
6 |
|
self::COLUMN_NAME, |
|
172
|
6 |
|
$mapper->extractOne($entity, self::COLUMN_NAME) + 1 |
|
|
|
|
|
|
173
|
|
|
); |
|
174
|
6 |
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Historicize entity |
|
178
|
|
|
* |
|
179
|
|
|
* @param E $entity |
|
180
|
|
|
* @param RepositoryInterface<E> $repository |
|
|
|
|
|
|
181
|
|
|
*/ |
|
182
|
6 |
|
protected function insertVersion($entity, $repository) |
|
|
|
|
|
|
183
|
|
|
{ |
|
184
|
6 |
|
$repository->repository($this->versionClass)->insert($entity); |
|
185
|
6 |
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|