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
}

test/EventListener/RequestListenerTest.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\TestCase;
66
use Prophecy\Argument;
77
use Sentry\ClientInterface;
8+
use Sentry\Event;
89
use Sentry\Options;
910
use Sentry\SentryBundle\EventListener\RequestListener;
1011
use Sentry\State\Hub;
@@ -94,7 +95,7 @@ public function testOnKernelRequestUserDataIsSetToScope($user): void
9495
'ip_address' => '1.2.3.4',
9596
'username' => 'john-doe',
9697
];
97-
$this->assertEquals($expectedUserData, $this->currentScope->getUser());
98+
$this->assertEquals($expectedUserData, $this->getUserContext($this->currentScope));
9899
}
99100

100101
public function userDataProvider(): \Generator
@@ -131,7 +132,7 @@ public function testOnKernelRequestUserDataIsNotSetIfSendPiiIsDisabled(): void
131132

132133
$listener->onKernelRequest($event->reveal());
133134

134-
$this->assertEquals([], $this->currentScope->getUser());
135+
$this->assertEquals([], $this->getUserContext($this->currentScope));
135136
}
136137

137138
public function testOnKernelRequestUserDataIsNotSetIfNoClientIsPresent(): void
@@ -156,7 +157,7 @@ public function testOnKernelRequestUserDataIsNotSetIfNoClientIsPresent(): void
156157

157158
$listener->onKernelRequest($event->reveal());
158159

159-
$this->assertEquals([], $this->currentScope->getUser());
160+
$this->assertEquals([], $this->getUserContext($this->currentScope));
160161
}
161162

162163
public function testOnKernelRequestUsernameIsNotSetIfTokenStorageIsAbsent(): void
@@ -187,7 +188,7 @@ public function testOnKernelRequestUsernameIsNotSetIfTokenStorageIsAbsent(): voi
187188
$expectedUserData = [
188189
'ip_address' => '1.2.3.4',
189190
];
190-
$this->assertEquals($expectedUserData, $this->currentScope->getUser());
191+
$this->assertEquals($expectedUserData, $this->getUserContext($this->currentScope));
191192
}
192193

193194
public function testOnKernelRequestUsernameIsNotSetIfAuthorizationCheckerIsAbsent(): void
@@ -218,7 +219,7 @@ public function testOnKernelRequestUsernameIsNotSetIfAuthorizationCheckerIsAbsen
218219
$expectedUserData = [
219220
'ip_address' => '1.2.3.4',
220221
];
221-
$this->assertEquals($expectedUserData, $this->currentScope->getUser());
222+
$this->assertEquals($expectedUserData, $this->getUserContext($this->currentScope));
222223
}
223224

224225
public function testOnKernelRequestUsernameIsNotSetIfTokenIsAbsent(): void
@@ -253,7 +254,7 @@ public function testOnKernelRequestUsernameIsNotSetIfTokenIsAbsent(): void
253254
$expectedUserData = [
254255
'ip_address' => '1.2.3.4',
255256
];
256-
$this->assertEquals($expectedUserData, $this->currentScope->getUser());
257+
$this->assertEquals($expectedUserData, $this->getUserContext($this->currentScope));
257258
}
258259

259260
/**
@@ -295,7 +296,7 @@ public function testOnKernelRequestUsernameIsNotSetIfTokenIsNotAuthenticated():
295296
$expectedUserData = [
296297
'ip_address' => '1.2.3.4',
297298
];
298-
$this->assertEquals($expectedUserData, $this->currentScope->getUser());
299+
$this->assertEquals($expectedUserData, $this->getUserContext($this->currentScope));
299300
}
300301

301302
public function testOnKernelRequestUsernameIsNotSetIfUserIsNotRemembered(): void
@@ -330,7 +331,7 @@ public function testOnKernelRequestUsernameIsNotSetIfUserIsNotRemembered(): void
330331
$expectedUserData = [
331332
'ip_address' => '1.2.3.4',
332333
];
333-
$this->assertEquals($expectedUserData, $this->currentScope->getUser());
334+
$this->assertEquals($expectedUserData, $this->getUserContext($this->currentScope));
334335
}
335336

336337
public function testOnKernelControllerAddsRouteTag(): void
@@ -352,7 +353,7 @@ public function testOnKernelControllerAddsRouteTag(): void
352353

353354
$listener->onKernelController($event->reveal());
354355

355-
$this->assertSame(['route' => 'sf-route'], $this->currentScope->getTags());
356+
$this->assertSame(['route' => 'sf-route'], $this->getTagsContext($this->currentScope));
356357
}
357358

358359
public function testOnKernelControllerRouteTagIsNotSetIfRequestDoesNotHaveARoute(): void
@@ -403,8 +404,24 @@ public function testOnKernelRequestUserDataAndTagsAreNotSetInSubRequest(): void
403404

404405
$listener->onKernelRequest($event->reveal());
405406

406-
$this->assertEmpty($this->currentScope->getUser());
407-
$this->assertEmpty($this->currentScope->getTags());
407+
$this->assertEmpty($this->getUserContext($this->currentScope));
408+
$this->assertEmpty($this->getTagsContext($this->currentScope));
409+
}
410+
411+
private function getUserContext(Scope $scope): array
412+
{
413+
$event = new Event();
414+
$scope->applyToEvent($event, []);
415+
416+
return $event->getUserContext()->toArray();
417+
}
418+
419+
private function getTagsContext(Scope $scope): array
420+
{
421+
$event = new Event();
422+
$scope->applyToEvent($event, []);
423+
424+
return $event->getTagsContext()->toArray();
408425
}
409426
}
410427

0 commit comments

Comments
 (0)