Skip to content

Commit

Permalink
Better Cache Strategy Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Mar 17, 2024
1 parent 5e2e5a6 commit 4e58ffb
Show file tree
Hide file tree
Showing 39 changed files with 163 additions and 153 deletions.
96 changes: 48 additions & 48 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
parameters:
ignoreErrors:
-
message: "#^Parameter \\#1 \\$name of static method SpomkyLabs\\\\PwaBundle\\\\CachingStrategy\\\\WorkboxCacheStrategy\\:\\:create\\(\\) expects string, string\\|null given\\.$#"
count: 1
path: src/CachingStrategy/AssetCache.php

-
message: "#^Parameter \\#8 \\$preloadUrls of static method SpomkyLabs\\\\PwaBundle\\\\CachingStrategy\\\\WorkboxCacheStrategy\\:\\:create\\(\\) expects array\\<string\\>, mixed given\\.$#"
count: 1
path: src/CachingStrategy/AssetCache.php

-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#"
count: 1
path: src/CachingStrategy/FontCache.php

-
message: "#^Parameter \\#8 \\$preloadUrls of static method SpomkyLabs\\\\PwaBundle\\\\CachingStrategy\\\\WorkboxCacheStrategy\\:\\:create\\(\\) expects array\\<string\\>, mixed given\\.$#"
count: 1
path: src/CachingStrategy/FontCache.php

-
message: "#^Parameter \\#8 \\$preloadUrls of static method SpomkyLabs\\\\PwaBundle\\\\CachingStrategy\\\\WorkboxCacheStrategy\\:\\:create\\(\\) expects array\\<string\\>, mixed given\\.$#"
count: 1
path: src/CachingStrategy/ResourceCaches.php

-
message: "#^Part \\$this\\-\\>options\\['networkTimeoutSeconds'\\] \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
count: 1
path: src/CachingStrategy/WorkboxCacheStrategy.php

-
message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#"
count: 1
Expand Down Expand Up @@ -346,7 +376,7 @@ parameters:
path: src/Dto/Workbox.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\Workbox has an uninitialized property \\$pageCaches\\. Give it default value or assign it in the constructor\\.$#"
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\Workbox has an uninitialized property \\$resourceCaches\\. Give it default value or assign it in the constructor\\.$#"
count: 1
path: src/Dto/Workbox.php

Expand Down Expand Up @@ -576,7 +606,7 @@ parameters:
path: src/Resources/config/definition/service_worker.php

-
message: "#^Cannot access offset 'page_caches' on mixed\\.$#"
message: "#^Cannot access offset 'resource_caches' on mixed\\.$#"
count: 2
path: src/Resources/config/definition/service_worker.php

Expand Down Expand Up @@ -650,72 +680,42 @@ parameters:
count: 1
path: src/Resources/config/definition/web_client.php

-
message: "#^Part \\$broadcastChannel \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
count: 1
path: src/Service/Plugin/BackgroundSyncPlugin.php

-
message: "#^Part \\$maxRetentionTime \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
count: 1
path: src/Service/Plugin/BackgroundSyncPlugin.php

-
message: "#^Part \\$queueName \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
count: 2
path: src/Service/Plugin/BackgroundSyncPlugin.php

-
message: "#^Parameter \\#2 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#"
count: 1
path: src/Service/Rule/AppendCacheStrategies.php

-
message: "#^Parameter \\#1 \\$name of static method SpomkyLabs\\\\PwaBundle\\\\Service\\\\WorkboxCacheStrategy\\:\\:create\\(\\) expects string, string\\|null given\\.$#"
count: 1
path: src/Service/Rule/AssetCache.php

-
message: "#^Parameter \\#8 \\$preloadUrls of static method SpomkyLabs\\\\PwaBundle\\\\Service\\\\WorkboxCacheStrategy\\:\\:create\\(\\) expects array\\<string\\>, mixed given\\.$#"
count: 1
path: src/Service/Rule/AssetCache.php
path: src/ServiceWorkerRule/AppendCacheStrategies.php

-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#"
count: 1
path: src/Service/Rule/FontCache.php

-
message: "#^Parameter \\#8 \\$preloadUrls of static method SpomkyLabs\\\\PwaBundle\\\\Service\\\\WorkboxCacheStrategy\\:\\:create\\(\\) expects array\\<string\\>, mixed given\\.$#"
message: "#^Strict comparison using \\=\\=\\= between int\\<1, max\\> and 0 will always evaluate to false\\.$#"
count: 1
path: src/Service/Rule/FontCache.php
path: src/ServiceWorkerRule/OfflineFallback.php

