-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add preload URLs generator and refactored caching strategy
Introduced a preload URLs generator to efficiently handle URLs and refactored caching strategy to enhance efficiency. The changes include creating a preload URLs generator manager, implementing a dummy URLs generator for testing, and updating cache resource classes. Deprecated PageCache in favor of ResourceCache for better flexibility in handling resources.
- Loading branch information
Showing
14 changed files
with
226 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpomkyLabs\PwaBundle\CachingStrategy; | ||
|
||
use SpomkyLabs\PwaBundle\Dto\Url; | ||
|
||
interface PreloadUrlsGeneratorInterface | ||
{ | ||
/** | ||
* @return iterable<Url> | ||
*/ | ||
public function generateUrls(): iterable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpomkyLabs\PwaBundle\CachingStrategy; | ||
|
||
use InvalidArgumentException; | ||
use function array_key_exists; | ||
|
||
final class PreloadUrlsGeneratorManager | ||
{ | ||
/** | ||
* @var array<string, PreloadUrlsGeneratorInterface> | ||
*/ | ||
private array $generators = []; | ||
|
||
public function add(string $alias, PreloadUrlsGeneratorInterface $generator): void | ||
{ | ||
$this->generators[$alias] = $generator; | ||
} | ||
|
||
public function get(string $alias): PreloadUrlsGeneratorInterface | ||
{ | ||
if (! array_key_exists($alias, $this->generators)) { | ||
throw new InvalidArgumentException(sprintf('The generator with alias "%s" does not exist.', $alias)); | ||
} | ||
return $this->generators[$alias]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/DependencyInjection/Compiler/AddPreloadUrlsGeneratorPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpomkyLabs\PwaBundle\DependencyInjection\Compiler; | ||
|
||
use InvalidArgumentException; | ||
use SpomkyLabs\PwaBundle\CachingStrategy\PreloadUrlsGeneratorManager; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
final readonly class AddPreloadUrlsGeneratorPass implements CompilerPassInterface | ||
{ | ||
public const TAG = 'spomky_labs_pwa.preload_urls_generator'; | ||
|
||
public function process(ContainerBuilder $container): void | ||
{ | ||
if (! $container->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)]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpomkyLabs\PwaBundle\Dto; | ||
|
||
use Symfony\Component\Serializer\Attribute\SerializedName; | ||
|
||
/** | ||
* @final | ||
*/ | ||
class ResourceCache extends Cache | ||
{ | ||
#[SerializedName('match_callback')] | ||
public string $matchCallback; | ||
|
||
#[SerializedName('network_timeout')] | ||
public int $networkTimeout = 3; | ||
|
||
public string $strategy = 'NetworkFirst'; | ||
|
||
public bool $broadcast = false; | ||
|
||
#[SerializedName('range_requests')] | ||
public bool $rangeRequests = false; | ||
|
||
/** | ||
* @var int[] | ||
*/ | ||
#[SerializedName('cacheable_response_statuses')] | ||
public array $cacheableResponseStatuses = [0, 200]; | ||
|
||
/** | ||
* @var null|array<string, string> | ||
*/ | ||
#[SerializedName('cacheable_response_headers')] | ||
public array $cacheableResponseHeaders = []; | ||
|
||
/** | ||
* @var array<string> | ||
*/ | ||
#[SerializedName('broadcast_headers')] | ||
public array $broadcastHeaders = ['Content-Type', 'ETag', 'Last-Modified']; | ||
|
||
/** | ||
* @var array<Url> | ||
*/ | ||
#[SerializedName('preload_urls')] | ||
public array $urls = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpomkyLabs\PwaBundle\Tests; | ||
|
||
use SpomkyLabs\PwaBundle\CachingStrategy\PreloadUrlsGeneratorInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class DummyUrlsGenerator implements PreloadUrlsGeneratorInterface | ||
{ | ||
public function generateUrls(): iterable | ||
{ | ||
yield '/dummy/1'; | ||
yield '/dummy/2'; | ||
yield '/dummy/3'; | ||
yield '/dummy/4'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.