Skip to content

Commit 8d90388

Browse files
committed
Avoid any usage of Hub::(s|g)etCurrent
1 parent 5b6fc90 commit 8d90388

File tree

7 files changed

+39
-12
lines changed

7 files changed

+39
-12
lines changed

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ parameters:
99
- '/Parameter \$.+ of method Sentry\\SentryBundle\\EventListener\\ErrorListener::onConsoleException\(\) has invalid typehint type Symfony\\Component\\Console\\Event\\ConsoleExceptionEvent./'
1010
- '/Call to method getException\(\) on an unknown class Symfony\\Component\\Console\\Event\\ConsoleExceptionEvent./'
1111
- '/Access to undefined constant Symfony\\Component\\Console\\ConsoleEvents::EXCEPTION./'
12+
- '/Sentry\\SentrySdk/'
1213

1314
includes:
1415
- vendor/jangregor/phpstan-prophecy/src/extension.neon

src/Command/SentryTestCommand.php

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

33
namespace Sentry\SentryBundle\Command;
44

5-
use Sentry\State\Hub;
5+
use Sentry\SentryBundle\SentryBundle;
66
use Symfony\Component\Console\Command\Command;
77
use Symfony\Component\Console\Input\InputInterface;
88
use Symfony\Component\Console\Output\OutputInterface;
@@ -16,7 +16,7 @@ public function __construct()
1616

1717
protected function execute(InputInterface $input, OutputInterface $output): int
1818
{
19-
$currentHub = Hub::getCurrent();
19+
$currentHub = SentryBundle::getCurrentHub();
2020
$client = $currentHub->getClient();
2121

2222
if (! $client) {

src/EventListener/ConsoleListener.php

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

33
namespace Sentry\SentryBundle\EventListener;
44

5+
use Sentry\SentryBundle\SentryBundle;
56
use Sentry\State\Hub;
67
use Sentry\State\HubInterface;
78
use Sentry\State\Scope;
@@ -33,7 +34,7 @@ public function onConsoleCommand(ConsoleCommandEvent $event): void
3334
{
3435
$command = $event->getCommand();
3536

36-
Hub::getCurrent()
37+
SentryBundle::getCurrentHub()
3738
->configureScope(function (Scope $scope) use ($command): void {
3839
$scope->setTag('command', $command ? $command->getName() : 'N/A');
3940
});

src/EventListener/RequestListener.php

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

33
namespace Sentry\SentryBundle\EventListener;
44

5+
use Sentry\SentryBundle\SentryBundle;
56
use Sentry\State\Hub;
67
use Sentry\State\HubInterface;
78
use Sentry\State\Scope;
@@ -54,7 +55,7 @@ public function onKernelRequest(GetResponseEvent $event): void
5455
return;
5556
}
5657

57-
$currentClient = Hub::getCurrent()->getClient();
58+
$currentClient = SentryBundle::getCurrentHub()->getClient();
5859
if (null === $currentClient || ! $currentClient->getOptions()->shouldSendDefaultPii()) {
5960
return;
6061
}
@@ -76,7 +77,7 @@ public function onKernelRequest(GetResponseEvent $event): void
7677

7778
$userData['ip_address'] = $event->getRequest()->getClientIp();
7879

79-
Hub::getCurrent()
80+
SentryBundle::getCurrentHub()
8081
->configureScope(function (Scope $scope) use ($userData): void {
8182
$scope->setUser($userData);
8283
});
@@ -94,7 +95,7 @@ public function onKernelController(FilterControllerEvent $event): void
9495

9596
$matchedRoute = (string) $event->getRequest()->attributes->get('_route');
9697

97-
Hub::getCurrent()
98+
SentryBundle::getCurrentHub()
9899
->configureScope(function (Scope $scope) use ($matchedRoute): void {
99100
$scope->setTag('route', $matchedRoute);
100101
});

src/EventListener/SubRequestListener.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Sentry\SentryBundle\EventListener;
44

5-
use Sentry\State\Hub;
5+
use Sentry\SentryBundle\SentryBundle;
66
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
77
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
88

@@ -19,7 +19,7 @@ public function onKernelRequest(GetResponseEvent $event): void
1919
return;
2020
}
2121

22-
Hub::getCurrent()->pushScope();
22+
SentryBundle::getCurrentHub()->pushScope();
2323
}
2424

2525
/**
@@ -33,6 +33,6 @@ public function onKernelFinishRequest(FinishRequestEvent $event): void
3333
return;
3434
}
3535

36-
Hub::getCurrent()->popScope();
36+
SentryBundle::getCurrentHub()->popScope();
3737
}
3838
}

src/Resources/config/services.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
<service id="Sentry\State\HubInterface" class="Sentry\State\Hub" public="false">
1919
<argument type="service" id="Sentry\ClientInterface" />
20-
<call method="setCurrent">
21-
<argument type="service" id="Sentry\State\HubInterface" />
22-
</call>
2320
</service>
2421

2522
<service id="Sentry\SentryBundle\EventListener\ConsoleListener" class="Sentry\SentryBundle\EventListener\ConsoleListener" public="false">

src/SentryBundle.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Sentry\SentryBundle;
44

55
use Jean85\PrettyVersions;
6+
use Sentry\SentrySdk;
7+
use Sentry\State\Hub;
8+
use Sentry\State\HubInterface;
69
use Symfony\Component\HttpKernel\Bundle\Bundle;
710

811
class SentryBundle extends Bundle
@@ -14,4 +17,28 @@ public static function getSdkVersion(): string
1417
return PrettyVersions::getVersion('sentry/sentry-symfony')
1518
->getPrettyVersion();
1619
}
20+
21+
/**
22+
* This method avoids deprecations with sentry/sentry:^2.2
23+
*/
24+
public static function getCurrentHub(): HubInterface
25+
{
26+
if (class_exists(SentrySdk::class)) {
27+
return SentrySdk::getCurrentHub();
28+
}
29+
30+
return Hub::getCurrent();
31+
}
32+
33+
/**
34+
* This method avoids deprecations with sentry/sentry:^2.2
35+
*/
36+
public static function setCurrentHub(HubInterface $hub): void
37+
{
38+
if (class_exists(SentrySdk::class)) {
39+
SentrySdk::setCurrentHub($hub);
40+
}
41+
42+
Hub::setCurrent($hub);
43+
}
1744
}

0 commit comments

Comments
 (0)