-
message: "#^Strict comparison using \\=\\=\\= between int\\<1, max\\> and 0 will always evaluate to false\\.$#"
message: "#^Method SpomkyLabs\\\\PwaBundle\\\\SpomkyLabsPwaBundle\\:\\:loadExtension\\(\\) has parameter \\$config with no value type specified in iterable type array\\.$#"
count: 1
path: src/Service/Rule/OfflineFallback.php
path: src/SpomkyLabsPwaBundle.php

-
message: "#^Parameter \\#8 \\$preloadUrls of static method SpomkyLabs\\\\PwaBundle\\\\Service\\\\WorkboxCacheStrategy\\:\\:create\\(\\) expects array\\<string\\>, mixed given\\.$#"
message: "#^Property SpomkyLabs\\\\PwaBundle\\\\Subscriber\\\\ManifestCompileEventListener\\:\\:\\$jsonOptions type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Service/Rule/PageCaches.php
path: src/Subscriber/ManifestCompileEventListener.php

-
message: "#^Part \\$this\\-\\>options\\['networkTimeoutSeconds'\\] \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
message: "#^Property SpomkyLabs\\\\PwaBundle\\\\Subscriber\\\\PwaDevServerSubscriber\\:\\:\\$jsonOptions type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Service/WorkboxCacheStrategy.php
path: src/Subscriber/PwaDevServerSubscriber.php

-
message: "#^Method SpomkyLabs\\\\PwaBundle\\\\SpomkyLabsPwaBundle\\:\\:loadExtension\\(\\) has parameter \\$config with no value type specified in iterable type array\\.$#"
message: "#^Part \\$broadcastChannel \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
count: 1
path: src/SpomkyLabsPwaBundle.php
path: src/WorkboxPlugin/BackgroundSyncPlugin.php

-
message: "#^Property SpomkyLabs\\\\PwaBundle\\\\Subscriber\\\\ManifestCompileEventListener\\:\\:\\$jsonOptions type has no value type specified in iterable type array\\.$#"
message: "#^Part \\$maxRetentionTime \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
count: 1
path: src/Subscriber/ManifestCompileEventListener.php
path: src/WorkboxPlugin/BackgroundSyncPlugin.php

-
message: "#^Property SpomkyLabs\\\\PwaBundle\\\\Subscriber\\\\PwaDevServerSubscriber\\:\\:\\$jsonOptions type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Subscriber/PwaDevServerSubscriber.php
message: "#^Part \\$queueName \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
count: 2
path: src/WorkboxPlugin/BackgroundSyncPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Service\Rule;
namespace SpomkyLabs\PwaBundle\CachingStrategy;

use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
use SpomkyLabs\PwaBundle\Dto\Workbox;
use SpomkyLabs\PwaBundle\Service\CacheStrategy;
use SpomkyLabs\PwaBundle\Service\HasCacheStrategies;
use SpomkyLabs\PwaBundle\Service\Plugin\ExpirationPlugin;
use SpomkyLabs\PwaBundle\Service\WorkboxCacheStrategy;
use SpomkyLabs\PwaBundle\WorkboxPlugin\ExpirationPlugin;
use Symfony\Component\AssetMapper\AssetMapperInterface;
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolverInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Service\Rule;
namespace SpomkyLabs\PwaBundle\CachingStrategy;

use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
use SpomkyLabs\PwaBundle\Dto\Workbox;
use SpomkyLabs\PwaBundle\Service\CacheStrategy;
use SpomkyLabs\PwaBundle\Service\HasCacheStrategies;
use SpomkyLabs\PwaBundle\Service\MatchCallbackHandler\MatchCallbackHandler;
use SpomkyLabs\PwaBundle\Service\Plugin\BackgroundSyncPlugin;
use SpomkyLabs\PwaBundle\Service\WorkboxCacheStrategy;
use SpomkyLabs\PwaBundle\MatchCallbackHandler\MatchCallbackHandler;
use SpomkyLabs\PwaBundle\WorkboxPlugin\BackgroundSyncPlugin;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;

final readonly class BackgroundSync implements HasCacheStrategies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Service;
namespace SpomkyLabs\PwaBundle\CachingStrategy;

abstract readonly class CacheStrategy
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Service\Rule;
namespace SpomkyLabs\PwaBundle\CachingStrategy;

