1
|
|
|
<?php |
2
|
|
|
namespace SmoothPhp\EventSourcing; |
3
|
|
|
|
4
|
|
|
use SmoothPhp\Contracts\EventSourcing\AggregateRoot as AggregateRootInterface; |
5
|
|
|
use SmoothPhp\Contracts\EventSourcing\Entity; |
|
|
|
|
6
|
|
|
use SmoothPhp\Contracts\EventSourcing\Event; |
7
|
|
|
use SmoothPhp\Domain\DomainEventStream; |
8
|
|
|
use SmoothPhp\Domain\DomainMessage; |
9
|
|
|
use SmoothPhp\Domain\Metadata; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class AggregateRoot |
13
|
|
|
* @author Simon Bennett <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
abstract class AggregateRoot implements AggregateRootInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var [] |
19
|
|
|
*/ |
20
|
|
|
private $uncommittedEvents = []; |
21
|
|
|
|
22
|
|
|
private $playHead = -1; |
23
|
|
|
|
24
|
|
|
/** @var Entity[] */ |
25
|
|
|
private $children = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Event $event |
29
|
|
|
*/ |
30
|
18 |
|
public function apply(Event $event) |
31
|
|
|
{ |
32
|
18 |
|
$this->handleRecursively($event); |
33
|
|
|
|
34
|
18 |
|
$this->playHead++; |
35
|
18 |
|
$this->uncommittedEvents[] = DomainMessage::recordNow( |
36
|
18 |
|
$this->getAggregateRootId(), |
37
|
18 |
|
$this->playHead, |
38
|
18 |
|
new Metadata(array()), |
39
|
|
|
$event |
40
|
18 |
|
); |
41
|
18 |
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Handles event if capable. |
46
|
|
|
* |
47
|
|
|
* @param $event |
48
|
|
|
*/ |
49
|
24 |
View Code Duplication |
protected function handle(Event $event) |
|
|
|
|
50
|
|
|
{ |
51
|
24 |
|
$method = $this->getApplyMethod($event); |
52
|
24 |
|
if (!method_exists($this, $method)) { |
53
|
18 |
|
return; |
54
|
|
|
} |
55
|
6 |
|
$this->$method($event); |
56
|
6 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Event $event |
60
|
|
|
*/ |
61
|
18 |
|
protected function handleRecursively(Event $event) |
62
|
|
|
{ |
63
|
18 |
|
$this->handle($event); |
64
|
|
|
|
65
|
18 |
|
foreach ($this->children as $child) { |
66
|
6 |
|
$child->registerAggregateRoot($this); |
67
|
6 |
|
$child->handleRecursively($event); |
68
|
18 |
|
} |
69
|
18 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Event $event |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
24 |
|
private function getApplyMethod(Event $event) |
76
|
|
|
{ |
77
|
24 |
|
$classParts = explode('\\', get_class($event)); |
78
|
|
|
|
79
|
24 |
|
return 'apply' . end($classParts); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return DomainEventStream |
84
|
|
|
*/ |
85
|
15 |
|
public function getUncommittedEvents() |
86
|
|
|
{ |
87
|
15 |
|
$stream = new DomainEventStream($this->uncommittedEvents); |
88
|
|
|
|
89
|
15 |
|
$this->uncommittedEvents = []; |
90
|
|
|
|
91
|
15 |
|
return $stream; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param DomainEventStream $stream |
96
|
|
|
*/ |
97
|
6 |
|
public function initializeState(DomainEventStream $stream) |
98
|
|
|
{ |
99
|
6 |
|
foreach ($stream as $message) { |
100
|
6 |
|
$this->playHead++; |
101
|
6 |
|
$this->handle($message->getPayload()); |
102
|
6 |
|
} |
103
|
6 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param Entity $entity |
107
|
|
|
*/ |
108
|
6 |
|
public function addChildEntity(Entity $entity) |
109
|
|
|
{ |
110
|
6 |
|
$this->children[] = $entity; |
111
|
|
|
} |
112
|
|
|
} |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: