From 51fd0c0461b585b526a095cc4c4da0df3ae4c817 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Fri, 26 Mar 2021 12:30:25 +0100 Subject: [PATCH] Drop symfony 3.4 support (#275) * Removed Symfony 3.4 from composer allowed dependencies * Removed conditional event contract existence from code base * Removed conditional configuration init due to Symfony 3.4 * Updated testing matrix versions --- .github/workflows/tests.yml | 22 +- DependencyInjection/Configuration.php | 9 +- Event/SitemapAddUrlEvent.php | 294 ++++++------------ Event/SitemapPopulateEvent.php | 133 +++----- .../RouteAnnotationEventListener.php | 7 +- Service/AbstractGenerator.php | 7 +- .../RouteAnnotationEventListenerTest.php | 7 +- ...StaticRoutesAlternateEventListenerTest.php | 7 +- composer.json | 13 +- 9 files changed, 155 insertions(+), 344 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 091ce8cf..67cc04ec 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -14,18 +14,14 @@ jobs: strategy: matrix: include: - - php-version: 7.1 - symfony-version: 3.4.* - - php-version: 7.4 - symfony-version: 3.4.* - php-version: 7.1 symfony-version: 4.4.* - php-version: 7.4 symfony-version: 4.4.* - php-version: 7.2 - symfony-version: 5.1.* + symfony-version: 5.2.* - php-version: 7.4 - symfony-version: 5.1.* + symfony-version: 5.2.* steps: - name: "Checkout" @@ -37,14 +33,14 @@ jobs: coverage: none php-version: ${{ matrix.php-version }} - - name: "Require symfony/messenger dependencies when possible" - if: matrix.symfony-version != '3.4.*' - run: | - composer require --no-update symfony/messenger:${{ matrix.symfony-version }} - - name: "Install dependencies with composer" run: | - composer require symfony/console:${{ matrix.symfony-version }} symfony/framework-bundle:${{ matrix.symfony-version }} symfony/http-kernel:${{ matrix.symfony-version }} symfony/routing:${{ matrix.symfony-version }} --no-interaction --no-update + composer require --no-interaction --no-update \ + symfony/console:${{ matrix.symfony-version }} \ + symfony/framework-bundle:${{ matrix.symfony-version }} \ + symfony/http-kernel:${{ matrix.symfony-version }} \ + symfony/routing:${{ matrix.symfony-version }} \ + symfony/messenger:${{ matrix.symfony-version }} composer update --no-interaction --no-progress --no-suggest - name: "Run tests with phpunit/phpunit" @@ -58,7 +54,7 @@ jobs: matrix: include: - php-version: 7.4 - symfony-version: 5.1.* + symfony-version: 5.2.* steps: - name: "Checkout" diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 189dd1a0..b73697fa 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -30,13 +30,8 @@ class Configuration implements ConfigurationInterface */ public function getConfigTreeBuilder() { - if (method_exists(TreeBuilder::class, 'getRootNode')) { - $treeBuilder = new TreeBuilder('presta_sitemap'); - $rootNode = $treeBuilder->getRootNode(); - } else { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('presta_sitemap'); - } + $treeBuilder = new TreeBuilder('presta_sitemap'); + $rootNode = $treeBuilder->getRootNode(); $rootNode ->children() diff --git a/Event/SitemapAddUrlEvent.php b/Event/SitemapAddUrlEvent.php index f1e81a4f..b01c1777 100644 --- a/Event/SitemapAddUrlEvent.php +++ b/Event/SitemapAddUrlEvent.php @@ -12,213 +12,107 @@ namespace Presta\SitemapBundle\Event; use Presta\SitemapBundle\Sitemap\Url\Url; -use Symfony\Component\EventDispatcher\Event as BaseEvent; -use Symfony\Contracts\EventDispatcher\Event as ContractsBaseEvent; +use Symfony\Contracts\EventDispatcher\Event; + +/** + * Event to allow generation of static routes sitemap urls. + */ +class SitemapAddUrlEvent extends Event +{ + /** + * @Event("Presta\SitemapBundle\Event\SitemapAddUrlEvent") + */ + public const NAME = 'presta_sitemap.add_url'; + + /** + * @var bool + */ + private $shouldBeRegistered = true; + + /** + * @var Url|null + */ + private $url; + + /** + * @var string + */ + private $route; + + /** + * @var array + */ + private $options; + + public function __construct(string $route, array $options) + { + $this->route = $route; + $this->options = $options; + } + + /** + * Whether or not associated URL should be registered to sitemap. + * + * @return bool + */ + public function shouldBeRegistered(): bool + { + return $this->shouldBeRegistered; + } + + /** + * Allow URL registration to sitemap. + */ + public function allowRegistration(): void + { + $this->shouldBeRegistered = true; + } + + /** + * Prevent URL registration to sitemap. + */ + public function preventRegistration(): void + { + $this->shouldBeRegistered = false; + } -if (is_subclass_of('Symfony\Component\EventDispatcher\EventDispatcher', 'Symfony\Contracts\EventDispatcher\EventDispatcherInterface')) { /** - * Event to allow generation of static routes sitemap urls. + * URL that is about to be added to sitemap or NULL if not set yet. + * + * @return Url|null */ - class SitemapAddUrlEvent extends ContractsBaseEvent + public function getUrl(): ?Url { - /** - * @Event("Presta\SitemapBundle\Event\SitemapAddUrlEvent") - */ - public const NAME = 'presta_sitemap.add_url'; - - /** - * @var bool - */ - private $shouldBeRegistered = true; - - /** - * @var Url|null - */ - private $url; - - /** - * @var string - */ - private $route; - - /** - * @var array - */ - private $options; - - public function __construct(string $route, array $options) - { - $this->route = $route; - $this->options = $options; - } - - /** - * Whether or not associated URL should be registered to sitemap. - * - * @return bool - */ - public function shouldBeRegistered(): bool - { - return $this->shouldBeRegistered; - } - - /** - * Allow URL registration to sitemap. - */ - public function allowRegistration(): void - { - $this->shouldBeRegistered = true; - } - - /** - * Prevent URL registration to sitemap. - */ - public function preventRegistration(): void - { - $this->shouldBeRegistered = false; - } - - /** - * URL that is about to be added to sitemap or NULL if not set yet. - * - * @return Url|null - */ - public function getUrl(): ?Url - { - return $this->url; - } - - /** - * Set the URL that will be added to sitemap. - * - * @param Url $url Replacement - */ - public function setUrl(Url $url): void - { - $this->url = $url; - } - - /** - * The route name. - * - * @return string - */ - public function getRoute(): string - { - return $this->route; - } - - /** - * The sitemap route options. - * - * @return array - */ - public function getOptions(): array - { - return $this->options; - } + return $this->url; } -} else { + + /** + * Set the URL that will be added to sitemap. + * + * @param Url $url Replacement + */ + public function setUrl(Url $url): void + { + $this->url = $url; + } + + /** + * The route name. + * + * @return string + */ + public function getRoute(): string + { + return $this->route; + } + /** - * Event to allow generation of static routes sitemap urls. + * The sitemap route options. + * + * @return array */ - class SitemapAddUrlEvent extends BaseEvent + public function getOptions(): array { - /** - * @Event("Presta\SitemapBundle\Event\SitemapAddUrlEvent") - */ - public const NAME = 'presta_sitemap.add_url'; - - /** - * @var bool - */ - private $shouldBeRegistered = true; - - /** - * @var Url|null - */ - private $url; - - /** - * @var string - */ - private $route; - - /** - * @var array - */ - private $options; - - public function __construct(string $route, array $options) - { - $this->route = $route; - $this->options = $options; - } - - /** - * Whether or not associated URL should be registered to sitemap. - * - * @return bool - */ - public function shouldBeRegistered(): bool - { - return $this->shouldBeRegistered; - } - - /** - * Allow URL registration to sitemap. - */ - public function allowRegistration(): void - { - $this->shouldBeRegistered = true; - } - - /** - * Prevent URL registration to sitemap. - */ - public function preventRegistration(): void - { - $this->shouldBeRegistered = false; - } - - /** - * URL that is about to be added to sitemap or NULL if not set yet. - * - * @return Url|null - */ - public function getUrl(): ?Url - { - return $this->url; - } - - /** - * Set the URL that will be added to sitemap. - * - * @param Url $url Replacement - */ - public function setUrl(Url $url): void - { - $this->url = $url; - } - - /** - * The route name. - * - * @return string - */ - public function getRoute(): string - { - return $this->route; - } - - /** - * The sitemap route options. - * - * @return array - */ - public function getOptions(): array - { - return $this->options; - } + return $this->options; } } diff --git a/Event/SitemapPopulateEvent.php b/Event/SitemapPopulateEvent.php index f072e07a..bb23fb42 100644 --- a/Event/SitemapPopulateEvent.php +++ b/Event/SitemapPopulateEvent.php @@ -12,111 +12,56 @@ namespace Presta\SitemapBundle\Event; use Presta\SitemapBundle\Service\UrlContainerInterface; -use Symfony\Component\EventDispatcher\Event as BaseEvent; -use Symfony\Contracts\EventDispatcher\Event as ContractsBaseEvent; +use Symfony\Contracts\EventDispatcher\Event; -if (is_subclass_of('Symfony\Component\EventDispatcher\EventDispatcher', 'Symfony\Contracts\EventDispatcher\EventDispatcherInterface')) { +/** + * Manage populate event + * + * @author depely + */ +class SitemapPopulateEvent extends Event +{ /** - * Manage populate event - * - * @author depely + * @Event("Presta\SitemapBundle\Event\SitemapPopulateEvent") */ - class SitemapPopulateEvent extends ContractsBaseEvent - { - /** - * @Event("Presta\SitemapBundle\Event\SitemapPopulateEvent") - */ - const ON_SITEMAP_POPULATE = 'presta_sitemap.populate'; - - /** - * @var UrlContainerInterface - */ - protected $urlContainer; + const ON_SITEMAP_POPULATE = 'presta_sitemap.populate'; - /** - * Allows creating EventListeners for particular sitemap sections, used when dumping - * @var string - */ - protected $section; + /** + * @var UrlContainerInterface + */ + protected $urlContainer; - /** - * @param UrlContainerInterface $urlContainer - * @param string|null $section - */ - public function __construct(UrlContainerInterface $urlContainer, $section = null) - { - $this->urlContainer = $urlContainer; - $this->section = $section; - } + /** + * Allows creating EventListeners for particular sitemap sections, used when dumping + * @var string + */ + protected $section; - /** - * @return UrlContainerInterface - */ - public function getUrlContainer() - { - return $this->urlContainer; - } + /** + * @param UrlContainerInterface $urlContainer + * @param string|null $section + */ + public function __construct(UrlContainerInterface $urlContainer, $section = null) + { + $this->urlContainer = $urlContainer; + $this->section = $section; + } - /** - * Section to be processed, null means any - * - * @return null|string - */ - public function getSection() - { - return $this->section; - } + /** + * @return UrlContainerInterface + */ + public function getUrlContainer() + { + return $this->urlContainer; } -} else { + /** - * Manage populate event + * Section to be processed, null means any * - * @author depely + * @return null|string */ - class SitemapPopulateEvent extends BaseEvent + public function getSection() { - /** - * @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; - } - - /** - * @return UrlContainerInterface - */ - public function getUrlContainer() - { - return $this->urlContainer; - } - - /** - * Section to be processed, null means any - * - * @return null|string - */ - public function getSection() - { - return $this->section; - } + return $this->section; } } diff --git a/EventListener/RouteAnnotationEventListener.php b/EventListener/RouteAnnotationEventListener.php index c6a80512..335648cc 100644 --- a/EventListener/RouteAnnotationEventListener.php +++ b/EventListener/RouteAnnotationEventListener.php @@ -22,7 +22,6 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouterInterface; -use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface; /** * This listener iterate over configured routes, and register allowed URLs to sitemap. @@ -94,11 +93,7 @@ private function addUrlsFromRoutes(UrlContainerInterface $container, ?string $se } $event = new SitemapAddUrlEvent($name, $options); - if ($this->dispatcher instanceof ContractsEventDispatcherInterface) { - $this->dispatcher->dispatch($event, SitemapAddUrlEvent::NAME); - } else { - $this->dispatcher->dispatch(SitemapAddUrlEvent::NAME, $event); - } + $this->dispatcher->dispatch($event, SitemapAddUrlEvent::NAME); if (!$event->shouldBeRegistered()) { continue; diff --git a/Service/AbstractGenerator.php b/Service/AbstractGenerator.php index 81fdb564..6801517b 100644 --- a/Service/AbstractGenerator.php +++ b/Service/AbstractGenerator.php @@ -18,7 +18,6 @@ use Presta\SitemapBundle\Sitemap\Url\UrlDecorator; use Presta\SitemapBundle\Sitemap\Urlset; use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface; /** * Abstract sitemap generator class @@ -148,11 +147,7 @@ protected function populate($section = null) { $event = new SitemapPopulateEvent($this, $section); - if ($this->dispatcher instanceof ContractsEventDispatcherInterface) { - $this->dispatcher->dispatch($event, SitemapPopulateEvent::ON_SITEMAP_POPULATE); - } else { - $this->dispatcher->dispatch(SitemapPopulateEvent::ON_SITEMAP_POPULATE, $event); - } + $this->dispatcher->dispatch($event, SitemapPopulateEvent::ON_SITEMAP_POPULATE); } /** diff --git a/Tests/Unit/EventListener/RouteAnnotationEventListenerTest.php b/Tests/Unit/EventListener/RouteAnnotationEventListenerTest.php index 03c1826e..989d26f9 100644 --- a/Tests/Unit/EventListener/RouteAnnotationEventListenerTest.php +++ b/Tests/Unit/EventListener/RouteAnnotationEventListenerTest.php @@ -24,7 +24,6 @@ use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Router; -use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface; class RouteAnnotationEventListenerTest extends TestCase { @@ -149,11 +148,7 @@ static function () use ($routes): RouteCollection { ); $dispatcher->addSubscriber(new RouteAnnotationEventListener($router, $dispatcher, 'default')); - if ($dispatcher instanceof ContractsEventDispatcherInterface) { - $dispatcher->dispatch($event, SitemapPopulateEvent::ON_SITEMAP_POPULATE); - } else { - $dispatcher->dispatch(SitemapPopulateEvent::ON_SITEMAP_POPULATE, $event); - } + $dispatcher->dispatch($event, SitemapPopulateEvent::ON_SITEMAP_POPULATE); } private function findUrl(array $urlset, string $loc): ?UrlConcrete diff --git a/Tests/Unit/EventListener/StaticRoutesAlternateEventListenerTest.php b/Tests/Unit/EventListener/StaticRoutesAlternateEventListenerTest.php index 8c8f2c45..ddc5261b 100644 --- a/Tests/Unit/EventListener/StaticRoutesAlternateEventListenerTest.php +++ b/Tests/Unit/EventListener/StaticRoutesAlternateEventListenerTest.php @@ -8,7 +8,6 @@ use Prophecy\Prophecy\ObjectProphecy; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; -use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface; class StaticRoutesAlternateEventListenerTest extends TestCase { @@ -102,11 +101,7 @@ private function dispatch(array $listenerOptions, string $route, array $options $dispatcher->addSubscriber(new StaticRoutesAlternateEventListener($this->router->reveal(), $listenerOptions)); $event = new SitemapAddUrlEvent($route, $options); - if ($dispatcher instanceof ContractsEventDispatcherInterface) { - $dispatcher->dispatch($event, SitemapAddUrlEvent::NAME); - } else { - $dispatcher->dispatch(SitemapAddUrlEvent::NAME, $event); - } + $dispatcher->dispatch($event, SitemapAddUrlEvent::NAME); return $event; } diff --git a/composer.json b/composer.json index be756cb1..c99636cb 100644 --- a/composer.json +++ b/composer.json @@ -14,17 +14,18 @@ "issues": "https://github.com/prestaconcept/PrestaSitemapBundle/issues" }, "require": { - "php": ">=7.1.0", + "php": ">=7.1.3", "ext-simplexml": "*", - "symfony/console": "^3.4|~4.0|~5.0", - "symfony/framework-bundle": "^3.4|~4.0|~5.0" + "symfony/console": "^4.4|^5.0", + "symfony/framework-bundle": "^4.4|^5.0" }, "require-dev": { "doctrine/annotations": "^1.0", "phpunit/phpunit": "^7.5", - "symfony/browser-kit": "^4.4", - "symfony/phpunit-bridge": "^4.4", - "symfony/yaml": "^4.4" + "symfony/messenger": "^4.4|^5.0", + "symfony/browser-kit": "^4.4|^5.0", + "symfony/phpunit-bridge": "^4.4|^5.0", + "symfony/yaml": "^4.4|^5.0" }, "autoload": { "psr-4": {