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\EventPublisher; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\EventBus\Event\EventBus; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* DomainEventPublisher class. |
18
|
|
|
* |
19
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class DomainEventPublisher |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var DomainEventPublisher |
25
|
|
|
*/ |
26
|
|
|
private static $instance = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var EventBus |
30
|
|
|
*/ |
31
|
|
|
protected $eventBus; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return DomainEventPublisher |
35
|
|
|
*/ |
36
|
|
|
private static function instance() |
37
|
|
|
{ |
38
|
|
|
if (static::$instance === null) { |
|
|
|
|
39
|
|
|
static::$instance = new static(); |
|
|
|
|
40
|
|
|
|
41
|
|
|
static::$instance->setEventBus(EventBus::create()); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return static::$instance; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param EventBus $eventBus |
49
|
|
|
*/ |
50
|
|
|
public static function set(EventBus $eventBus) |
51
|
|
|
{ |
52
|
|
|
static::instance()->setEventBus($eventBus); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return EventBus $eventBus |
57
|
|
|
*/ |
58
|
|
|
public static function eventBus() |
59
|
|
|
{ |
60
|
|
|
return static::instance()->getEventBus(); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param DomainEventInterface $event |
65
|
|
|
*/ |
66
|
|
|
public static function publish(DomainEventInterface $event) |
67
|
|
|
{ |
68
|
|
|
static::instance()->dispatch($event); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param DomainEventSubscriberInterface $subscriber |
73
|
|
|
*/ |
74
|
|
|
public static function subscribe(DomainEventSubscriberInterface $subscriber) |
75
|
|
|
{ |
76
|
|
|
static::instance()->addSubscriber($subscriber); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param DomainEventInterface $event |
81
|
|
|
*/ |
82
|
|
|
protected function dispatch(DomainEventInterface $event) |
83
|
|
|
{ |
84
|
|
|
$this->eventBus->dispatch($event); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param DomainEventSubscriberInterface $subscriber |
89
|
|
|
*/ |
90
|
|
|
protected function addSubscriber(DomainEventSubscriberInterface $subscriber) |
91
|
|
|
{ |
92
|
|
|
$this->eventBus->addSubscriber($subscriber); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param EventBus $eventBus |
97
|
|
|
*/ |
98
|
|
|
protected function setEventBus(EventBus $eventBus) |
99
|
|
|
{ |
100
|
|
|
$this->eventBus = $eventBus; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return EventBus |
105
|
|
|
*/ |
106
|
|
|
protected function getEventBus() |
107
|
|
|
{ |
108
|
|
|
return $this->eventBus; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
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: