Skip to content

Remove deprecated listener events #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/EventListener/ErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public function __construct(HubInterface $hub)
$this->hub = $hub; // not used, needed to trigger instantiation
}

public function onException(ExceptionEvent $event): void
public function onException(ErrorListenerExceptionEvent $event): void
{
\Sentry\captureException($event->getThrowable());
}

/**
* BC layer for Symfony < 4.3
*/
public function onKernelException(GetResponseForExceptionEvent $event): void
public function onKernelException(ErrorListenerExceptionEvent $event): void
{
\Sentry\captureException($event->getException());
}
Expand All @@ -48,3 +48,9 @@ public function onConsoleException(ConsoleExceptionEvent $event): void
\Sentry\captureException($event->getException());
}
}

if (\class_exists(ExceptionEvent::class)) {
\class_alias(ExceptionEvent::class, ErrorListenerExceptionEvent::class);
} else {
\class_alias(GetResponseForExceptionEvent::class, ErrorListenerExceptionEvent::class);
}
20 changes: 17 additions & 3 deletions src/EventListener/RequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use Sentry\SentryBundle\SentryBundle;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\User\UserInterface;

Expand Down Expand Up @@ -38,9 +40,9 @@ public function __construct(
/**
* Set the username from the security context by listening on core.request
*
* @param GetResponseEvent $event
* @param RequestListenerResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event): void
public function onKernelRequest(RequestListenerResponseEvent $event): void
{
if (! $event->isMasterRequest()) {
return;
Expand Down Expand Up @@ -75,7 +77,7 @@ public function onKernelRequest(GetResponseEvent $event): void
});
}

public function onKernelController(FilterControllerEvent $event): void
public function onKernelController(RequestListenerControllerEvent $event): void
{
if (! $event->isMasterRequest()) {
return;
Expand Down Expand Up @@ -119,3 +121,15 @@ private function getUserData($user): array
return [];
}
}

if (\class_exists(ResponseEvent::class)) {
\class_alias(ResponseEvent::class, RequestListenerResponseEvent::class);
} else {
\class_alias(GetResponseEvent::class, RequestListenerResponseEvent::class);
}

if (\class_exists(ControllerEvent::class)) {
\class_alias(ControllerEvent::class, RequestListenerControllerEvent::class);
} else {
\class_alias(FilterControllerEvent::class, RequestListenerControllerEvent::class);
}
11 changes: 9 additions & 2 deletions src/EventListener/SubRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
use Sentry\SentryBundle\SentryBundle;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;

final class SubRequestListener
{
/**
* Pushes a new {@see Scope} for each SubRequest
*
* @param GetResponseEvent $event
* @param SubRequestListenerResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event): void
public function onKernelRequest(SubRequestListenerResponseEvent $event): void
{
if ($event->isMasterRequest()) {
return;
Expand All @@ -36,3 +37,9 @@ public function onKernelFinishRequest(FinishRequestEvent $event): void
SentryBundle::getCurrentHub()->popScope();
}
}

if (\class_exists(ResponseEvent::class)) {
\class_alias(ResponseEvent::class, SubRequestListenerResponseEvent::class);
} else {
\class_alias(GetResponseEvent::class, SubRequestListenerResponseEvent::class);
}
22 changes: 12 additions & 10 deletions test/EventListener/RequestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\User\UserInterface;
Expand Down Expand Up @@ -56,7 +58,7 @@ protected function setUp()
public function testOnKernelRequestUserDataIsSetToScope($user): void
{
$tokenStorage = $this->prophesize(TokenStorageInterface::class);
$event = $this->prophesize(GetResponseEvent::class);
$event = $this->prophesize(\class_exists(ResponseEvent::class) ? ResponseEvent::class : GetResponseEvent::class);
$request = $this->prophesize(Request::class);
$token = $this->prophesize(TokenInterface::class);

Expand Down Expand Up @@ -101,7 +103,7 @@ public function userDataProvider(): \Generator
public function testOnKernelRequestUserDataIsNotSetIfSendPiiIsDisabled(): void
{
$tokenStorage = $this->prophesize(TokenStorageInterface::class);
$event = $this->prophesize(GetResponseEvent::class);
$event = $this->prophesize(\class_exists(ResponseEvent::class) ? ResponseEvent::class : GetResponseEvent::class);

$event->isMasterRequest()
->willReturn(true);
Expand All @@ -124,7 +126,7 @@ public function testOnKernelRequestUserDataIsNotSetIfSendPiiIsDisabled(): void
public function testOnKernelRequestUserDataIsNotSetIfNoClientIsPresent(): void
{
$tokenStorage = $this->prophesize(TokenStorageInterface::class);
$event = $this->prophesize(GetResponseEvent::class);
$event = $this->prophesize(\class_exists(ResponseEvent::class) ? ResponseEvent::class : GetResponseEvent::class);

$event->isMasterRequest()
->willReturn(true);
Expand All @@ -146,7 +148,7 @@ public function testOnKernelRequestUserDataIsNotSetIfNoClientIsPresent(): void

public function testOnKernelRequestUsernameIsNotSetIfTokenStorageIsAbsent(): void
{
$event = $this->prophesize(GetResponseEvent::class);
$event = $this->prophesize(\class_exists(ResponseEvent::class) ? ResponseEvent::class : GetResponseEvent::class);
$request = $this->prophesize(Request::class);

$event->isMasterRequest()
Expand All @@ -173,7 +175,7 @@ public function testOnKernelRequestUsernameIsNotSetIfTokenStorageIsAbsent(): voi
public function testOnKernelRequestUsernameIsNotSetIfTokenIsAbsent(): void
{
$tokenStorage = $this->prophesize(TokenStorageInterface::class);
$event = $this->prophesize(GetResponseEvent::class);
$event = $this->prophesize(\class_exists(ResponseEvent::class) ? ResponseEvent::class : GetResponseEvent::class);
$request = $this->prophesize(Request::class);

$event->isMasterRequest()
Expand Down Expand Up @@ -207,7 +209,7 @@ public function testOnKernelRequestUsernameIsNotSetIfTokenIsNotAuthenticated():
{
$tokenStorage = $this->prophesize(TokenStorageInterface::class);
$token = $this->prophesize(TokenInterface::class);
$event = $this->prophesize(GetResponseEvent::class);
$event = $this->prophesize(\class_exists(ResponseEvent::class) ? ResponseEvent::class : GetResponseEvent::class);
$request = $this->prophesize(Request::class);

$event->isMasterRequest()
Expand Down Expand Up @@ -240,7 +242,7 @@ public function testOnKernelRequestUsernameIsNotSetIfTokenIsNotAuthenticated():
public function testOnKernelRequestUsernameIsNotSetIfUserIsNotRemembered(): void
{
$tokenStorage = $this->prophesize(TokenStorageInterface::class);
$event = $this->prophesize(GetResponseEvent::class);
$event = $this->prophesize(\class_exists(ResponseEvent::class) ? ResponseEvent::class : GetResponseEvent::class);
$request = $this->prophesize(Request::class);

$event->isMasterRequest()
Expand Down Expand Up @@ -271,7 +273,7 @@ public function testOnKernelControllerAddsRouteTag(): void
{
$request = new Request();
$request->attributes->set('_route', 'sf-route');
$event = $this->prophesize(FilterControllerEvent::class);
$event = $this->prophesize(\class_exists(ControllerEvent::class) ? ControllerEvent::class : FilterControllerEvent::class);

$event->isMasterRequest()
->willReturn(true);
Expand All @@ -294,7 +296,7 @@ public function testOnKernelControllerRouteTagIsNotSetIfRequestDoesNotHaveARoute
->shouldNotBeCalled();

$request = new Request();
$event = $this->prophesize(FilterControllerEvent::class);
$event = $this->prophesize(\class_exists(ControllerEvent::class) ? ControllerEvent::class : FilterControllerEvent::class);

$event->isMasterRequest()
->willReturn(true);
Expand All @@ -315,7 +317,7 @@ public function testOnKernelRequestUserDataAndTagsAreNotSetInSubRequest(): void
->shouldNotBeCalled();

$tokenStorage = $this->prophesize(TokenStorageInterface::class);
$event = $this->prophesize(GetResponseEvent::class);
$event = $this->prophesize(\class_exists(ResponseEvent::class) ? ResponseEvent::class : GetResponseEvent::class);

$event->isMasterRequest()
->willReturn(false);
Expand Down
5 changes: 3 additions & 2 deletions test/EventListener/SubRequestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Sentry\State\Scope;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;

class SubRequestListenerTest extends BaseTestCase
{
Expand All @@ -26,7 +27,7 @@ public function testOnKernelRequestWithMasterRequest(): void
{
$listener = new SubRequestListener();

$subRequestEvent = $this->prophesize(GetResponseEvent::class);
$subRequestEvent = $this->prophesize(\class_exists(ResponseEvent::class) ? ResponseEvent::class : GetResponseEvent::class);
$subRequestEvent->isMasterRequest()
->willReturn(true);

Expand All @@ -40,7 +41,7 @@ public function testOnKernelRequestWithSubRequest(): void
{
$listener = new SubRequestListener();

$subRequestEvent = $this->prophesize(GetResponseEvent::class);
$subRequestEvent = $this->prophesize(\class_exists(ResponseEvent::class) ? ResponseEvent::class : GetResponseEvent::class);
$subRequestEvent->isMasterRequest()
->willReturn(false);

Expand Down