From a7fd3a7385c8be477a41002594a94193a1275c93 Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Fri, 5 Apr 2024 15:44:30 +0200 Subject: [PATCH] Remove AddPreloadUrlsGeneratorPass and refactor related classes Removed AddPreloadUrlsGeneratorPass as it is no longer needed. Updated PreloadUrlsGeneratorInterface by adding getAlias() method. Made appropriate changes in the PreloadUrlsGeneratorManager class to use this new method. This simplifies the usage and configuration of PreloadUrlsGenerator services. Removed unused compiler pass registration from SpomkyLabsPwaBundle class. --- phpstan-baseline.neon | 8 ++--- .../PreloadUrlsGeneratorInterface.php | 2 ++ .../PreloadUrlsGeneratorManager.php | 18 +++++++++-- src/CachingStrategy/ResourceCaches.php | 2 +- .../Compiler/AddPreloadUrlsGeneratorPass.php | 32 ------------------- src/Resources/config/services.php | 4 +++ src/SpomkyLabsPwaBundle.php | 6 ---- tests/DummyUrlsGenerator.php | 10 ++++-- tests/config.php | 4 +-- 9 files changed, 35 insertions(+), 51 deletions(-) delete mode 100644 src/DependencyInjection/Compiler/AddPreloadUrlsGeneratorPass.php diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index b5f0f29..7c77dde 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -36,17 +36,17 @@ parameters: path: src/CachingStrategy/FontCache.php - - message: "#^Only iterables can be unpacked, mixed given in argument \\#1\\.$#" + message: "#^Foreach overwrites \\$generator with its value variable\\.$#" count: 1 - path: src/CachingStrategy/ResourceCaches.php + path: src/CachingStrategy/PreloadUrlsGeneratorManager.php - - message: "#^Parameter \\#1 \\$preloadUrl of method SpomkyLabs\\\\PwaBundle\\\\CachingStrategy\\\\WorkboxCacheStrategy\\:\\:withPreloadUrl\\(\\) expects string, mixed given\\.$#" + message: "#^Method SpomkyLabs\\\\PwaBundle\\\\CachingStrategy\\\\ResourceCaches\\:\\:prepareMatchCallback\\(\\) is unused\\.$#" count: 1 path: src/CachingStrategy/ResourceCaches.php - - message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#" + message: "#^Unreachable statement \\- code above always terminates\\.$#" count: 1 path: src/CachingStrategy/ResourceCaches.php diff --git a/src/CachingStrategy/PreloadUrlsGeneratorInterface.php b/src/CachingStrategy/PreloadUrlsGeneratorInterface.php index c85921f..dbc3cd1 100644 --- a/src/CachingStrategy/PreloadUrlsGeneratorInterface.php +++ b/src/CachingStrategy/PreloadUrlsGeneratorInterface.php @@ -8,6 +8,8 @@ interface PreloadUrlsGeneratorInterface { + public function getAlias(): string; + /** * @return iterable */ diff --git a/src/CachingStrategy/PreloadUrlsGeneratorManager.php b/src/CachingStrategy/PreloadUrlsGeneratorManager.php index 97f0e18..e081185 100644 --- a/src/CachingStrategy/PreloadUrlsGeneratorManager.php +++ b/src/CachingStrategy/PreloadUrlsGeneratorManager.php @@ -5,6 +5,7 @@ namespace SpomkyLabs\PwaBundle\CachingStrategy; use InvalidArgumentException; +use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; use function array_key_exists; final class PreloadUrlsGeneratorManager @@ -14,9 +15,22 @@ final class PreloadUrlsGeneratorManager */ private array $generators = []; - public function add(string $alias, PreloadUrlsGeneratorInterface $generator): void + /** + * @param PreloadUrlsGeneratorInterface[] $generators + */ + public function __construct( + #[TaggedIterator('spomky_labs_pwa.preload_urls_generator')] + iterable $generators + ) { + $this->add(...$generators); + } + + public function add(PreloadUrlsGeneratorInterface $generator, PreloadUrlsGeneratorInterface ...$generators): void { - $this->generators[$alias] = $generator; + $this->generators[$generator->getAlias()] = $generator; + foreach ($generators as $generator) { + $this->generators[$generator->getAlias()] = $generator; + } } public function get(string $alias): PreloadUrlsGeneratorInterface diff --git a/src/CachingStrategy/ResourceCaches.php b/src/CachingStrategy/ResourceCaches.php index b1dbb8c..69c27fe 100644 --- a/src/CachingStrategy/ResourceCaches.php +++ b/src/CachingStrategy/ResourceCaches.php @@ -56,7 +56,7 @@ public function getCacheStrategies(): array JsonEncode::OPTIONS => $this->jsonOptions, ]); $urls = json_decode($routes, true, 512, JSON_THROW_ON_ERROR); - + dd($urls); $cacheName = $resourceCache->cacheName ?? sprintf('page-cache-%d', $id); $plugins = [ diff --git a/src/DependencyInjection/Compiler/AddPreloadUrlsGeneratorPass.php b/src/DependencyInjection/Compiler/AddPreloadUrlsGeneratorPass.php deleted file mode 100644 index db465dd..0000000 --- a/src/DependencyInjection/Compiler/AddPreloadUrlsGeneratorPass.php +++ /dev/null @@ -1,32 +0,0 @@ -has(PreloadUrlsGeneratorManager::class)) { - return; - } - $definition = $container->findDefinition(PreloadUrlsGeneratorManager::class); - foreach ($container->findTaggedServiceIds(self::TAG) as $id => $tags) { - foreach ($tags as $attributes) { - if (! isset($attributes['alias'])) { - throw new InvalidArgumentException('The alias is required'); - } - $definition->addMethodCall('add', [$attributes['alias'], new Reference($id)]); - } - } - } -} diff --git a/src/Resources/config/services.php b/src/Resources/config/services.php index 9831c93..88b3f56 100644 --- a/src/Resources/config/services.php +++ b/src/Resources/config/services.php @@ -26,6 +26,7 @@ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use Symfony\Component\Mime\MimeTypes; use Symfony\Component\Panther\Client; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use function Symfony\Component\DependencyInjection\Loader\Configurator\param; use function Symfony\Component\DependencyInjection\Loader\Configurator\service; @@ -127,4 +128,7 @@ $container->load('SpomkyLabs\\PwaBundle\\MatchCallbackHandler\\', '../../MatchCallbackHandler/*'); $container->set(PreloadUrlsGeneratorManager::class); + $container->instanceof(UrlGeneratorInterface::class) + ->tag('spomky_labs_pwa.preload_urls_generator') + ; }; diff --git a/src/SpomkyLabsPwaBundle.php b/src/SpomkyLabsPwaBundle.php index 1af1d51..f698c7f 100644 --- a/src/SpomkyLabsPwaBundle.php +++ b/src/SpomkyLabsPwaBundle.php @@ -4,7 +4,6 @@ namespace SpomkyLabs\PwaBundle; -use SpomkyLabs\PwaBundle\DependencyInjection\Compiler\AddPreloadUrlsGeneratorPass; use SpomkyLabs\PwaBundle\ImageProcessor\ImageProcessorInterface; use SpomkyLabs\PwaBundle\Subscriber\PwaDevServerSubscriber; use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator; @@ -58,11 +57,6 @@ public function prependExtension(ContainerConfigurator $container, ContainerBuil $this->setAssetMapperPath($builder); } - public function build(ContainerBuilder $container): void - { - $container->addCompilerPass(new AddPreloadUrlsGeneratorPass()); - } - private function setAssetMapperPath(ContainerBuilder $builder): void { $builder->prependExtensionConfig('framework', [ diff --git a/tests/DummyUrlsGenerator.php b/tests/DummyUrlsGenerator.php index e33f470..b6eff14 100644 --- a/tests/DummyUrlsGenerator.php +++ b/tests/DummyUrlsGenerator.php @@ -5,17 +5,21 @@ namespace SpomkyLabs\PwaBundle\Tests; use SpomkyLabs\PwaBundle\CachingStrategy\PreloadUrlsGeneratorInterface; +use SpomkyLabs\PwaBundle\Dto\Url; /** * @internal */ class DummyUrlsGenerator implements PreloadUrlsGeneratorInterface { + public function getAlias(): string + { + return 'dummy'; + } + public function generateUrls(): iterable { yield '/dummy/1'; - yield '/dummy/2'; - yield '/dummy/3'; - yield '/dummy/4'; + yield Url::create('/dummy/2'); } } diff --git a/tests/config.php b/tests/config.php index 3e9a96f..196a3f9 100644 --- a/tests/config.php +++ b/tests/config.php @@ -22,9 +22,7 @@ ; $container->services() ->set(DummyUrlsGenerator::class) - ->tag('spomky_labs_pwa.preload_urls_generator', [ - 'alias' => 'dummy', - ]) + ->tag('spomky_labs_pwa.preload_urls_generator') ; $container->extension('framework', [