Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased
- Add support for Symfony 5.0 (#266, thanks to @Big-Shark)
- Drop support for Symfony < 3.4 (#277)
- Fix handling of command with no name on `ConsoleListener` (#261)
- Remove check by AuthorizationChecker in `RequestListener` (#264)
Expand Down
22 changes: 11 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
"php": "^7.1",
"jean85/pretty-package-versions": "^1.0",
"sentry/sdk": "^2.0",
"symfony/config": "^3.4||^4.0",
"symfony/console": "^3.4||^4.0",
"symfony/dependency-injection": "^3.4||^4.0",
"symfony/event-dispatcher": "^3.4||^4.0",
"symfony/http-kernel": "^3.4||^4.0",
"symfony/security-core": "^3.4||^4.0"
"symfony/config": "^3.4||^4.0||^5.0",
"symfony/console": "^3.4||^4.0||^5.0",
"symfony/dependency-injection": "^3.4||^4.0||^5.0",
"symfony/event-dispatcher": "^3.4||^4.0||^5.0",
"symfony/http-kernel": "^3.4||^4.0||^5.0",
"symfony/security-core": "^3.4||^4.0||^5.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.8",
Expand All @@ -36,13 +36,13 @@
"php-http/mock-client": "^1.0",
"phpstan/phpstan-shim": "^0.11",
"phpstan/phpstan-phpunit": "^0.11",
"phpunit/phpunit": "^7.5",
"symfony/browser-kit": "^3.4||^4.0",
"symfony/expression-language": "^3.4||^4.0",
"symfony/framework-bundle": "^3.4||^4.0",
"phpunit/phpunit": "^7.5||^8.5",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this, and let symfony/phpunit-bridge pick the right version ./vendor/bin/simple-phpunit

"symfony/browser-kit": "^3.4||^4.0||^5.0",
"symfony/expression-language": "^3.4||^4.0||^5.0",
"symfony/framework-bundle": "^3.4||^4.0||^5.0",
"symfony/monolog-bundle": "^3.4",
"symfony/phpunit-bridge": "^5.0",
"symfony/yaml": "^3.4||^4.0"
"symfony/yaml": "^3.4||^4.0||^5.0"
},
"suggest": {
"monolog/monolog": "Required to use the Monolog handler"
Expand Down
3 changes: 2 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/7.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.5/phpunit.xsd"
colors="true"
backupGlobals="false"
bootstrap="vendor/autoload.php"
cacheResult="false"
processIsolation="true"
>

Expand Down
20 changes: 5 additions & 15 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\HttpKernel\Kernel;

/**
* This is the class that validates and merges configuration from your app/config files
Expand Down Expand Up @@ -78,7 +77,7 @@ public function getConfigTreeBuilder(): TreeBuilder
$optionsChildNodes->arrayNode('in_app_exclude')
->defaultValue([
'%kernel.cache_dir%',
$this->getProjectRoot() . '/vendor',
'%kernel.project_dir%/vendor',
])
->prototype('scalar');
$optionsChildNodes->arrayNode('excluded_exceptions')
Expand All @@ -88,7 +87,7 @@ public function getConfigTreeBuilder(): TreeBuilder
$optionsChildNodes->arrayNode('integrations')
->prototype('scalar')
->validate()
->ifTrue(function ($value): bool {
->ifTrue(static function ($value): bool {
if (! is_string($value) && '' != $value) {
return true;
}
Expand All @@ -114,7 +113,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->defaultValue($defaultValues->getPrefixes())
->prototype('scalar');
$optionsChildNodes->scalarNode('project_root')
->defaultValue($this->getProjectRoot());
->defaultValue('%kernel.project_dir%');
$optionsChildNodes->scalarNode('release');
$optionsChildNodes->floatNode('sample_rate')
->min(0.0)
Expand Down Expand Up @@ -166,7 +165,7 @@ public function getConfigTreeBuilder(): TreeBuilder

private function getTrimClosure(): \Closure
{
return function ($str): ?string {
return static function ($str): ?string {
$value = trim($str);
if ($value === '') {
return null;
Expand All @@ -176,18 +175,9 @@ private function getTrimClosure(): \Closure
};
}

private function getProjectRoot(): string
{
if (method_exists(Kernel::class, 'getProjectDir')) {
return '%kernel.project_dir%';
}

return '%kernel.root_dir%/..';
}

private function isNotAValidCallback(): \Closure
{
return function ($value): bool {
return static function ($value): bool {
if (is_callable($value)) {
return false;
}
Expand Down
50 changes: 0 additions & 50 deletions src/DependencyInjection/SentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,16 @@
use Sentry\ClientBuilderInterface;
use Sentry\Monolog\Handler;
use Sentry\Options;
use Sentry\SentryBundle\Command\SentryTestCommand;
use Sentry\SentryBundle\ErrorTypesParser;
use Sentry\SentryBundle\EventListener\ConsoleListener;
use Sentry\SentryBundle\EventListener\ErrorListener;
use Sentry\SentryBundle\EventListener\RequestListener;
use Sentry\SentryBundle\EventListener\SubRequestListener;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelEvents;

/**
Expand Down Expand Up @@ -54,7 +47,6 @@ public function load(array $configs, ContainerBuilder $container): void
}

$this->configureErrorListener($container, $processedConfiguration);
$this->setLegacyVisibilities($container);
$this->configureMonologHandler($container, $processedConfiguration['monolog']);
}

Expand Down Expand Up @@ -150,7 +142,6 @@ private function configureErrorListener(ContainerBuilder $container, array $proc
}

$this->tagExceptionListener($container);
$this->tagConsoleErrorListener($container);
}

/**
Expand All @@ -172,47 +163,6 @@ private function tagExceptionListener(ContainerBuilder $container): void
$listener->addTag('kernel.event_listener', $tagAttributes);
}

/**
* BC layer for Symfony < 3.3; see https://symfony.com/blog/new-in-symfony-3-3-better-handling-of-command-exceptions
*/
private function tagConsoleErrorListener(ContainerBuilder $container): void
{
$listener = $container->getDefinition(ErrorListener::class);

if (class_exists(ConsoleErrorEvent::class)) {
$tagAttributes = [
'event' => ConsoleEvents::ERROR,
'method' => 'onConsoleError',
'priority' => '%sentry.listener_priorities.console_error%',
];
} else {
$tagAttributes = [
'event' => ConsoleEvents::EXCEPTION,
'method' => 'onConsoleException',
'priority' => '%sentry.listener_priorities.console_error%',
];
}

$listener->addTag('kernel.event_listener', $tagAttributes);
}

/**
* BC layer for symfony < 3.3, listeners and commands must be public
*/
private function setLegacyVisibilities(ContainerBuilder $container): void
{
if (Kernel::VERSION_ID < 30300) {
$container->getDefinition(SentryTestCommand::class)->setPublic(true);
$container->getDefinition(ConsoleListener::class)->setPublic(true);
$container->getDefinition(RequestListener::class)->setPublic(true);
$container->getDefinition(SubRequestListener::class)->setPublic(true);

if ($container->hasDefinition(ErrorListener::class)) {
$container->getDefinition(ErrorListener::class)->setPublic(true);
}
}
}

private function configureMonologHandler(ContainerBuilder $container, array $monologConfiguration): void
{
$errorHandler = $monologConfiguration['error_handler'];
Expand Down
9 changes: 0 additions & 9 deletions src/EventListener/ErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Sentry\State\HubInterface;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;

Expand Down Expand Up @@ -39,12 +38,4 @@ public function onConsoleError(ConsoleErrorEvent $event): void
{
\Sentry\captureException($event->getError());
}

/**
* BC layer for Symfony < 3.3; see https://symfony.com/blog/new-in-symfony-3-3-better-handling-of-command-exceptions
*/
public function onConsoleException(ConsoleExceptionEvent $event): void
{
\Sentry\captureException($event->getException());
}
}
16 changes: 13 additions & 3 deletions src/EventListener/RequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@
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\RequestEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\User\UserInterface;

if (! class_exists(RequestEvent::class)) {
class_alias(GetResponseEvent::class, RequestEvent::class);
}

if (! class_exists(ControllerEvent::class)) {
class_alias(FilterControllerEvent::class, ControllerEvent::class);
}

/**
* Class RequestListener
* @package Sentry\SentryBundle\EventListener
Expand Down Expand Up @@ -38,9 +48,9 @@ public function __construct(
/**
* Set the username from the security context by listening on core.request
*
* @param GetResponseEvent $event
* @param RequestEvent $event
*/
public function onKernelRequest(GetResponseEvent $event): void
public function onKernelRequest(RequestEvent $event): void
{
if (! $event->isMasterRequest()) {
return;
Expand Down Expand Up @@ -75,7 +85,7 @@ public function onKernelRequest(GetResponseEvent $event): void
});
}

public function onKernelController(FilterControllerEvent $event): void
public function onKernelController(ControllerEvent $event): void
{
if (! $event->isMasterRequest()) {
return;
Expand Down
9 changes: 7 additions & 2 deletions src/EventListener/SubRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
use Sentry\SentryBundle\SentryBundle;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;

if (! class_exists(RequestEvent::class)) {
class_alias(GetResponseEvent::class, RequestEvent::class);
}

final class SubRequestListener
{
/**
* Pushes a new {@see Scope} for each SubRequest
*
* @param GetResponseEvent $event
* @param RequestEvent $event
*/
public function onKernelRequest(GetResponseEvent $event): void
public function onKernelRequest(RequestEvent $event): void
{
if ($event->isMasterRequest()) {
return;
Expand Down
3 changes: 1 addition & 2 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
<argument type="service" id="Sentry\State\HubInterface" />
<!-- The following tag is done manually in PHP for BC with Symfony < 4.3, -->
<!-- <tag name="kernel.event_listener" event="kernel.exception" method="onException" priority="%sentry.listener_priorities.request_error%" />-->
<!-- The following tag is done manually in PHP for BC with Symfony < 3.3 -->
<!-- <tag name="kernel.event_listener" event="console.error" method="onConsoleError" priority="%sentry.listener_priorities.console_error%" />-->
<tag name="kernel.event_listener" event="console.error" method="onConsoleError" priority="%sentry.listener_priorities.console_error%" />
</service>

<service id="Sentry\SentryBundle\EventListener\RequestListener" class="Sentry\SentryBundle\EventListener\RequestListener" public="false">
Expand Down
33 changes: 33 additions & 0 deletions test/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use Sentry\SentrySdk;
use Sentry\State\Hub;
use Sentry\State\HubInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelInterface;

abstract class BaseTestCase extends TestCase
{
Expand Down Expand Up @@ -43,4 +48,32 @@ protected function setCurrentHub(HubInterface $hub): void
Hub::setCurrent($hub);
}
}

/**
* @return GetResponseEvent|RequestEvent
*/
protected function createRequestEvent(Request $request = null, int $type = KernelInterface::MASTER_REQUEST)
{
if ($request === null) {
$request = $this->prophesize(Request::class)->reveal();
}

if (class_exists(RequestEvent::class)) {
$event = new RequestEvent(
$this->prophesize(KernelInterface::class)->reveal(),
$request,
$type,
$this->prophesize(Response::class)->reveal()
);
} else {
$event = new GetResponseEvent(
$this->prophesize(KernelInterface::class)->reveal(),
$request,
$type,
$this->prophesize(Response::class)->reveal()
);
}

return $event;
}
}
10 changes: 2 additions & 8 deletions test/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Sentry\SentryBundle\Test\BaseTestCase;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\HttpKernel\Kernel;

class ConfigurationTest extends BaseTestCase
{
Expand Down Expand Up @@ -60,12 +59,12 @@ public function testConfigurationDefaults(): void
'environment' => '%kernel.environment%',
'in_app_exclude' => [
'%kernel.cache_dir%',
'%kernel.root_dir%/../vendor',
'%kernel.project_dir%/vendor',
],
'integrations' => $defaultSdkValues->getIntegrations(),
'excluded_exceptions' => $defaultSdkValues->getExcludedExceptions(),
'prefixes' => $defaultSdkValues->getPrefixes(),
'project_root' => '%kernel.root_dir%/..',
'project_root' => '%kernel.project_dir%',
'tags' => [],
],
'monolog' => [
Expand All @@ -77,11 +76,6 @@ public function testConfigurationDefaults(): void
],
];

if (method_exists(Kernel::class, 'getProjectDir')) {
$expectedDefaults['options']['project_root'] = '%kernel.project_dir%';
$expectedDefaults['options']['in_app_exclude'][1] = '%kernel.project_dir%/vendor';
}

if ($this->classSerializersAreSupported()) {
$expectedDefaults['options']['class_serializers'] = [];
}
Expand Down
1 change: 0 additions & 1 deletion test/DependencyInjection/SentryExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ private function getContainer(array $configuration = []): Container
{
$containerBuilder = new ContainerBuilder();
$containerBuilder->setParameter('kernel.cache_dir', 'var/cache');
$containerBuilder->setParameter('kernel.root_dir', 'kernel/root');
if (method_exists(Kernel::class, 'getProjectDir')) {
$containerBuilder->setParameter('kernel.project_dir', '/dir/project/root');
}
Expand Down
Loading