Skip to content

Commit

Permalink
Allow subscribing events by class (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
norkunas committed Jan 24, 2022
1 parent 341ad68 commit ad23fe5
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion doc/4-dynamic-routes-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class SitemapSubscriber implements EventSubscriberInterface
public static function getSubscribedEvents()
{
return [
SitemapPopulateEvent::ON_SITEMAP_POPULATE => 'populate',
SitemapPopulateEvent::class => 'populate',
];
}

Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/PrestaSitemapExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Presta\SitemapBundle\DependencyInjection;

use Presta\SitemapBundle\Event\SitemapAddUrlEvent;
use Presta\SitemapBundle\Event\SitemapPopulateEvent;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
Expand Down
1 change: 1 addition & 0 deletions src/Event/SitemapAddUrlEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class SitemapAddUrlEvent extends Event
{
/**
* @Event("Presta\SitemapBundle\Event\SitemapAddUrlEvent")
* @deprecated since presta/sitemap-bundle 3.3, use `SitemapAddUrlEvent::class` instead.
*/
public const NAME = 'presta_sitemap.add_url';

Expand Down
1 change: 1 addition & 0 deletions src/Event/SitemapPopulateEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class SitemapPopulateEvent extends Event
{
/**
* @Event("Presta\SitemapBundle\Event\SitemapPopulateEvent")
* @deprecated since presta/sitemap-bundle 3.3, use `SitemapPopulateEvent::class` instead.
*/
public const ON_SITEMAP_POPULATE = 'presta_sitemap.populate';

Expand Down
2 changes: 1 addition & 1 deletion src/EventListener/RouteAnnotationEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __construct(
public static function getSubscribedEvents(): array
{
return [
SitemapPopulateEvent::ON_SITEMAP_POPULATE => ['registerRouteAnnotation', 0],
SitemapPopulateEvent::class => ['registerRouteAnnotation', 0],
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/EventListener/StaticRoutesAlternateEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __construct(UrlGeneratorInterface $router, array $options)
public static function getSubscribedEvents(): array
{
return [
SitemapAddUrlEvent::NAME => 'addAlternate',
SitemapAddUrlEvent::class => 'addAlternate',
];
}

Expand Down
20 changes: 20 additions & 0 deletions src/PrestaSitemapBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

namespace Presta\SitemapBundle;

use Presta\SitemapBundle\DependencyInjection\Compiler\EventAliasMappingPass;
use Presta\SitemapBundle\Event\SitemapAddUrlEvent;
use Presta\SitemapBundle\Event\SitemapPopulateEvent;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\DependencyInjection\AddEventAliasesPass;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
Expand All @@ -20,6 +25,21 @@
*/
class PrestaSitemapBundle extends Bundle
{
/**
* @inheritdoc
*
* @return void
*/
public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(new AddEventAliasesPass([
SitemapAddUrlEvent::class => SitemapAddUrlEvent::NAME,
SitemapPopulateEvent::class => SitemapPopulateEvent::ON_SITEMAP_POPULATE,
]));
}

/**
* @inheritDoc
*/
Expand Down
3 changes: 1 addition & 2 deletions tests/Integration/src/Listener/SitemapListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;

final class SitemapListener implements EventSubscriberInterface
{
Expand Down Expand Up @@ -54,7 +53,7 @@ final class SitemapListener implements EventSubscriberInterface
public static function getSubscribedEvents(): array
{
return [
SitemapPopulateEvent::ON_SITEMAP_POPULATE => 'populate',
SitemapPopulateEvent::class => 'populate',
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ static function () use ($routes): RouteCollection {
);

$dispatcher->addSubscriber(new RouteAnnotationEventListener($router, $dispatcher, 'default'));
$dispatcher->dispatch($event, SitemapPopulateEvent::ON_SITEMAP_POPULATE);
$dispatcher->dispatch($event, SitemapPopulateEvent::class);
}

private function findUrl(array $urlset, string $loc): ?UrlConcrete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private function dispatch(array $listenerOptions, string $route, array $options
$dispatcher->addSubscriber(new StaticRoutesAlternateEventListener($this->router, $listenerOptions));

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

return $event;
}
Expand Down

0 comments on commit ad23fe5

Please sign in to comment.