Skip to content

Commit cb49aba

Browse files
authored
Merge pull request #244 from getsentry/sdk-2.2-compat
Sentry/sentry 2.2 compat
2 parents 9c1471e + f6dfa64 commit cb49aba

File tree

11 files changed

+81
-25
lines changed

11 files changed

+81
-25
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ jobs:
3131
- composer remove --dev friendsofphp/php-cs-fixer phpstan/phpstan --no-update
3232
- travis_retry travis_wait composer install --no-interaction --prefer-dist
3333
- travis_retry travis_wait composer update --no-interaction --prefer-dist --prefer-stable --prefer-lowest
34+
- name: sentry/sentry dev-develop
35+
install: composer require sentry/sentry:dev-develop
3436
- stage: Code style and static analysis
3537
script:
3638
- composer phpstan

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88
- Add forward compatibility with Symfony 5 (#235, thanks to @garak)
9+
- Fix compatibility with sentry/sentry 2.2+ (#244)
910

1011
## 3.1.0 - 2019-07-02
1112
- Add support for Symfony 2.8 (#233, thanks to @nocive)

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
}

test/EventListener/ConsoleListenerTest.php

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

55
use PHPUnit\Framework\TestCase;
66
use Prophecy\Argument;
7+
use Sentry\Event;
78
use Sentry\SentryBundle\EventListener\ConsoleListener;
89
use Sentry\State\Hub;
910
use Sentry\State\HubInterface;
@@ -47,7 +48,7 @@ public function testOnConsoleCommandAddsCommandName(): void
4748

4849
$listener->onConsoleCommand($event->reveal());
4950

50-
$this->assertSame(['command' => 'sf:command:name'], $this->currentScope->getTags());
51+
$this->assertSame(['command' => 'sf:command:name'], $this->getTagsContext($this->currentScope));
5152
}
5253

5354
public function testOnConsoleCommandAddsPlaceholderCommandName(): void
@@ -60,6 +61,14 @@ public function testOnConsoleCommandAddsPlaceholderCommandName(): void
6061

6162
$listener->onConsoleCommand($event->reveal());
6263

63-
$this->assertSame(['command' => 'N/A'], $this->currentScope->getTags());
64+
$this->assertSame(['command' => 'N/A'], $this->getTagsContext($this->currentScope));
65+
}
66+
67+
private function getTagsContext(Scope $scope): array
68+
{
69+
$event = new Event();
70+
$scope->applyToEvent($event, []);
71+
72+
return $event->getTagsContext()->toArray();
6473
}
6574
}

0 commit comments

Comments
 (0)