1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\EventDispatcher\ListenerProviders\Discover; |
4
|
|
|
|
5
|
|
|
use ByTIC\EventDispatcher\ListenerProviders\DefaultProvider; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class DiscoverProvider |
9
|
|
|
* @package ByTIC\EventDispatcher\ListenerProviders\Discover |
10
|
|
|
*/ |
11
|
|
|
class DiscoverProvider extends DefaultProvider |
12
|
|
|
{ |
13
|
|
|
protected $discovered = null; |
14
|
|
|
|
15
|
|
|
protected $discoveryPath = null; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @inheritDoc |
19
|
|
|
*/ |
20
|
1 |
|
public function getListenersForEvent(object $event): iterable |
21
|
|
|
{ |
22
|
1 |
|
$this->discover(); |
23
|
1 |
|
return parent::getListenersForEvent($event); |
24
|
|
|
} |
25
|
|
|
|
26
|
1 |
|
protected function discover() |
27
|
|
|
{ |
28
|
1 |
|
if ($this->discovered !== null) { |
29
|
|
|
return; |
30
|
|
|
} |
31
|
1 |
|
$eventsArray = DiscoverEvents::within($this->discoverEventsWithin()); |
32
|
1 |
|
foreach ($eventsArray as $event => $listeners) { |
33
|
1 |
|
foreach ($listeners as $listener) { |
34
|
1 |
|
$this->listen($event, $listener); |
35
|
|
|
} |
36
|
|
|
} |
37
|
1 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the listener directories that should be used to discover events. |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
1 |
|
protected function discoverEventsWithin(): array |
45
|
|
|
{ |
46
|
1 |
|
if ($this->discoveryPath === null) { |
47
|
|
|
$this->discoveryPath = $this->detectDiscoveryPath(); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
return $this->discoveryPath; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $path |
55
|
|
|
*/ |
56
|
1 |
|
public function addDiscoveryPath(string $path) |
57
|
|
|
{ |
58
|
1 |
|
$this->discoveryPath[] = $path; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
protected function detectDiscoveryPath(): array |
65
|
|
|
{ |
66
|
|
|
$paths = $this->detectDiscoveryFromConfig(); |
67
|
|
|
if (count($paths)) { |
68
|
|
|
return $paths; |
69
|
|
|
} |
70
|
|
|
return $this->detectDiscoveryFromApplication(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function detectDiscoveryFromConfig(): array |
74
|
|
|
{ |
75
|
|
|
if (!function_exists('config')) { |
76
|
|
|
return []; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$config = config('event-dispatcher.listener_paths'); |
80
|
|
|
if (!$config) { |
81
|
|
|
return []; |
82
|
|
|
} |
83
|
|
|
$paths = $config->toArray(); |
84
|
|
|
foreach ($paths as $key => $path) { |
85
|
|
|
if (!is_dir($path)) { |
86
|
|
|
unset($paths[$key]); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
return $paths; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function detectDiscoveryFromApplication(): array |
93
|
|
|
{ |
94
|
|
|
$basePath = $this->detectApplicationPath(); |
95
|
|
|
if (!is_dir($basePath)) { |
|
|
|
|
96
|
|
|
return []; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$folder = $basePath . '/Listeners'; |
|
|
|
|
100
|
|
|
if (!is_dir($folder)) { |
101
|
|
|
return []; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return [ |
105
|
|
|
$folder, |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return false|string |
111
|
|
|
*/ |
112
|
|
|
protected function detectApplicationPath() |
113
|
|
|
{ |
114
|
|
|
if (defined('APPLICATION_PATH')) { |
115
|
|
|
return APPLICATION_PATH; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
if (!function_exists('app')) { |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
$app = app(); |
121
|
|
|
if ($app->has('path')) { |
122
|
|
|
return $app->get('path'); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|