-
Notifications
You must be signed in to change notification settings - Fork 10.8k
/
EventServiceProvider.php
214 lines (190 loc) · 5.07 KB
/
EventServiceProvider.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
namespace Illuminate\Foundation\Support\Providers;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Events\DiscoverEvents;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event handler mappings for the application.
*
* @var array<string, array<int, string>>
*/
protected $listen = [];
/**
* The subscribers to register.
*
* @var array
*/
protected $subscribe = [];
/**
* The model observers to register.
*
* @var array<string, string|object|array<int, string|object>>
*/
protected $observers = [];
/**
* Indicates if events should be discovered.
*
* @var bool
*/
protected static $shouldDiscoverEvents = true;
/**
* The configured event discovery paths.
*
* @var array|null
*/
protected static $eventDiscoveryPaths;
/**
* Register the application's event listeners.
*
* @return void
*/
public function register()
{
$this->booting(function () {
$events = $this->getEvents();
foreach ($events as $event => $listeners) {
foreach (array_unique($listeners, SORT_REGULAR) as $listener) {
Event::listen($event, $listener);
}
}
foreach ($this->subscribe as $subscriber) {
Event::subscribe($subscriber);
}
foreach ($this->observers as $model => $observers) {
$model::observe($observers);
}
});
$this->booted(function () {
$this->configureEmailVerification();
});
}
/**
* Boot any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Get the events and handlers.
*
* @return array
*/
public function listens()
{
return $this->listen;
}
/**
* Get the discovered events and listeners for the application.
*
* @return array
*/
public function getEvents()
{
if ($this->app->eventsAreCached()) {
$cache = require $this->app->getCachedEventsPath();
return $cache[get_class($this)] ?? [];
} else {
return array_merge_recursive(
$this->discoveredEvents(),
$this->listens()
);
}
}
/**
* Get the discovered events for the application.
*
* @return array
*/
protected function discoveredEvents()
{
return $this->shouldDiscoverEvents()
? $this->discoverEvents()
: [];
}
/**
* Determine if events and listeners should be automatically discovered.
*
* @return bool
*/
public function shouldDiscoverEvents()
{
return get_class($this) === __CLASS__ && static::$shouldDiscoverEvents === true;
}
/**
* Discover the events and listeners for the application.
*
* @return array
*/
public function discoverEvents()
{
return collect($this->discoverEventsWithin())
->reject(function ($directory) {
return ! is_dir($directory);
})
->reduce(function ($discovered, $directory) {
return array_merge_recursive(
$discovered,
DiscoverEvents::within($directory, $this->eventDiscoveryBasePath())
);
}, []);
}
/**
* Get the listener directories that should be used to discover events.
*
* @return array
*/
protected function discoverEventsWithin()
{
return static::$eventDiscoveryPaths ?: [
$this->app->path('Listeners'),
];
}
/**
* Set the globally configured event discovery paths.
*
* @param array $paths
* @return void
*/
public static function setEventDiscoveryPaths(array $paths)
{
static::$eventDiscoveryPaths = $paths;
}
/**
* Get the base path to be used during event discovery.
*
* @return string
*/
protected function eventDiscoveryBasePath()
{
return base_path();
}
/**
* Disable event discovery for the application.
*
* @return void
*/
public static function disableEventDiscovery()
{
static::$shouldDiscoverEvents = false;
}
/**
* Configure the proper event listeners for email verification.
*
* @return void
*/
protected function configureEmailVerification()
{
if (! isset($this->listen[Registered::class]) ||
! in_array(SendEmailVerificationNotification::class, Arr::wrap($this->listen[Registered::class]))) {
Event::listen(Registered::class, SendEmailVerificationNotification::class);
}
}
}