-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListenerServiceProvider.php
More file actions
140 lines (121 loc) · 3.44 KB
/
Copy pathListenerServiceProvider.php
File metadata and controls
140 lines (121 loc) · 3.44 KB
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
<?php
declare(strict_types=1);
namespace RabbitEvents\Listener;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use RabbitEvents\Listener\Facades\RabbitEvents;
class ListenerServiceProvider extends BaseServiceProvider
{
use HasListeners\RegisterListeners;
/**
* The event listener mappings for the application.
*
* @var array
*/
protected array $listen = [];
/**
* Register any events for your application.
*
* @return void
*/
public function boot(): void
{
if (!$this->app->runningInConsole()) {
return;
}
$this->commands([
Console\ListenCommand::class,
Console\EventsListCommand::class,
Console\EventsCacheCommand::class,
Console\EventsClearCommand::class,
]);
foreach ($this->listens() as $event => $listeners) {
foreach ($listeners as $listener) {
RabbitEvents::listen($event, $listener);
}
}
}
/**
* Get the events and handlers.
*
* @return array
*/
public function listens(): array
{
if ($this->eventsAreCached()) {
$listeners = array_merge_recursive(
$this->listen,
require $this->app->bootstrapPath('cache/rabbitevents.php')
);
} else {
if ($this->shouldDiscoverEvents()) {
$this->listenerClasses = array_unique(array_merge(
$this->listenerClasses,
$this->discoverEvents()
));
}
$listeners = array_merge_recursive($this->listen, $this->getEventsFromAttributes());
}
return $this->deduplicateListeners($listeners);
}
/**
* Determine if the application events are cached.
*
* @return bool
*/
protected function eventsAreCached(): bool
{
return $this->app->bound('path.bootstrap') &&
file_exists($this->app->bootstrapPath('cache/rabbitevents.php'));
}
/**
* Determine if events and listeners should be automatically discovered.
*
* @return bool
*/
public function shouldDiscoverEvents(): bool
{
return true;
}
/**
* Get the listener directory path.
*
* @return string
*/
protected function listenerDirectory(): string
{
return $this->app->path('Listeners');
}
/**
* Discover the events and listeners for the application.
*
* @return array
*/
public function discoverEvents(): array
{
return HasListeners\ListenerDiscoverer::discover(
$this->listenerDirectory(),
$this->app->path(),
$this->app->getNamespace()
);
}
public function register(): void
{
$this->app->singleton(Dispatcher::class);
$this->app->alias(Dispatcher::class, 'rabbitevents.events');
$this->registerPublishing();
}
/**
* Set up the resource publishing groups for RabbitEvents.
*
* @return void
*/
protected function registerPublishing(): void
{
if ($this->app->runningInConsole()) {
$serviceProvider = 'RabbitEventsServiceProvider';
$this->publishes([
__DIR__ . "/stubs/$serviceProvider.stub" => $this->app->path("Providers/$serviceProvider.php"),
], 'rabbitevents-listener-provider');
}
}
}