From 572c5b48c1302021fa9d3f0b86074aa50855d818 Mon Sep 17 00:00:00 2001 From: Jeroen Thora Date: Sun, 14 Mar 2021 16:59:24 +0100 Subject: [PATCH] [AdminListBundle] Change pagerfanta dependencies to require the minimum needed packages --- UPGRADE-5.9.md | 4 ++ composer.json | 2 +- .../Compiler/PagerfantaBridgePass.php | 55 ++++++++++++++++++ .../PagerfantaConfiguration.php | 58 +++++++++++++++++++ .../PagerfantaExtension.php | 41 +++++++++++++ .../AdminBundle/KunstmaanAdminBundle.php | 6 ++ .../Compiler/PagerfantaBridgePassTest.php | 31 ++++++++++ .../PagerfantaExtensionTest.php | 47 +++++++++++++++ src/Kunstmaan/AdminBundle/composer.json | 4 +- ...stractDoctrineORMAdminListConfigurator.php | 4 +- .../Helper/DoctrineDBALAdapterTest.php | 5 ++ src/Kunstmaan/AdminListBundle/composer.json | 5 +- src/Kunstmaan/ArticleBundle/composer.json | 4 +- src/Kunstmaan/MediaBundle/composer.json | 3 +- src/Kunstmaan/NodeSearchBundle/composer.json | 4 +- 15 files changed, 264 insertions(+), 9 deletions(-) create mode 100644 src/Kunstmaan/AdminBundle/DependencyInjection/Compiler/PagerfantaBridgePass.php create mode 100644 src/Kunstmaan/AdminBundle/DependencyInjection/PagerfantaConfiguration.php create mode 100644 src/Kunstmaan/AdminBundle/DependencyInjection/PagerfantaExtension.php create mode 100644 src/Kunstmaan/AdminBundle/Tests/DependencyInjection/Compiler/PagerfantaBridgePassTest.php create mode 100644 src/Kunstmaan/AdminBundle/Tests/DependencyInjection/PagerfantaExtensionTest.php diff --git a/UPGRADE-5.9.md b/UPGRADE-5.9.md index c712d14320..94db3d01b7 100644 --- a/UPGRADE-5.9.md +++ b/UPGRADE-5.9.md @@ -5,6 +5,10 @@ General ------- * All event classes are marked as final. +* If you still require the `kunstmaan/bundles-cms` package, update you `composer.json` to require `babdev/pagerfanta-bundle` + instead of `white-october/pagerfanta-bundle`. All specific bundle packages that use pagerfanta functionality are now requiring + the specifc pagerfanta packages. Newer skeleton installs will require the correct pagerfanta bundle package but old projects + or projects using the `white-october` package are encouraged to switch their dependencies. AdminBundle ------------ diff --git a/composer.json b/composer.json index a17d8108ed..8f8833e6fb 100644 --- a/composer.json +++ b/composer.json @@ -61,7 +61,7 @@ "friendsofsymfony/user-bundle": "^2.0", "knplabs/knp-menu-bundle": "^3.0", "guzzlehttp/guzzle": "~6.1", - "white-october/pagerfanta-bundle": "~1.0", + "babdev/pagerfanta-bundle": "^2.5", "kunstmaan/google-api-custom": "~1.0", "gedmo/doctrine-extensions": "^2.4.34", "doctrine/doctrine-fixtures-bundle": "^3.3", diff --git a/src/Kunstmaan/AdminBundle/DependencyInjection/Compiler/PagerfantaBridgePass.php b/src/Kunstmaan/AdminBundle/DependencyInjection/Compiler/PagerfantaBridgePass.php new file mode 100644 index 0000000000..af059c7188 --- /dev/null +++ b/src/Kunstmaan/AdminBundle/DependencyInjection/Compiler/PagerfantaBridgePass.php @@ -0,0 +1,55 @@ +changeViewFactoryClass($container); + $this->aliasRenamedServices($container); + } + + private function changeViewFactoryClass(ContainerBuilder $container): void + { + if (!$container->hasParameter('white_october_pagerfanta.view_factory.class') || !$container->hasDefinition('pagerfanta.view_factory')) { + return; + } + + $container->getDefinition('pagerfanta.view_factory') + ->setClass($container->getParameter('white_october_pagerfanta.view_factory.class')); + } + + private function aliasRenamedServices(ContainerBuilder $container): void + { + if ($container->hasDefinition('pagerfanta.twig_extension')) { + $alias = $container->setAlias('twig.extension.pagerfanta', 'pagerfanta.twig_extension'); + if (method_exists(Alias::class, 'setDeprecated')) { + $alias->setDeprecated(true, 'The "%alias_id%" service alias is deprecated since KunstmaanAdminBundle 5.9, use the "pagerfanta.twig_extension" service ID instead.'); + } + } + + if ($container->hasDefinition('pagerfanta.view_factory')) { + $alias = $container->setAlias('white_october_pagerfanta.view_factory', 'pagerfanta.view_factory'); + if (method_exists(Alias::class, 'setDeprecated')) { + $alias->setDeprecated(true, 'The "%alias_id%" service alias is deprecated since KunstmaanAdminBundle 5.9, use the "pagerfanta.view_factory" service ID instead.'); + } + } + } +} diff --git a/src/Kunstmaan/AdminBundle/DependencyInjection/PagerfantaConfiguration.php b/src/Kunstmaan/AdminBundle/DependencyInjection/PagerfantaConfiguration.php new file mode 100644 index 0000000000..b17fb84c22 --- /dev/null +++ b/src/Kunstmaan/AdminBundle/DependencyInjection/PagerfantaConfiguration.php @@ -0,0 +1,58 @@ +getRootNode(); + } else { + // BC layer for symfony/config 4.1 and older + $rootNode = $treeBuilder->root('white_october_pagerfanta'); + } + + $this->addExceptionsStrategySection($rootNode); + + $rootNode + ->children() + ->scalarNode('default_view') + ->defaultValue('default') + ->end() + ->end() + ; + + return $treeBuilder; + } + + private function addExceptionsStrategySection(ArrayNodeDefinition $node): void + { + $node + ->children() + ->arrayNode('exceptions_strategy') + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('out_of_range_page')->defaultValue(self::EXCEPTION_STRATEGY_TO_HTTP_NOT_FOUND)->end() + ->scalarNode('not_valid_current_page')->defaultValue(self::EXCEPTION_STRATEGY_TO_HTTP_NOT_FOUND)->end() + ->end() + ->end() + ->end() + ; + } +} diff --git a/src/Kunstmaan/AdminBundle/DependencyInjection/PagerfantaExtension.php b/src/Kunstmaan/AdminBundle/DependencyInjection/PagerfantaExtension.php new file mode 100644 index 0000000000..97684eac99 --- /dev/null +++ b/src/Kunstmaan/AdminBundle/DependencyInjection/PagerfantaExtension.php @@ -0,0 +1,41 @@ +processConfiguration($this->getConfiguration($config, $container), $config); + + $container->setParameter('white_october_pagerfanta.default_view', $config['default_view']); + } + + public function prepend(ContainerBuilder $container): void + { + $config = $this->processConfiguration($this->getConfiguration([], $container), $container->getExtensionConfig($this->getAlias())); + + $container->prependExtensionConfig('babdev_pagerfanta', $config); + } +} diff --git a/src/Kunstmaan/AdminBundle/KunstmaanAdminBundle.php b/src/Kunstmaan/AdminBundle/KunstmaanAdminBundle.php index 575314f9dd..32d148fab4 100644 --- a/src/Kunstmaan/AdminBundle/KunstmaanAdminBundle.php +++ b/src/Kunstmaan/AdminBundle/KunstmaanAdminBundle.php @@ -12,8 +12,11 @@ use Kunstmaan\AdminBundle\DependencyInjection\Compiler\EnablePermissionsPass; use Kunstmaan\AdminBundle\DependencyInjection\Compiler\InjectUntrackedTokenStorageCompilerPass; use Kunstmaan\AdminBundle\DependencyInjection\Compiler\MenuCompilerPass; +use Kunstmaan\AdminBundle\DependencyInjection\Compiler\PagerfantaBridgePass; use Kunstmaan\AdminBundle\DependencyInjection\Compiler\VersionCheckerCacheBcPass; use Kunstmaan\AdminBundle\DependencyInjection\KunstmaanAdminExtension; +use Kunstmaan\AdminBundle\DependencyInjection\PagerfantaExtension; +use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -43,5 +46,8 @@ public function build(ContainerBuilder $container) $container->addCompilerPass(new VersionCheckerCacheBcPass()); $container->registerExtension(new KunstmaanAdminExtension()); + + $container->registerExtension(new PagerfantaExtension()); + $container->addCompilerPass(new PagerfantaBridgePass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -1); // Should run after all passes from BabDevPagerfantaBundle } } diff --git a/src/Kunstmaan/AdminBundle/Tests/DependencyInjection/Compiler/PagerfantaBridgePassTest.php b/src/Kunstmaan/AdminBundle/Tests/DependencyInjection/Compiler/PagerfantaBridgePassTest.php new file mode 100644 index 0000000000..91774a3c4e --- /dev/null +++ b/src/Kunstmaan/AdminBundle/Tests/DependencyInjection/Compiler/PagerfantaBridgePassTest.php @@ -0,0 +1,31 @@ +registerService('pagerfanta.twig_extension', PagerfantaExtension::class); + $this->registerService('pagerfanta.view_factory', ViewFactory::class); + + $this->setParameter('white_october_pagerfanta.view_factory.class', 'My\ViewFactory'); + + $this->compile(); + + $this->assertContainerBuilderHasAlias('twig.extension.pagerfanta', 'pagerfanta.twig_extension'); + $this->assertContainerBuilderHasAlias('white_october_pagerfanta.view_factory', 'pagerfanta.view_factory'); + $this->assertContainerBuilderHasService('pagerfanta.view_factory', 'My\ViewFactory'); + } + + protected function registerCompilerPass(ContainerBuilder $container): void + { + $container->addCompilerPass(new PagerfantaBridgePass()); + } +} diff --git a/src/Kunstmaan/AdminBundle/Tests/DependencyInjection/PagerfantaExtensionTest.php b/src/Kunstmaan/AdminBundle/Tests/DependencyInjection/PagerfantaExtensionTest.php new file mode 100644 index 0000000000..a9342ea4a8 --- /dev/null +++ b/src/Kunstmaan/AdminBundle/Tests/DependencyInjection/PagerfantaExtensionTest.php @@ -0,0 +1,47 @@ +container->setParameter( + 'kernel.bundles', + [ + 'BabDevPagerfantaBundle' => BabDevPagerfantaBundle::class, + 'TwigBundle' => TwigBundle::class, + ] + ); + + $bundleConfig = [ + 'default_view' => 'twitter_bootstrap', + 'exceptions_strategy' => [ + 'out_of_range_page' => 'custom', + 'not_valid_current_page' => 'to_http_not_found', + ], + ]; + + // Prepend config now to allow the prepend pass to work + $this->container->prependExtensionConfig('white_october_pagerfanta', $bundleConfig); + + $this->load($bundleConfig); + + $this->assertSame([$bundleConfig], $this->container->getExtensionConfig('babdev_pagerfanta')); + $this->assertContainerBuilderHasParameter('white_october_pagerfanta.default_view', $bundleConfig['default_view']); + } + + protected function getContainerExtensions(): array + { + return [ + new PagerfantaExtension(), + new BabDevPagerfantaExtension(), + ]; + } +} diff --git a/src/Kunstmaan/AdminBundle/composer.json b/src/Kunstmaan/AdminBundle/composer.json index aad5bd6586..bccf1a0cef 100644 --- a/src/Kunstmaan/AdminBundle/composer.json +++ b/src/Kunstmaan/AdminBundle/composer.json @@ -14,6 +14,7 @@ ], "require": { "php": "^7.2", + "babdev/pagerfanta-bundle": "^2.5", "doctrine/orm": "^2.5", "doctrine/dbal": "^2.5", "doctrine/doctrine-bundle": "^1.6.12|^2.0", @@ -53,8 +54,7 @@ "symfony/yaml": "^3.4|^4.4", "twig/twig": "^1.40|^2.9", "twig/extensions": "~1.0", - "sensio/framework-extra-bundle": "^5.0", - "white-october/pagerfanta-bundle": "~1.0" + "sensio/framework-extra-bundle": "^5.0" }, "suggest": { "kunstmaan/translator-bundle": "^5.2", diff --git a/src/Kunstmaan/AdminListBundle/AdminList/Configurator/AbstractDoctrineORMAdminListConfigurator.php b/src/Kunstmaan/AdminListBundle/AdminList/Configurator/AbstractDoctrineORMAdminListConfigurator.php index 4fa1f3bb28..9f1dba5d86 100644 --- a/src/Kunstmaan/AdminListBundle/AdminList/Configurator/AbstractDoctrineORMAdminListConfigurator.php +++ b/src/Kunstmaan/AdminListBundle/AdminList/Configurator/AbstractDoctrineORMAdminListConfigurator.php @@ -10,7 +10,7 @@ use Kunstmaan\AdminListBundle\AdminList\Filter; use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM\AbstractORMFilterType; use Kunstmaan\AdminListBundle\AdminList\SortableInterface; -use Pagerfanta\Adapter\DoctrineORMAdapter; +use Pagerfanta\Doctrine\ORM\QueryAdapter as OrmQueryAdapter; use Pagerfanta\Pagerfanta; use Traversable; @@ -92,7 +92,7 @@ public function getDeleteUrlFor($item) public function getPagerfanta() { if (\is_null($this->pagerfanta)) { - $adapter = new DoctrineORMAdapter($this->getQuery()); + $adapter = new OrmQueryAdapter($this->getQuery()); $this->pagerfanta = new Pagerfanta($adapter); $this->pagerfanta->setNormalizeOutOfRangePages(true); $this->pagerfanta->setMaxPerPage($this->getLimit()); diff --git a/src/Kunstmaan/AdminListBundle/Tests/AdminList/Helper/DoctrineDBALAdapterTest.php b/src/Kunstmaan/AdminListBundle/Tests/AdminList/Helper/DoctrineDBALAdapterTest.php index a4fb35c730..3b0c39f47c 100644 --- a/src/Kunstmaan/AdminListBundle/Tests/AdminList/Helper/DoctrineDBALAdapterTest.php +++ b/src/Kunstmaan/AdminListBundle/Tests/AdminList/Helper/DoctrineDBALAdapterTest.php @@ -9,6 +9,11 @@ class DoctrineDBALAdapterTest extends TestCase { + /** + * Mark test as legacy to avoid "\Pagerfanta\Exception\Exception" interface deprecation + * + * @group legacy + */ public function testConstructorWithIncorrectCountField() { $this->expectExceptionMessage('The $countField must contain a table alias in the string.'); diff --git a/src/Kunstmaan/AdminListBundle/composer.json b/src/Kunstmaan/AdminListBundle/composer.json index 175d4b6210..2474f342fa 100644 --- a/src/Kunstmaan/AdminListBundle/composer.json +++ b/src/Kunstmaan/AdminListBundle/composer.json @@ -14,8 +14,11 @@ ], "require": { "php": "^7.2", + "box/spout": "^2.5", "kunstmaan/admin-bundle": "^5.7", - "box/spout": "^2.5" + "pagerfanta/core": "^2.4", + "pagerfanta/doctrine-orm-adapter": "^2.4", + "pagerfanta/twig": "^2.4" }, "require-dev": { "matthiasnoback/symfony-config-test": "^4.0", diff --git a/src/Kunstmaan/ArticleBundle/composer.json b/src/Kunstmaan/ArticleBundle/composer.json index 213e269585..28f72cfaed 100644 --- a/src/Kunstmaan/ArticleBundle/composer.json +++ b/src/Kunstmaan/ArticleBundle/composer.json @@ -15,7 +15,9 @@ "require": { "php": "^7.2", "kunstmaan/adminlist-bundle": "^5.2", - "kunstmaan/pagepart-bundle": "^5.2" + "kunstmaan/pagepart-bundle": "^5.2", + "pagerfanta/core": "^2.4", + "pagerfanta/twig": "^2.4" }, "require-dev": { "matthiasnoback/symfony-config-test": "^4.0", diff --git a/src/Kunstmaan/MediaBundle/composer.json b/src/Kunstmaan/MediaBundle/composer.json index 782f47da00..3f426ff19f 100644 --- a/src/Kunstmaan/MediaBundle/composer.json +++ b/src/Kunstmaan/MediaBundle/composer.json @@ -20,7 +20,8 @@ "symfony/mime": "^4.4", "imagine/imagine": "^1.1", "knplabs/knp-gaufrette-bundle": "~0.1", - "kunstmaan/adminlist-bundle": "~5.2" + "kunstmaan/adminlist-bundle": "~5.2", + "pagerfanta/twig": "^2.4" }, "require-dev": { "matthiasnoback/symfony-config-test": "^4.0", diff --git a/src/Kunstmaan/NodeSearchBundle/composer.json b/src/Kunstmaan/NodeSearchBundle/composer.json index b913faff50..8a50e07718 100644 --- a/src/Kunstmaan/NodeSearchBundle/composer.json +++ b/src/Kunstmaan/NodeSearchBundle/composer.json @@ -22,7 +22,9 @@ "php": "^7.2", "kunstmaan/admin-bundle": "^5.7", "kunstmaan/pagepart-bundle": "~5.2", - "kunstmaan/search-bundle": "~5.2" + "kunstmaan/search-bundle": "~5.2", + "pagerfanta/core": "^2.4", + "pagerfanta/twig": "^2.4" }, "require-dev": { "matthiasnoback/symfony-config-test": "^4.0",