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 1ef2470 commit 5e2e5a6
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 93 deletions.
82 changes: 71 additions & 11 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,22 @@ parameters:
path: src/Dto/BackgroundSync.php

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

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

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

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

Expand Down Expand Up @@ -176,12 +176,12 @@ parameters:
path: src/Dto/Manifest.php

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

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\PageCache has an uninitialized property \\$regex\\. Give it default value or assign it in the constructor\\.$#"
message: "#^PHPDoc tag @var for property SpomkyLabs\\\\PwaBundle\\\\Dto\\\\PageCache\\:\\:\\$cacheableResponseHeaders with type array\\<string, string\\>\\|null is not subtype of native type array\\.$#"
count: 1
path: src/Dto/PageCache.php

Expand Down Expand Up @@ -492,14 +492,19 @@ parameters:

-
message: "#^Anonymous function should return array but returns mixed\\.$#"
count: 6
count: 8
path: src/Resources/config/definition/service_worker.php

-
message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeDefinition\\:\\:children\\(\\)\\.$#"
count: 1
path: src/Resources/config/definition/service_worker.php

-
message: "#^Cannot access an offset on mixed\\.$#"
count: 1
path: src/Resources/config/definition/service_worker.php

-
message: "#^Cannot access offset 'asset_cache' on mixed\\.$#"
count: 2
Expand Down Expand Up @@ -560,11 +565,31 @@ parameters:
count: 1
path: src/Resources/config/definition/service_worker.php

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

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

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

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

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

-
message: "#^Strict comparison using \\!\\=\\= between mixed and null will always evaluate to true\\.$#"
count: 3
Expand Down Expand Up @@ -626,25 +651,60 @@ parameters:
path: src/Resources/config/definition/web_client.php

-
message: "#^Parameter \\#6 \\$options of static method SpomkyLabs\\\\PwaBundle\\\\Service\\\\CacheStrategy\\:\\:create\\(\\) expects array\\{maxTimeout\\?\\: int, maxAge\\?\\: int, maxEntries\\?\\: int, warmUrls\\?\\: array\\<string\\>, plugins\\?\\: array\\<string\\>\\}, array\\{maxEntries\\: int\\<0, max\\>, maxAge\\: int, warmUrls\\: mixed\\} given\\.$#"
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

-
message: "#^Parameter \\#6 \\$options of static method SpomkyLabs\\\\PwaBundle\\\\Service\\\\CacheStrategy\\:\\:create\\(\\) expects array\\{maxTimeout\\?\\: int, maxAge\\?\\: int, maxEntries\\?\\: int, warmUrls\\?\\: array\\<string\\>, plugins\\?\\: array\\<string\\>\\}, array\\{maxAge\\: int\\|null, maxEntries\\: int\\|null\\} given\\.$#"
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#"
count: 1
path: src/Service/Rule/GoogleFontCache.php
path: src/Service/Rule/FontCache.php

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

-
message: "#^Strict comparison using \\=\\=\\= between int\\<1, max\\> and 0 will always evaluate to false\\.$#"
count: 1
path: src/Service/Rule/OfflineFallback.php

-
message: "#^Parameter \\#6 \\$options of static method SpomkyLabs\\\\PwaBundle\\\\Service\\\\CacheStrategy\\:\\:create\\(\\) expects array\\{maxTimeout\\?\\: int, maxAge\\?\\: int, maxEntries\\?\\: int, warmUrls\\?\\: array\\<string\\>, plugins\\?\\: array\\<string\\>\\}, array\\{maxTimeout\\: int, plugins\\: array\\{0\\: 'CacheableResponsePl…', 1\\?\\: 'BroadcastUpdatePlug…'\\}, warmUrls\\: mixed\\} given\\.$#"
message: "#^Parameter \\#8 \\$preloadUrls of static method SpomkyLabs\\\\PwaBundle\\\\Service\\\\WorkboxCacheStrategy\\:\\:create\\(\\) expects array\\<string\\>, mixed given\\.$#"
count: 1
path: src/Service/Rule/PageCaches.php

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

