1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cubiche\Domain\EventSourcing\Versioning; |
13
|
|
|
|
14
|
|
|
use Cubiche\Domain\EventSourcing\EventSourcedAggregateRootInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* VersionManager class. |
18
|
|
|
* |
19
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class VersionManager |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var VersionManager |
25
|
|
|
*/ |
26
|
|
|
private static $instance = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var VersionStoreInterface |
30
|
|
|
*/ |
31
|
|
|
protected $versionStore; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* VersionManager constructor. |
35
|
|
|
* |
36
|
|
|
* @param VersionStoreInterface $versionStore |
37
|
|
|
*/ |
38
|
|
|
private function __construct(VersionStoreInterface $versionStore) |
39
|
|
|
{ |
40
|
|
|
$this->versionStore = $versionStore; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return VersionManager |
45
|
|
|
*/ |
46
|
|
|
public static function create() |
47
|
|
|
{ |
48
|
|
|
if (static::$instance === null) { |
|
|
|
|
49
|
|
|
static::$instance = new static(new InMemoryVersionStore()); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return static::$instance; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param VersionStoreInterface $versionStore |
57
|
|
|
*/ |
58
|
|
|
public static function setVersionStore(VersionStoreInterface $versionStore) |
59
|
|
|
{ |
60
|
|
|
static::create()->versionStore = $versionStore; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param EventSourcedAggregateRootInterface $aggregate |
65
|
|
|
* |
66
|
|
|
* @return int |
67
|
|
|
*/ |
68
|
|
|
public static function versionOf(EventSourcedAggregateRootInterface $aggregate) |
69
|
|
|
{ |
70
|
|
|
return static::versionOfClass(get_class($aggregate)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param EventSourcedAggregateRootInterface $aggregate |
75
|
|
|
*/ |
76
|
|
|
public static function persistVersionOf(EventSourcedAggregateRootInterface $aggregate) |
77
|
|
|
{ |
78
|
|
|
static::persistVersionOfClass(get_class($aggregate), $aggregate->version()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $className |
83
|
|
|
* |
84
|
|
|
* @return int |
85
|
|
|
*/ |
86
|
|
|
public static function versionOfClass($className) |
87
|
|
|
{ |
88
|
|
|
return static::create()->getAggregateRootVersion($className); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $className |
93
|
|
|
* @param int $version |
94
|
|
|
*/ |
95
|
|
|
public static function persistVersionOfClass($className, $version) |
96
|
|
|
{ |
97
|
|
|
static::create()->setAggregateRootVersion($className, $version); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $aggregateClassName |
102
|
|
|
* |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
|
|
protected function getAggregateRootVersion($aggregateClassName) |
106
|
|
|
{ |
107
|
|
|
return $this->versionStore->load($aggregateClassName); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $aggregateClassName |
112
|
|
|
* @param int $version |
113
|
|
|
*/ |
114
|
|
|
protected function setAggregateRootVersion($aggregateClassName, $version) |
115
|
|
|
{ |
116
|
|
|
$this->versionStore->persist($aggregateClassName, $version); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: