Skip to content

Commit 475a958

Browse files
authored
Allow to install the bundle without Symfony security (#912)
1 parent fd43dd9 commit 475a958

File tree

6 files changed

+41
-5
lines changed

6 files changed

+41
-5
lines changed

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
"symfony/event-dispatcher": "^4.4.20||^5.0.11||^6.0||^7.0",
2424
"symfony/http-kernel": "^4.4.20||^5.0.11||^6.0||^7.0",
2525
"symfony/polyfill-php80": "^1.22",
26-
"symfony/psr-http-message-bridge": "^1.2||^2.0||^6.4||^7.0",
27-
"symfony/security-core": "^4.4.20||^5.0.11||^6.0||^7.0",
28-
"symfony/security-http": "^4.4.20||^5.0.11||^6.0||^7.0"
26+
"symfony/psr-http-message-bridge": "^1.2||^2.0||^6.4||^7.0"
2927
},
3028
"require-dev": {
3129
"doctrine/dbal": "^2.13||^3.3||^4.0",
@@ -46,6 +44,8 @@
4644
"symfony/monolog-bundle": "^3.4",
4745
"symfony/phpunit-bridge": "^5.2.6||^6.0||^7.0",
4846
"symfony/process": "^4.4.20||^5.0.11||^6.0||^7.0",
47+
"symfony/security-core": "^4.4.20||^5.0.11||^6.0||^7.0",
48+
"symfony/security-http": "^4.4.20||^5.0.11||^6.0||^7.0",
4949
"symfony/twig-bundle": "^4.4.20||^5.0.11||^6.0||^7.0",
5050
"symfony/yaml": "^4.4.20||^5.0.11||^6.0||^7.0",
5151
"vimeo/psalm": "^4.3||^5.16.0"

src/DependencyInjection/Compiler/AddLoginListenerTagPass.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,17 @@ final class AddLoginListenerTagPass implements CompilerPassInterface
1717
*/
1818
public function process(ContainerBuilder $container): void
1919
{
20+
if (!$container->hasDefinition(LoginListener::class)) {
21+
return;
22+
}
2023
$listenerDefinition = $container->getDefinition(LoginListener::class);
2124

22-
if (!class_exists(LoginSuccessEvent::class)) {
25+
if (class_exists(LoginSuccessEvent::class)) {
26+
$listenerDefinition->addTag('kernel.event_listener', [
27+
'event' => LoginSuccessEvent::class,
28+
'method' => 'handleLoginSuccessEvent',
29+
]);
30+
} elseif (class_exists(AuthenticationSuccessEvent::class)) {
2331
$listenerDefinition->addTag('kernel.event_listener', [
2432
'event' => AuthenticationSuccessEvent::class,
2533
'method' => 'handleAuthenticationSuccessEvent',

src/DependencyInjection/SentryExtension.php

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Sentry\Options;
1616
use Sentry\SentryBundle\EventListener\ConsoleListener;
1717
use Sentry\SentryBundle\EventListener\ErrorListener;
18+
use Sentry\SentryBundle\EventListener\LoginListener;
1819
use Sentry\SentryBundle\EventListener\MessengerListener;
1920
use Sentry\SentryBundle\EventListener\TracingConsoleListener;
2021
use Sentry\SentryBundle\EventListener\TracingRequestListener;
@@ -34,6 +35,7 @@
3435
use Symfony\Component\DependencyInjection\Reference;
3536
use Symfony\Component\HttpClient\HttpClient;
3637
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
38+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
3739

3840
final class SentryExtension extends ConfigurableExtension
3941
{
@@ -75,6 +77,10 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
7577
$this->registerTwigTracingConfiguration($container, $mergedConfig['tracing']);
7678
$this->registerCacheTracingConfiguration($container, $mergedConfig['tracing']);
7779
$this->registerHttpClientTracingConfiguration($container, $mergedConfig['tracing']);
80+
81+
if (!interface_exists(TokenStorageInterface::class)) {
82+
$container->removeDefinition(LoginListener::class);
83+
}
7884
}
7985

8086
/**

src/Resources/config/services.xml

-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
<argument type="service" id="security.token_storage" on-invalid="ignore" />
8484

8585
<tag name="kernel.event_listener" event="kernel.request" method="handleKernelRequestEvent" />
86-
<tag name="kernel.event_listener" event="Symfony\Component\Security\Http\Event\LoginSuccessEvent" method="handleLoginSuccessEvent" />
8786
</service>
8887

8988
<service id="Sentry\SentryBundle\Command\SentryTestCommand" class="Sentry\SentryBundle\Command\SentryTestCommand">

tests/DependencyInjection/Compiler/AddLoginListenerTagPassTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,20 @@ public function testProcess(): void
2828

2929
$this->assertSame([['event' => AuthenticationSuccessEvent::class, 'method' => 'handleAuthenticationSuccessEvent']], $listenerDefinition->getTag('kernel.event_listener'));
3030
}
31+
32+
public function testProcessLoginSuccess(): void
33+
{
34+
if (!class_exists(LoginSuccessEvent::class)) {
35+
$this->markTestSkipped('Skipping this test because LoginSuccessEvent does not exist.');
36+
}
37+
38+
$container = new ContainerBuilder();
39+
$container->register(LoginListener::class)->setPublic(true);
40+
$container->addCompilerPass(new AddLoginListenerTagPass());
41+
$container->compile();
42+
43+
$listenerDefinition = $container->getDefinition(LoginListener::class);
44+
45+
$this->assertSame([['event' => LoginSuccessEvent::class, 'method' => 'handleLoginSuccessEvent']], $listenerDefinition->getTag('kernel.event_listener'));
46+
}
3147
}

tests/DependencyInjection/SentryExtensionTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Sentry\SentryBundle\DependencyInjection\SentryExtension;
1515
use Sentry\SentryBundle\EventListener\ConsoleListener;
1616
use Sentry\SentryBundle\EventListener\ErrorListener;
17+
use Sentry\SentryBundle\EventListener\LoginListener;
1718
use Sentry\SentryBundle\EventListener\MessengerListener;
1819
use Sentry\SentryBundle\EventListener\RequestListener;
1920
use Sentry\SentryBundle\EventListener\SubRequestListener;
@@ -418,6 +419,12 @@ public function testLoggerOptionFallbackToNullLoggerIfNotSet(): void
418419
$this->assertDefinitionMethodCallAt($methodCalls[3], 'setLogger', [new Reference(NullLogger::class, ContainerBuilder::IGNORE_ON_INVALID_REFERENCE)]);
419420
}
420421

422+
public function testLoginListener(): void
423+
{
424+
$container = $this->createContainerFromFixture('full');
425+
$this->assertTrue($container->hasDefinition(LoginListener::class));
426+
}
427+
421428
/**
422429
* @dataProvider releaseOptionDataProvider
423430
*/

0 commit comments

Comments
 (0)