Skip to content
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

Replace event subscribers with event listeners #315

Merged
merged 1 commit into from
Oct 9, 2023
Merged
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
2 changes: 1 addition & 1 deletion config/alternate_listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<service id="presta_sitemap.event_listener.static_routes_alternate" class="Presta\SitemapBundle\EventListener\StaticRoutesAlternateEventListener">
<argument type="service" id="router"/>
<argument>%presta_sitemap.alternate%</argument>
<tag name="kernel.event_subscriber"/>
<tag name="kernel.event_listener" event="Presta\SitemapBundle\Event\SitemapAddUrlEvent" method="addAlternate"/>
</service>
</services>

Expand Down
2 changes: 1 addition & 1 deletion config/route_annotation_listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<argument type="service" id="router"/>
<argument type="service" id="event_dispatcher"/>
<argument>%presta_sitemap.default_section%</argument>
<tag name="kernel.event_subscriber"/>
<tag name="kernel.event_listener" event="Presta\SitemapBundle\Event\SitemapPopulateEvent" method="registerRouteAnnotation"/>
</service>
</services>

Expand Down
13 changes: 1 addition & 12 deletions src/EventListener/RouteAnnotationEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Presta\SitemapBundle\Service\UrlContainerInterface;
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
Expand All @@ -28,7 +27,7 @@
*
* @phpstan-import-type RouteOptions from RouteOptionParser
*/
class RouteAnnotationEventListener implements EventSubscriberInterface
class RouteAnnotationEventListener
{
/**
* @var RouterInterface
Expand All @@ -55,16 +54,6 @@ public function __construct(
$this->defaultSection = $defaultSection;
}

/**
* @inheritdoc
*/
public static function getSubscribedEvents(): array
{
return [
SitemapPopulateEvent::class => ['registerRouteAnnotation', 0],
];
}

/**
* @param SitemapPopulateEvent $event
*/
Expand Down
13 changes: 1 addition & 12 deletions src/EventListener/StaticRoutesAlternateEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Presta\SitemapBundle\Event\SitemapAddUrlEvent;
use Presta\SitemapBundle\Sitemap\Url\GoogleMultilangUrlDecorator;
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
Expand All @@ -28,7 +27,7 @@
* locales: array<string>
* }
*/
final class StaticRoutesAlternateEventListener implements EventSubscriberInterface
final class StaticRoutesAlternateEventListener
{
private const TRANSLATED_ROUTE_NAME_STRATEGIES = [
'symfony' => '/^(?P<name>.+)\.(?P<locale>%locales%)$/',
Expand All @@ -55,16 +54,6 @@ public function __construct(UrlGeneratorInterface $router, array $options)
$this->options = $options;
}

/**
* @inheritdoc
*/
public static function getSubscribedEvents(): array
{
return [
SitemapAddUrlEvent::class => 'addAlternate',
];
}

public function addAlternate(SitemapAddUrlEvent $event): void
{
$name = $event->getRoute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class PrestaSitemapExtensionTest extends TestCase
{
private const SERVICES = [
['presta_sitemap.eventlistener.route_annotation', 'Static routes listener', 'kernel.event_subscriber'],
['presta_sitemap.eventlistener.route_annotation', 'Static routes listener', 'kernel.event_listener'],
['presta_sitemap.controller', 'Sitemap controller', null],
['presta_sitemap.dump_command', 'Dump sitemap command', 'console.command'],
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ static function () use ($routes): RouteCollection {
['resource_type' => 'closure']
);

$dispatcher->addSubscriber(new RouteAnnotationEventListener($router, $dispatcher, 'default'));
$listener = new RouteAnnotationEventListener($router, $dispatcher, 'default');
$dispatcher->addListener(SitemapPopulateEvent::class, [$listener, 'registerRouteAnnotation']);
$dispatcher->dispatch($event, SitemapPopulateEvent::class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ public function untranslated(): \Generator

private function dispatch(array $listenerOptions, string $route, array $options = []): SitemapAddUrlEvent
{
$listener = new StaticRoutesAlternateEventListener($this->router, $listenerOptions);
$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new StaticRoutesAlternateEventListener($this->router, $listenerOptions));
$dispatcher->addListener(SitemapAddUrlEvent::class, [$listener, 'addAlternate']);

$event = new SitemapAddUrlEvent($route, $options);
$dispatcher->dispatch($event, SitemapAddUrlEvent::class);
Expand Down