6
6
use Illuminate \Auth \Events \Authenticated ;
7
7
use Illuminate \Console \Events \CommandFinished ;
8
8
use Illuminate \Console \Events \CommandStarting ;
9
- use Illuminate \Contracts \Foundation \Application ;
9
+ use Illuminate \Contracts \Container \BindingResolutionException ;
10
+ use Illuminate \Contracts \Container \Container ;
11
+ use Illuminate \Contracts \Events \Dispatcher ;
10
12
use Illuminate \Database \Events \QueryExecuted ;
11
13
use Illuminate \Http \Request ;
12
14
use Illuminate \Log \Events \MessageLogged ;
@@ -67,9 +69,9 @@ class EventHandler
67
69
/**
68
70
* The Laravel container.
69
71
*
70
- * @var \Illuminate\Contracts\Foundation\Application
72
+ * @var \Illuminate\Contracts\Container\Container
71
73
*/
72
- private $ app ;
74
+ private $ container ;
73
75
74
76
/**
75
77
* Indicates if we should we add SQL queries to the breadcrumbs.
@@ -116,12 +118,12 @@ class EventHandler
116
118
/**
117
119
* EventHandler constructor.
118
120
*
119
- * @param \Illuminate\Contracts\Foundation\Application $app
120
- * @param array $config
121
+ * @param \Illuminate\Contracts\Container\Container $container
122
+ * @param array $config
121
123
*/
122
- public function __construct (Application $ app , array $ config )
124
+ public function __construct (Container $ container , array $ config )
123
125
{
124
- $ this ->app = $ app ;
126
+ $ this ->container = $ container ;
125
127
126
128
$ this ->recordSqlQueries = ($ config ['breadcrumbs.sql_queries ' ] ?? $ config ['breadcrumbs ' ]['sql_queries ' ] ?? true ) === true ;
127
129
$ this ->recordSqlBindings = ($ config ['breadcrumbs.sql_bindings ' ] ?? $ config ['breadcrumbs ' ]['sql_bindings ' ] ?? false ) === true ;
@@ -133,26 +135,34 @@ public function __construct(Application $app, array $config)
133
135
/**
134
136
* Attach all event handlers.
135
137
*/
136
- public function subscribe ()
138
+ public function subscribe (): void
137
139
{
138
- /** @var \Illuminate\Events\Dispatcher $dispatcher */
139
- $ dispatcher = $ this ->app ['events ' ];
140
+ /** @var \Illuminate\Contracts\Events\Dispatcher $dispatcher */
141
+ try {
142
+ $ dispatcher = $ this ->container ->make (Dispatcher::class);
140
143
141
- foreach (static ::$ eventHandlerMap as $ eventName => $ handler ) {
142
- $ dispatcher ->listen ($ eventName , [$ this , $ handler ]);
144
+ foreach (static ::$ eventHandlerMap as $ eventName => $ handler ) {
145
+ $ dispatcher ->listen ($ eventName , [$ this , $ handler ]);
146
+ }
147
+ } catch (BindingResolutionException $ e ) {
148
+ // If we cannot resolve the event dispatcher we also cannot listen to events
143
149
}
144
150
}
145
151
146
152
/**
147
153
* Attach all authentication event handlers.
148
154
*/
149
- public function subscribeAuthEvents ()
155
+ public function subscribeAuthEvents (): void
150
156
{
151
- /** @var \Illuminate\Events\Dispatcher $dispatcher */
152
- $ dispatcher = $ this ->app ['events ' ];
157
+ /** @var \Illuminate\Contracts\Events\Dispatcher $dispatcher */
158
+ try {
159
+ $ dispatcher = $ this ->container ->make (Dispatcher::class);
153
160
154
- foreach (static ::$ authEventHandlerMap as $ eventName => $ handler ) {
155
- $ dispatcher ->listen ($ eventName , [$ this , $ handler ]);
161
+ foreach (static ::$ authEventHandlerMap as $ eventName => $ handler ) {
162
+ $ dispatcher ->listen ($ eventName , [$ this , $ handler ]);
163
+ }
164
+ } catch (BindingResolutionException $ e ) {
165
+ // If we cannot resolve the event dispatcher we also cannot listen to events
156
166
}
157
167
}
158
168
@@ -161,18 +171,22 @@ public function subscribeAuthEvents()
161
171
*
162
172
* @param \Illuminate\Queue\QueueManager $queue
163
173
*/
164
- public function subscribeQueueEvents (QueueManager $ queue )
174
+ public function subscribeQueueEvents (QueueManager $ queue ): void
165
175
{
166
176
$ queue ->looping (function () {
167
177
$ this ->cleanupScopeForQueuedJob ();
168
178
$ this ->afterQueuedJob ();
169
179
});
170
180
171
- /** @var \Illuminate\Events\Dispatcher $dispatcher */
172
- $ dispatcher = $ this ->app ['events ' ];
181
+ /** @var \Illuminate\Contracts\Events\Dispatcher $dispatcher */
182
+ try {
183
+ $ dispatcher = $ this ->container ->make (Dispatcher::class);
173
184
174
- foreach (static ::$ queueEventHandlerMap as $ eventName => $ handler ) {
175
- $ dispatcher ->listen ($ eventName , [$ this , $ handler ]);
185
+ foreach (static ::$ queueEventHandlerMap as $ eventName => $ handler ) {
186
+ $ dispatcher ->listen ($ eventName , [$ this , $ handler ]);
187
+ }
188
+ } catch (BindingResolutionException $ e ) {
189
+ // If we cannot resolve the event dispatcher we also cannot listen to events
176
190
}
177
191
}
178
192
@@ -375,9 +389,9 @@ protected function authenticatedHandler(Authenticated $event)
375
389
'id ' => $ event ->user ->getAuthIdentifier (),
376
390
];
377
391
378
- if ( $ this -> app -> bound ( ' request ' )) {
392
+ try {
379
393
/** @var \Illuminate\Http\Request $request */
380
- $ request = $ this ->app ->make ('request ' );
394
+ $ request = $ this ->container ->make ('request ' );
381
395
382
396
if ($ request instanceof Request) {
383
397
$ ipAddress = $ request ->ip ();
@@ -386,6 +400,8 @@ protected function authenticatedHandler(Authenticated $event)
386
400
$ userData ['ip_address ' ] = $ ipAddress ;
387
401
}
388
402
}
403
+ } catch (BindingResolutionException $ e ) {
404
+ // If there is no request bound we cannot get the IP address from it
389
405
}
390
406
391
407
Integration::configureScope (static function (Scope $ scope ) use ($ userData ): void {
0 commit comments