-
message: "#^Method SpomkyLabs\\\\PwaBundle\\\\SpomkyLabsPwaBundle\\:\\:loadExtension\\(\\) has parameter \\$config with no value type specified in iterable type array\\.$#"
count: 1
Expand Down
4 changes: 2 additions & 2 deletions src/Command/ListCacheStrategiesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
} else {
$table->addRow([
$strategy->name,
$strategy->strategy,
$strategy->matchCallback,
'---',
'---',
$strategy->enabled ? 'Yes' : 'No',
$strategy->requireWorkbox ? 'Yes' : 'No',
'',
Expand Down
7 changes: 6 additions & 1 deletion src/Dto/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use DateInterval;
use DateTimeImmutable;
use InvalidArgumentException;
use Symfony\Component\Serializer\Attribute\SerializedName;
use function is_string;

Expand All @@ -27,7 +28,11 @@ public function maxAgeInSeconds(): null|int
}
if (is_string($this->maxAge)) {
$now = new DateTimeImmutable();
$future = $now->add(DateInterval::createFromDateString($this->maxAge));
$interval = DateInterval::createFromDateString($this->maxAge);
if ($interval === false) {
throw new InvalidArgumentException('Invalid max age');
}
$future = $now->add($interval);
return abs($future->getTimestamp() - $now->getTimestamp());
}
return $this->maxAge;
Expand Down
17 changes: 17 additions & 0 deletions src/Service/Plugin/BackgroundSyncPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,21 @@ public function render(int $jsonOptions = 0): string

return trim($declaration);
}

public static function create(
string $queueName,
int $maxRetentionTime,
bool $forceSyncFallback,
?string $broadcastChannel
): static {
return new self(
'BackgroundSyncPlugin',
[
'queueName' => $queueName,
'maxRetentionTime' => $maxRetentionTime,
'forceSyncFallback' => $forceSyncFallback,
'broadcastChannel' => $broadcastChannel,
]
);
}
}
12 changes: 12 additions & 0 deletions src/Service/Plugin/BroadcastUpdatePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@ public function render(int $jsonOptions = 0): string
json_encode($this->options, $jsonOptions)
);
}

/**
* @param array<string, string> $headersToCheck
*/
public static function create(array $headersToCheck = []): static
{
$headersToCheck = $headersToCheck === [] ? ['Content-Type', 'ETag', 'Last-Modified'] : $headersToCheck;

return new self('BroadcastUpdatePlugin', [
'headersToCheck' => $headersToCheck,
]);
}
}
58 changes: 0 additions & 58 deletions src/Service/Plugin/CachePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,62 +16,4 @@ public function __construct(
}

abstract public function render(int $jsonOptions = 0): string;

public static function createExpirationPlugin(null|int $maxEntries, null|string|int $maxAgeSeconds): static
{
return new ExpirationPlugin(
'ExpirationPlugin',
[
'maxEntries' => $maxEntries,
'maxAgeSeconds' => $maxAgeSeconds,
]
);
}

public static function createBroadcastUpdatePlugin(array $headersToCheck = []): static
{
$headersToCheck = $headersToCheck === [] ? ['Content-Type', 'ETag', 'Last-Modified'] : $headersToCheck;

return new BroadcastUpdatePlugin(
'BroadcastUpdatePlugin',
[
'headersToCheck' => $headersToCheck,
]
);
}

public static function createCacheableResponsePlugin(array $statuses = [0, 200], array $headers = []): static
{
$options = array_filter([
'statuses' => $statuses,
'headers' => $headers,
], fn ($value) => $value !== []);
$options = $options === [] ? [
'statuses' => [0, 200],
] : $options;

return new CacheableResponsePlugin('CacheableResponsePlugin', $options);
}

public static function createRangeRequestsPlugin(): static
{
return new RangeRequestsPlugin('RangeRequestsPlugin');
}

