Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit afa4a09

Browse files
committed
Use event subscriber instead of listener
1 parent 7d6b2e7 commit afa4a09

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
6464
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
6565
use Symfony\Component\HttpKernel\EventListener\DisallowRobotsIndexingListener;
66-
use Symfony\Component\HttpKernel\KernelEvents;
6766
use Symfony\Component\Lock\Factory;
6867
use Symfony\Component\Lock\Lock;
6968
use Symfony\Component\Lock\LockInterface;
@@ -398,8 +397,7 @@ public function load(array $configs, ContainerBuilder $container)
398397
}
399398

400399
if ($config['disallow_search_engine_index'] ?? false) {
401-
$container->register(DisallowRobotsIndexingListener::class)
402-
->addTag('kernel.event_listener', ['event' => KernelEvents::RESPONSE, 'priority' => -255]);
400+
$container->register(DisallowRobotsIndexingListener::class)->addTag('kernel.event_subscriber');
403401
}
404402
}
405403

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,11 +1336,7 @@ public function testRobotsTagListenerIsRegisteredInDebugMode()
13361336
$this->assertTrue($container->has(DisallowRobotsIndexingListener::class), 'DisallowRobotsIndexingListener should be registered');
13371337

13381338
$definition = $container->getDefinition(DisallowRobotsIndexingListener::class);
1339-
$this->assertTrue($definition->hasTag('kernel.event_listener'), 'DisallowRobotsIndexingListener should have the correct tag');
1340-
$this->assertSame(
1341-
[['event' => KernelEvents::RESPONSE, 'priority' => -255]],
1342-
$definition->getTag('kernel.event_listener')
1343-
);
1339+
$this->assertTrue($definition->hasTag('kernel.event_subscriber'), 'DisallowRobotsIndexingListener should have the correct tag');
13441340

13451341
$container = $this->createContainer(['kernel.debug' => true]);
13461342
(new FrameworkExtension())->load([['disallow_search_engine_index' => false]], $container);

src/Symfony/Component/HttpKernel/EventListener/DisallowRobotsIndexingListener.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111

1212
namespace Symfony\Component\HttpKernel\EventListener;
1313

14+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1415
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
16+
use Symfony\Component\HttpKernel\KernelEvents;
1517

1618
/**
1719
* When enabled, ensures that the application is not indexed by search engines.
1820
*
1921
* @author Gary PEGEOT <[email protected]>
2022
*/
21-
class DisallowRobotsIndexingListener
23+
class DisallowRobotsIndexingListener implements EventSubscriberInterface
2224
{
2325
private const HEADER_NAME = 'X-Robots-Tag';
2426

@@ -28,4 +30,14 @@ public function __invoke(FilterResponseEvent $event): void
2830
$event->getResponse()->headers->set(static::HEADER_NAME, 'noindex');
2931
}
3032
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public static function getSubscribedEvents()
38+
{
39+
return [
40+
KernelEvents::RESPONSE => ['__invoke', -255],
41+
];
42+
}
3143
}

0 commit comments

Comments
 (0)