/
JarvesEventDispatcher.php
187 lines (162 loc) · 5.24 KB
/
JarvesEventDispatcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* This file is part of Jarves.
*
* (c) Marc J. Schmidt <marc@marcjschmidt.de>
*
* J.A.R.V.E.S - Just A Rather Very Easy [content management] System.
*
* http://jarves.io
*
* To get the full copyright and license information, please view the
* LICENSE file, that was distributed with this source code.
*/
namespace Jarves;
use Jarves\Cache\Cacher;
use Jarves\Configuration\Bundle;
use Jarves\Configuration\Event;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
/**
* Middleware for custom event hooking in jarves configuration files and real event dispatcher.
*/
class JarvesEventDispatcher
{
/**
* @var array
*/
protected $attachedEvents = [];
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
* @var ContainerInterface
*/
private $container;
/**
* @var ConditionOperator
*/
private $conditionOperator;
/**
* @var Cacher
*/
private $cacher;
/**
* @param ContainerInterface $container
* @param ConditionOperator $conditionOperator
* @param Cacher $cacher
*/
public function __construct(EventDispatcherInterface $eventDispatcher, ContainerInterface $container, ConditionOperator $conditionOperator, Cacher $cacher)
{
$this->container = $container;
$this->conditionOperator = $conditionOperator;
$this->cacher = $cacher;
$this->eventDispatcher = $eventDispatcher;
}
/**
* @param Configuration\Configs $configs
*/
public function registerBundleEvents(Configuration\Configs $configs)
{
$this->detachEvents();
foreach ($configs->getConfigs() as $bundleConfig) {
//register custom listener through config, like cache clearing, service calls etc
if ($events = $bundleConfig->getListeners()) {
foreach ($events as $event) {
$this->attachEvent($event);
}
}
//clear storage caches when object changes
if ($objects = $bundleConfig->getObjects()) {
foreach ($objects as $object) {
$fn = function () use ($object) {
$storage = $this->container->get($object->getStorageService());
$storage->configure($object->getKey(), $object);
$storage->clearCache();
};
$event = new Event();
$event->setSubject($object->getKey());
$event->setKey('core/object/modify');
$event->setCalls([$fn]);
$this->attachEvent($event);
}
}
}
}
public function attachEvent(Event $event)
{
$fn = function (GenericEvent $genericEvent) use ($event) {
if ($this->isCallable($event, $genericEvent)) {
$this->call($event, $genericEvent);
}
};
$this->eventDispatcher->addListener($event->getKey(), $fn);
$this->attachedEvents[] = [
'key' => $event->getKey(),
'event' => $event,
'callback' => $fn
];
}
/**
* @param Event $eventConfig
* @param GenericEvent $event
*/
public function call(Event $eventConfig, $event)
{
if ($eventConfig->getCalls()) {
foreach ($eventConfig->getCalls() as $call) {
call_user_func_array($call, [$event]);
}
}
if ($eventConfig->getClearCaches()) {
foreach ($eventConfig->getClearCaches() as $cacheKey) {
$this->cacher->invalidateCache($cacheKey);
}
}
if ($eventConfig->getServiceCalls()) {
foreach ($eventConfig->getServiceCalls() as $serviceCall) {
list($service, $method) = explode('::', $serviceCall);
if ($this->container->has($service)) {
$service = $this->container->get($service);
$service->$method($event);
}
}
}
}
/**
* Checks whether a eventConfig is appropriate to be called (subject fits, condition fits)
*
* @param Event $eventConfig
* @param GenericEvent $event
* @return bool
*/
public function isCallable(Event $eventConfig, GenericEvent $event)
{
if ($eventConfig->getSubject() && $event->getSubject() != $eventConfig->getSubject()) {
return false;
}
if ($eventConfig->getCondition()) {
$args = $event->getArguments() ?: [];
if ($eventConfig->getCondition() && !$this->conditionOperator->satisfy($eventConfig->getCondition(), $args)) {
return false;
}
}
return true;
}
public function detachEvents()
{
foreach ($this->attachedEvents as $eventInfo) {
$this->eventDispatcher->removeListener($eventInfo['key'], $eventInfo['callback']);
}
$this->attachedEvents = [];
}
/**
* @return array
*/
public function getAttachedEvents()
{
return $this->attachedEvents;
}
}