Skip to content

Commit b3b53ac

Browse files
authored
Fix type hinting the foundation application which is incompatible with Lumen (#462)
1 parent cd2c431 commit b3b53ac

File tree

3 files changed

+50
-32
lines changed

3 files changed

+50
-32
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Fix type hints incompatible with Laravel Lumen (#462)
6+
57
## 2.4.0
68

79
- Read the request IP from the Laravel request to make it more accurate when behind a reverse proxy (requires [trusted proxies](https://laravel.com/docs/8.x/requests#configuring-trusted-proxies) to be setup correctly) (#419)

src/Sentry/Laravel/EventHandler.php

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use Illuminate\Auth\Events\Authenticated;
77
use Illuminate\Console\Events\CommandFinished;
88
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;
1012
use Illuminate\Database\Events\QueryExecuted;
1113
use Illuminate\Http\Request;
1214
use Illuminate\Log\Events\MessageLogged;
@@ -67,9 +69,9 @@ class EventHandler
6769
/**
6870
* The Laravel container.
6971
*
70-
* @var \Illuminate\Contracts\Foundation\Application
72+
* @var \Illuminate\Contracts\Container\Container
7173
*/
72-
private $app;
74+
private $container;
7375

7476
/**
7577
* Indicates if we should we add SQL queries to the breadcrumbs.
@@ -116,12 +118,12 @@ class EventHandler
116118
/**
117119
* EventHandler constructor.
118120
*
119-
* @param \Illuminate\Contracts\Foundation\Application $app
120-
* @param array $config
121+
* @param \Illuminate\Contracts\Container\Container $container
122+
* @param array $config
121123
*/
122-
public function __construct(Application $app, array $config)
124+
public function __construct(Container $container, array $config)
123125
{
124-
$this->app = $app;
126+
$this->container = $container;
125127

126128
$this->recordSqlQueries = ($config['breadcrumbs.sql_queries'] ?? $config['breadcrumbs']['sql_queries'] ?? true) === true;
127129
$this->recordSqlBindings = ($config['breadcrumbs.sql_bindings'] ?? $config['breadcrumbs']['sql_bindings'] ?? false) === true;
@@ -133,26 +135,34 @@ public function __construct(Application $app, array $config)
133135
/**
134136
* Attach all event handlers.
135137
*/
136-
public function subscribe()
138+
public function subscribe(): void
137139
{
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);
140143

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
143149
}
144150
}
145151

146152
/**
147153
* Attach all authentication event handlers.
148154
*/
149-
public function subscribeAuthEvents()
155+
public function subscribeAuthEvents(): void
150156
{
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);
153160

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
156166
}
157167
}
158168

@@ -161,18 +171,22 @@ public function subscribeAuthEvents()
161171
*
162172
* @param \Illuminate\Queue\QueueManager $queue
163173
*/
164-
public function subscribeQueueEvents(QueueManager $queue)
174+
public function subscribeQueueEvents(QueueManager $queue): void
165175
{
166176
$queue->looping(function () {
167177
$this->cleanupScopeForQueuedJob();
168178
$this->afterQueuedJob();
169179
});
170180

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);
173184

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
176190
}
177191
}
178192

@@ -375,9 +389,9 @@ protected function authenticatedHandler(Authenticated $event)
375389
'id' => $event->user->getAuthIdentifier(),
376390
];
377391

378-
if ($this->app->bound('request')) {
392+
try {
379393
/** @var \Illuminate\Http\Request $request */
380-
$request = $this->app->make('request');
394+
$request = $this->container->make('request');
381395

382396
if ($request instanceof Request) {
383397
$ipAddress = $request->ip();
@@ -386,6 +400,8 @@ protected function authenticatedHandler(Authenticated $event)
386400
$userData['ip_address'] = $ipAddress;
387401
}
388402
}
403+
} catch (BindingResolutionException $e) {
404+
// If there is no request bound we cannot get the IP address from it
389405
}
390406

391407
Integration::configureScope(static function (Scope $scope) use ($userData): void {

src/Sentry/Laravel/Http/LaravelRequestFetcher.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,33 @@
33
namespace Sentry\Laravel\Http;
44

55
use Illuminate\Contracts\Container\BindingResolutionException;
6-
use Illuminate\Contracts\Foundation\Application;
6+
use Illuminate\Contracts\Container\Container;
77
use Psr\Http\Message\ServerRequestInterface;
88
use Sentry\Integration\RequestFetcher;
99
use Sentry\Integration\RequestFetcherInterface;
1010

1111
class LaravelRequestFetcher implements RequestFetcherInterface
1212
{
1313
/**
14-
* The Laravel application container.
14+
* The Laravel container.
1515
*
16-
* @var \Illuminate\Contracts\Foundation\Application
16+
* @var \Illuminate\Contracts\Container\Container
1717
*/
18-
private $app;
18+
private $container;
1919

20-
public function __construct(Application $app)
20+
public function __construct(Container $container)
2121
{
22-
$this->app = $app;
22+
$this->container = $container;
2323
}
2424

2525
public function fetchRequest(): ?ServerRequestInterface
2626
{
27-
if (!$this->app->bound('request') || $this->app->runningInConsole()) {
27+
if (\PHP_SAPI === 'cli' || \PHP_SAPI === 'phpdbg' || !$this->container->bound('request')) {
2828
return null;
2929
}
3030

3131
try {
32-
return $this->app->make(ServerRequestInterface::class);
32+
return $this->container->make(ServerRequestInterface::class);
3333
} catch (BindingResolutionException $e) {
3434
return (new RequestFetcher)->fetchRequest();
3535
}

0 commit comments

Comments
 (0)