Skip to content

Commit

Permalink
Remove AddPreloadUrlsGeneratorPass and refactor related classes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Spomky committed Apr 5, 2024
1 parent 7f1d07b commit a7fd3a7
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 51 deletions.
8 changes: 4 additions & 4 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

interface PreloadUrlsGeneratorInterface
{
public function getAlias(): string;

/**
* @return iterable<Url|string>
*/
Expand Down
18 changes: 16 additions & 2 deletions src/CachingStrategy/PreloadUrlsGeneratorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace SpomkyLabs\PwaBundle\CachingStrategy;

use InvalidArgumentException;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use function array_key_exists;

final class PreloadUrlsGeneratorManager
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/CachingStrategy/ResourceCaches.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
32 changes: 0 additions & 32 deletions src/DependencyInjection/Compiler/AddPreloadUrlsGeneratorPass.php

This file was deleted.

4 changes: 4 additions & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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')
;
};
6 changes: 0 additions & 6 deletions src/SpomkyLabsPwaBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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', [
Expand Down
10 changes: 7 additions & 3 deletions tests/DummyUrlsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
4 changes: 1 addition & 3 deletions tests/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down

0 comments on commit a7fd3a7

Please sign in to comment.