use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
use SpomkyLabs\PwaBundle\Dto\Workbox;
use SpomkyLabs\PwaBundle\Service\CacheStrategy;
use SpomkyLabs\PwaBundle\Service\HasCacheStrategies;
use SpomkyLabs\PwaBundle\Service\Plugin\CacheableResponsePlugin;
use SpomkyLabs\PwaBundle\Service\Plugin\ExpirationPlugin;
use SpomkyLabs\PwaBundle\Service\WorkboxCacheStrategy;
use SpomkyLabs\PwaBundle\WorkboxPlugin\CacheableResponsePlugin;
use SpomkyLabs\PwaBundle\WorkboxPlugin\ExpirationPlugin;
use Symfony\Component\AssetMapper\AssetMapperInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Serializer\Encoder\JsonEncode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Service\Rule;
namespace SpomkyLabs\PwaBundle\CachingStrategy;

use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
use SpomkyLabs\PwaBundle\Dto\Workbox;
use SpomkyLabs\PwaBundle\Service\CacheStrategy;
use SpomkyLabs\PwaBundle\Service\HasCacheStrategies;
use SpomkyLabs\PwaBundle\Service\Plugin\CacheableResponsePlugin;
use SpomkyLabs\PwaBundle\Service\Plugin\ExpirationPlugin;
use SpomkyLabs\PwaBundle\Service\WorkboxCacheStrategy;
use SpomkyLabs\PwaBundle\WorkboxPlugin\CacheableResponsePlugin;
use SpomkyLabs\PwaBundle\WorkboxPlugin\ExpirationPlugin;

final readonly class GoogleFontCache implements HasCacheStrategies
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Service;
namespace SpomkyLabs\PwaBundle\CachingStrategy;

interface HasCacheStrategies
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Service\Rule;
namespace SpomkyLabs\PwaBundle\CachingStrategy;

use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
use SpomkyLabs\PwaBundle\Dto\Workbox;
use SpomkyLabs\PwaBundle\Service\CacheStrategy;
use SpomkyLabs\PwaBundle\Service\HasCacheStrategies;
use SpomkyLabs\PwaBundle\Service\Plugin\CacheableResponsePlugin;
use SpomkyLabs\PwaBundle\Service\Plugin\ExpirationPlugin;
use SpomkyLabs\PwaBundle\Service\WorkboxCacheStrategy;
use SpomkyLabs\PwaBundle\WorkboxPlugin\CacheableResponsePlugin;
use SpomkyLabs\PwaBundle\WorkboxPlugin\ExpirationPlugin;
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolverInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Service\Rule;
namespace SpomkyLabs\PwaBundle\CachingStrategy;

use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
use SpomkyLabs\PwaBundle\Dto\Workbox;
use SpomkyLabs\PwaBundle\Service\CacheStrategy;
use SpomkyLabs\PwaBundle\Service\HasCacheStrategies;
use SpomkyLabs\PwaBundle\Service\WorkboxCacheStrategy;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use const PHP_EOL;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Service\Rule;
namespace SpomkyLabs\PwaBundle\CachingStrategy;

use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
use SpomkyLabs\PwaBundle\Dto\Workbox;
use SpomkyLabs\PwaBundle\Service\CacheStrategy;
use SpomkyLabs\PwaBundle\Service\HasCacheStrategies;
use SpomkyLabs\PwaBundle\Service\MatchCallbackHandler\MatchCallbackHandler;
use SpomkyLabs\PwaBundle\Service\Plugin\BroadcastUpdatePlugin;
use SpomkyLabs\PwaBundle\Service\Plugin\CacheableResponsePlugin;
use SpomkyLabs\PwaBundle\Service\Plugin\ExpirationPlugin;
use SpomkyLabs\PwaBundle\Service\Plugin\RangeRequestsPlugin;
use SpomkyLabs\PwaBundle\Service\WorkboxCacheStrategy;
use SpomkyLabs\PwaBundle\MatchCallbackHandler\MatchCallbackHandler;
use SpomkyLabs\PwaBundle\WorkboxPlugin\BroadcastUpdatePlugin;
use SpomkyLabs\PwaBundle\WorkboxPlugin\CacheableResponsePlugin;
use SpomkyLabs\PwaBundle\WorkboxPlugin\ExpirationPlugin;
use SpomkyLabs\PwaBundle\WorkboxPlugin\RangeRequestsPlugin;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\Serializer\Encoder\JsonEncode;
Expand All @@ -23,7 +20,7 @@
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;

