Skip to content

Commit cc71ebb

Browse files
Avoid collision if another package has bound 'sentry' (#467)
Co-authored-by: Alex Bouma <[email protected]>
1 parent 0c932f2 commit cc71ebb

File tree

7 files changed

+21
-14
lines changed

7 files changed

+21
-14
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+
- Avoid collision if another package has bound `sentry` in the Laravel container (#467)
6+
57
## 2.4.1
68

79
- Fix type hints incompatible with Laravel Lumen (#462)

src/Sentry/Laravel/Facade.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Sentry\Laravel;
44

5+
use Sentry\State\HubInterface;
6+
57
/**
68
* @method static bool addBreadcrumb(\Sentry\Breadcrumb $breadcrumb)
79
* @method static string|null captureMessage(string $message, \Sentry\Severity $level = null, \Sentry\State\Scope $scope = null)
@@ -21,6 +23,6 @@ class Facade extends \Illuminate\Support\Facades\Facade
2123
{
2224
protected static function getFacadeAccessor()
2325
{
24-
return 'sentry';
26+
return HubInterface::class;
2527
}
2628
}

src/Sentry/Laravel/Http/SetRequestIpMiddleware.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Illuminate\Http\Request;
7+
use Sentry\State\HubInterface;
78
use Sentry\State\Scope;
89

910
class SetRequestIpMiddleware
@@ -18,14 +19,14 @@ class SetRequestIpMiddleware
1819
*/
1920
public function handle(Request $request, Closure $next)
2021
{
21-
if (app()->bound('sentry')) {
22+
if (app()->bound(HubInterface::class)) {
2223
/** @var \Sentry\State\HubInterface $sentry */
23-
$sentry = app('sentry');
24+
$sentry = app(HubInterface::class);
2425

2526
$client = $sentry->getClient();
2627

2728
if ($client !== null && $client->getOptions()->shouldSendDefaultPii()) {
28-
app('sentry')->configureScope(static function (Scope $scope) use ($request): void {
29+
$sentry->configureScope(static function (Scope $scope) use ($request): void {
2930
$scope->setUser([
3031
'ip_address' => $request->ip(),
3132
]);

src/Sentry/Laravel/LogChannel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Monolog\Logger;
66
use Illuminate\Log\LogManager;
7+
use Sentry\State\HubInterface;
78

89
class LogChannel extends LogManager
910
{
@@ -15,7 +16,7 @@ class LogChannel extends LogManager
1516
public function __invoke(array $config): Logger
1617
{
1718
$handler = new SentryHandler(
18-
$this->app->make('sentry'),
19+
$this->app->make(HubInterface::class),
1920
$config['level'] ?? Logger::DEBUG,
2021
$config['bubble'] ?? true
2122
);

src/Sentry/Laravel/ServiceProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ServiceProvider extends BaseServiceProvider
3737
*/
3838
public function boot(): void
3939
{
40-
$this->app->make(static::$abstract);
40+
$this->app->make(HubInterface::class);
4141

4242
if ($this->hasDsnSet()) {
4343
$this->bindEvents($this->app);
@@ -71,7 +71,7 @@ public function boot(): void
7171
public function register(): void
7272
{
7373
if ($this->app instanceof Lumen) {
74-
$this->app->configure('sentry');
74+
$this->app->configure(static::$abstract);
7575
}
7676

7777
$this->mergeConfigFrom(__DIR__ . '/../../../config/sentry.php', static::$abstract);
@@ -157,7 +157,7 @@ protected function configureAndRegisterClient(): void
157157
return $clientBuilder;
158158
});
159159

160-
$this->app->singleton(static::$abstract, function () {
160+
$this->app->singleton(HubInterface::class, function () {
161161
/** @var \Sentry\ClientBuilderInterface $clientBuilder */
162162
$clientBuilder = $this->app->make(ClientBuilderInterface::class);
163163

@@ -212,7 +212,7 @@ protected function configureAndRegisterClient(): void
212212
return $hub;
213213
});
214214

215-
$this->app->alias(static::$abstract, HubInterface::class);
215+
$this->app->alias(HubInterface::class, static::$abstract);
216216
}
217217

218218
/**

src/Sentry/Laravel/TestCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Console\Command;
77
use Sentry\ClientBuilder;
88
use Sentry\State\Hub;
9+
use Sentry\State\HubInterface;
910
use Sentry\Tracing\SpanContext;
1011
use Sentry\Tracing\TransactionContext;
1112

@@ -43,8 +44,8 @@ public function handle()
4344
$old_error_reporting = error_reporting(E_ALL | E_STRICT);
4445

4546
try {
46-
/** @var \Sentry\State\Hub $hub */
47-
$hub = app('sentry');
47+
/** @var \Sentry\State\HubInterface $hub */
48+
$hub = app(HubInterface::class);
4849

4950
if ($this->option('dsn')) {
5051
$hub = new Hub(ClientBuilder::create(['dsn' => $this->option('dsn')])->getClient());

src/Sentry/Laravel/Tracing/Middleware.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class Middleware
3939
*/
4040
public function handle($request, Closure $next)
4141
{
42-
if (app()->bound('sentry')) {
43-
$this->startTransaction($request, app('sentry'));
42+
if (app()->bound(HubInterface::class)) {
43+
$this->startTransaction($request, app(HubInterface::class));
4444
}
4545

4646
return $next($request);
@@ -56,7 +56,7 @@ public function handle($request, Closure $next)
5656
*/
5757
public function terminate($request, $response): void
5858
{
59-
if ($this->transaction !== null && app()->bound('sentry')) {
59+
if ($this->transaction !== null && app()->bound(HubInterface::class)) {
6060
if ($this->appSpan !== null) {
6161
$this->appSpan->finish();
6262
}

0 commit comments

Comments
 (0)