diff --git a/Event/SitemapPopulateEvent.php b/Event/SitemapPopulateEvent.php index 10021a7e..5f5192e7 100644 --- a/Event/SitemapPopulateEvent.php +++ b/Event/SitemapPopulateEvent.php @@ -12,68 +12,135 @@ namespace Presta\SitemapBundle\Event; use Presta\SitemapBundle\Service\UrlContainerInterface; -use Symfony\Component\EventDispatcher\Event; +use Symfony\Component\EventDispatcher\Event as BaseEvent; +use Symfony\Contracts\EventDispatcher\Event as ContractsBaseEvent; -/** - * Manage populate event - * - * @author depely - */ -class SitemapPopulateEvent extends Event -{ +if (is_subclass_of('Symfony\Component\EventDispatcher\EventDispatcher', 'Symfony\Contracts\EventDispatcher\EventDispatcherInterface')) { /** - * @Event("Presta\SitemapBundle\Event\SitemapPopulateEvent") + * Manage populate event + * + * @author depely */ - const ON_SITEMAP_POPULATE = 'presta_sitemap.populate'; + class SitemapPopulateEvent extends ContractsBaseEvent + { + /** + * @Event("Presta\SitemapBundle\Event\SitemapPopulateEvent") + */ + const ON_SITEMAP_POPULATE = 'presta_sitemap.populate'; - /** - * @var UrlContainerInterface - */ - protected $urlContainer; + /** + * @var UrlContainerInterface + */ + protected $urlContainer; - /** - * Allows creating EventListeners for particular sitemap sections, used when dumping - * @var string - */ - protected $section; + /** + * Allows creating EventListeners for particular sitemap sections, used when dumping + * @var string + */ + protected $section; - /** - * @param UrlContainerInterface $urlContainer - * @param string|null $section - */ - public function __construct(UrlContainerInterface $urlContainer, $section = null) - { - $this->urlContainer = $urlContainer; - $this->section = $section; - } + /** + * @param UrlContainerInterface $urlContainer + * @param string|null $section + */ + public function __construct(UrlContainerInterface $urlContainer, $section = null) + { + $this->urlContainer = $urlContainer; + $this->section = $section; + } - /** - * @deprecated in favor of `Presta\SitemapBundle\Event\SitemapPopulateEvent::getUrlContainer()` - * - * @return UrlContainerInterface - */ - public function getGenerator() - { - @trigger_error('getGenerator is deprecated since 1.5. Use getUrlContainer instead', E_USER_DEPRECATED); + /** + * @deprecated in favor of `Presta\SitemapBundle\Event\SitemapPopulateEvent::getUrlContainer()` + * + * @return UrlContainerInterface + */ + public function getGenerator() + { + @trigger_error('getGenerator is deprecated since 1.5. Use getUrlContainer instead', E_USER_DEPRECATED); - return $this->urlContainer; - } + return $this->urlContainer; + } - /** - * @return UrlContainerInterface - */ - public function getUrlContainer() - { - return $this->urlContainer; - } + /** + * @return UrlContainerInterface + */ + public function getUrlContainer() + { + return $this->urlContainer; + } + /** + * Section to be processed, null means any + * + * @return null|string + */ + public function getSection() + { + return $this->section; + } + } +} else { /** - * Section to be processed, null means any + * Manage populate event * - * @return null|string + * @author depely */ - public function getSection() + class SitemapPopulateEvent extends BaseEvent { - return $this->section; + /** + * @Event("Presta\SitemapBundle\Event\SitemapPopulateEvent") + */ + const ON_SITEMAP_POPULATE = 'presta_sitemap.populate'; + + /** + * @var UrlContainerInterface + */ + protected $urlContainer; + + /** + * Allows creating EventListeners for particular sitemap sections, used when dumping + * @var string + */ + protected $section; + + /** + * @param UrlContainerInterface $urlContainer + * @param string|null $section + */ + public function __construct(UrlContainerInterface $urlContainer, $section = null) + { + $this->urlContainer = $urlContainer; + $this->section = $section; + } + + /** + * @deprecated in favor of `Presta\SitemapBundle\Event\SitemapPopulateEvent::getUrlContainer()` + * + * @return UrlContainerInterface + */ + public function getGenerator() + { + @trigger_error('getGenerator is deprecated since 1.5. Use getUrlContainer instead', E_USER_DEPRECATED); + + return $this->urlContainer; + } + + /** + * @return UrlContainerInterface + */ + public function getUrlContainer() + { + return $this->urlContainer; + } + + /** + * Section to be processed, null means any + * + * @return null|string + */ + public function getSection() + { + return $this->section; + } } } diff --git a/Service/AbstractGenerator.php b/Service/AbstractGenerator.php index a27e29ac..e620c15c 100644 --- a/Service/AbstractGenerator.php +++ b/Service/AbstractGenerator.php @@ -17,6 +17,7 @@ use Presta\SitemapBundle\Sitemap\Url\UrlConcrete; use Presta\SitemapBundle\Sitemap\Urlset; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface; /** * Abstract sitemap generator class @@ -144,7 +145,12 @@ abstract protected function newUrlset($name, \DateTime $lastmod = null); protected function populate($section = null) { $event = new SitemapPopulateEvent($this, $section); - $this->dispatcher->dispatch(SitemapPopulateEvent::ON_SITEMAP_POPULATE, $event); + + if ($this->dispatcher instanceof ContractsEventDispatcherInterface) { + $this->dispatcher->dispatch($event, SitemapPopulateEvent::ON_SITEMAP_POPULATE); + } else { + $this->dispatcher->dispatch(SitemapPopulateEvent::ON_SITEMAP_POPULATE, $event); + } } /**