public static function createBackgroundSyncPlugin(
string $queueName,
int $maxRetentionTime,
bool $forceSyncFallback,
?string $broadcastChannel
): static {
return new BackgroundSyncPlugin(
'BackgroundSyncPlugin',
[
'queueName' => $queueName,
'maxRetentionTime' => $maxRetentionTime,
'forceSyncFallback' => $forceSyncFallback,
'broadcastChannel' => $broadcastChannel,
]
);
}
}
17 changes: 17 additions & 0 deletions src/Service/Plugin/CacheableResponsePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,21 @@ public function render(int $jsonOptions = 0): string
json_encode($this->options, $jsonOptions)
);
}

/**
* @param array<int> $statuses
* @param array<string, string> $headers
*/
public static function create(array $statuses = [0, 200], array $headers = []): static
{
$options = array_filter([
'statuses' => $statuses,
'headers' => $headers,
], fn ($value) => $value !== []);
$options = $options === [] ? [
'statuses' => [0, 200],
] : $options;

return new self('CacheableResponsePlugin', $options);
}
}
8 changes: 8 additions & 0 deletions src/Service/Plugin/ExpirationPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ public function render(int $jsonOptions = 0): string
{
return sprintf('new workbox.expiration.ExpirationPlugin(%s)', json_encode($this->options, $jsonOptions));
}

public static function create(null|int $maxEntries, null|string|int $maxAgeSeconds): static
{
return new self('ExpirationPlugin', [
'maxEntries' => $maxEntries,
'maxAgeSeconds' => $maxAgeSeconds,
]);
}
}
5 changes: 5 additions & 0 deletions src/Service/Plugin/RangeRequestsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ public function render(int $jsonOptions = 0): string
{
return 'new workbox.rangeRequests.RangeRequestsPlugin()';
}

public static function create(): static
{
return new self('RangeRequestsPlugin');
}
}
4 changes: 2 additions & 2 deletions src/Service/Rule/AssetCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use SpomkyLabs\PwaBundle\Dto\Workbox;
use SpomkyLabs\PwaBundle\Service\CacheStrategy;
use SpomkyLabs\PwaBundle\Service\HasCacheStrategies;
use SpomkyLabs\PwaBundle\Service\Plugin\CachePlugin;
use SpomkyLabs\PwaBundle\Service\Plugin\ExpirationPlugin;
use SpomkyLabs\PwaBundle\Service\WorkboxCacheStrategy;
use Symfony\Component\AssetMapper\AssetMapperInterface;
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolverInterface;
Expand Down Expand Up @@ -61,7 +61,7 @@ public function getCacheStrategies(): array
true,
null,
[
CachePlugin::createExpirationPlugin(
ExpirationPlugin::create(
count($this->getAssets()) * 2,
$this->workbox->assetCache->maxAgeInSeconds() ?? 60 * 60 * 24 * 365,
),
Expand Down
11 changes: 6 additions & 5 deletions src/Service/Rule/BackgroundSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@
use SpomkyLabs\PwaBundle\Dto\Workbox;
use SpomkyLabs\PwaBundle\Service\CacheStrategy;
use SpomkyLabs\PwaBundle\Service\HasCacheStrategies;
use SpomkyLabs\PwaBundle\Service\Plugin\CachePlugin;
use SpomkyLabs\PwaBundle\Service\MatchCallbackHandler\MatchCallbackHandler;
use SpomkyLabs\PwaBundle\Service\Plugin\BackgroundSyncPlugin;
use SpomkyLabs\PwaBundle\Service\WorkboxCacheStrategy;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;

final readonly class BackgroundSync implements HasCacheStrategies
{
private Workbox $workbox;

/**
* @param iterable<MatchCallbackHandler> $matchCallbackHandlers
*/
public function __construct(
ServiceWorker $serviceWorker,
#[TaggedIterator('spomky_labs_pwa.match_callback_handler')]
private iterable $matchCallbackHandlers,
#[Autowire('%kernel.debug%')]
bool $debug,
) {
$this->workbox = $serviceWorker->workbox;
}
Expand All @@ -42,7 +43,7 @@ public function getCacheStrategies(): array
true,
null,
[
CachePlugin::createBackgroundSyncPlugin(
BackgroundSyncPlugin::create(
$sync->queueName,
$sync->maxRetentionTime,
$sync->forceSyncFallback,
Expand Down
Loading

0 comments on commit 5e2e5a6

Please sign in to comment.