-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListenerResolver.php
68 lines (53 loc) · 1.81 KB
/
ListenerResolver.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
<?php
namespace Racine;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class ListenerResolver implements ListenerResolverInterface
{
/**
* @var array
*/
private $listeners;
/**
* @var array
*/
private $sources;
public function __construct()
{
$this->sources = [
realpath(__DIR__.'/listeners.php'),
_APP_DIR_.'/listeners.php',
];
$this->listeners = $this->load();
}
public function resolve(EventDispatcherInterface $dispatcher)
{
foreach ($this->listeners as $name => $listener){
if(!empty($listener['class']) && !empty($listener['type'])){
switch ($listener['type']){
case 'racine.event_listener':
if(!empty($listener['method']) && !empty($listener['event'])){
if(!empty($listener['priority'])){
$listener['priority'] = 0;
}
$dispatcher->addListener($listener['event'], [new $listener['class'](), $listener['method']], (int)$listener['priority']);
}
break;
case 'racine.event_subscriber':
$dispatcher->addSubscriber(new $listener['class']());
break;
}
}
}
return $dispatcher;
}
private function load()
{
$this->listeners = [];
foreach ($this->sources as $source){
if(file_exists($source)){
$this->listeners = array_merge_recursive($this->listeners, (require $source));
}
}
return $this->listeners;
}
}