final readonly class PageCaches implements HasCacheStrategies
final readonly class ResourceCaches implements HasCacheStrategies
{
private int $jsonOptions;

Expand Down Expand Up @@ -51,41 +48,42 @@ public function __construct(
public function getCacheStrategies(): array
{
$strategies = [];
foreach ($this->workbox->pageCaches as $id => $pageCache) {
$routes = $this->serializer->serialize($pageCache->urls, 'json', [
foreach ($this->workbox->resourceCaches as $id => $resourceCache) {
$routes = $this->serializer->serialize($resourceCache->urls, 'json', [
JsonEncode::OPTIONS => $this->jsonOptions,
]);
$url = json_decode($routes, true, 512, JSON_THROW_ON_ERROR);
$cacheName = $pageCache->cacheName ?? sprintf('page-cache-%d', $id);

$cacheName = $resourceCache->cacheName ?? sprintf('page-cache-%d', $id);

$plugins = [
CacheableResponsePlugin::create(
$pageCache->cacheableResponseStatuses,
$pageCache->cacheableResponseHeaders
$resourceCache->cacheableResponseStatuses,
$resourceCache->cacheableResponseHeaders
),
];
if ($pageCache->broadcast === true && $pageCache->strategy === CacheStrategy::STRATEGY_STALE_WHILE_REVALIDATE) {
$plugins[] = BroadcastUpdatePlugin::create($pageCache->broadcastHeaders);
if ($resourceCache->broadcast === true && $resourceCache->strategy === CacheStrategy::STRATEGY_STALE_WHILE_REVALIDATE) {
$plugins[] = BroadcastUpdatePlugin::create($resourceCache->broadcastHeaders);
}
if ($pageCache->rangeRequests === true && $pageCache->strategy !== CacheStrategy::STRATEGY_NETWORK_ONLY) {
if ($resourceCache->rangeRequests === true && $resourceCache->strategy !== CacheStrategy::STRATEGY_NETWORK_ONLY) {
$plugins[] = RangeRequestsPlugin::create();
}
if ($pageCache->maxEntries !== null || $pageCache->maxAgeInSeconds() !== null) {
$plugins[] = ExpirationPlugin::create($pageCache->maxEntries, $pageCache->maxAgeInSeconds());
if ($resourceCache->maxEntries !== null || $resourceCache->maxAgeInSeconds() !== null) {
$plugins[] = ExpirationPlugin::create($resourceCache->maxEntries, $resourceCache->maxAgeInSeconds());
}

$strategies[] =
WorkboxCacheStrategy::create(
$cacheName,
$pageCache->strategy,
$this->prepareMatchCallback($pageCache->matchCallback),
$resourceCache->strategy,
$this->prepareMatchCallback($resourceCache->matchCallback),
$this->workbox->enabled,
true,
null,
$plugins,
$url,
[
'networkTimeoutSeconds' => $pageCache->networkTimeout,
'networkTimeoutSeconds' => $resourceCache->networkTimeout,
]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Service;
namespace SpomkyLabs\PwaBundle\CachingStrategy;

use SpomkyLabs\PwaBundle\Service\Plugin\CachePlugin;
use SpomkyLabs\PwaBundle\WorkboxPlugin\CachePlugin;
use function in_array;

final readonly class WorkboxCacheStrategy extends CacheStrategy
Expand Down Expand Up @@ -73,7 +73,7 @@ public function render(string $cacheObjectName, int $jsonOptions = 0): string
}
$cacheName = '';
if ($this->strategy !== self::STRATEGY_NETWORK_ONLY) {
$cacheName = "cacheName: '{$cacheName}',";
$cacheName = sprintf("cacheName: '%s',", $this->name ?? $cacheObjectName);
}
$plugins = sprintf('[%s]', implode(', ', array_map(
fn (CachePlugin $plugin) => $plugin->render($jsonOptions),
Expand Down
6 changes: 3 additions & 3 deletions src/Command/ListCacheStrategiesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace SpomkyLabs\PwaBundle\Command;

use SpomkyLabs\PwaBundle\Service\HasCacheStrategies;
use SpomkyLabs\PwaBundle\Service\Plugin\CachePlugin;
use SpomkyLabs\PwaBundle\Service\WorkboxCacheStrategy;
use SpomkyLabs\PwaBundle\CachingStrategy\HasCacheStrategies;
use SpomkyLabs\PwaBundle\CachingStrategy\WorkboxCacheStrategy;
use SpomkyLabs\PwaBundle\WorkboxPlugin\CachePlugin;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand Down
Loading

0 comments on commit 4e58ffb

Please sign in to comment.