Skip to content

Commit

Permalink
Cache as interfaces (#149)
Browse files Browse the repository at this point in the history
features: CachePlugin and CacheStrategy are now interfaces
  • Loading branch information
Spomky committed Mar 21, 2024
1 parent fdc1e61 commit 21503ac
Show file tree
Hide file tree
Showing 26 changed files with 609 additions and 315 deletions.
50 changes: 30 additions & 20 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
parameters:
ignoreErrors:
-
message: "#^Parameter \\#1 \\$name of static method SpomkyLabs\\\\PwaBundle\\\\CachingStrategy\\\\WorkboxCacheStrategy\\:\\:create\\(\\) expects string, string\\|null given\\.$#"
message: "#^Only iterables can be unpacked, mixed given in argument \\#1\\.$#"
count: 1
path: src/CachingStrategy/AssetCache.php

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

-
message: "#^Parameter \\#1 \\$preloadUrl of method SpomkyLabs\\\\PwaBundle\\\\CachingStrategy\\\\WorkboxCacheStrategy\\:\\:withPreloadUrl\\(\\) expects 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/AssetCache.php

-
message: "#^Only iterables can be unpacked, mixed given in argument \\#1\\.$#"
count: 1
path: src/CachingStrategy/FontCache.php

-
message: "#^Parameter \\#8 \\$preloadUrls of static method SpomkyLabs\\\\PwaBundle\\\\CachingStrategy\\\\WorkboxCacheStrategy\\:\\:create\\(\\) expects array\\<string\\>, mixed given\\.$#"
message: "#^Parameter \\#1 \\$preloadUrl of method SpomkyLabs\\\\PwaBundle\\\\CachingStrategy\\\\WorkboxCacheStrategy\\:\\:withPreloadUrl\\(\\) expects 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\\.$#"
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#"
count: 2
path: src/CachingStrategy/FontCache.php

-
message: "#^Only iterables can be unpacked, mixed given in argument \\#1\\.$#"
count: 1
path: src/CachingStrategy/ResourceCaches.php

-
message: "#^Parameter \\#1 \\$preloadUrl of method SpomkyLabs\\\\PwaBundle\\\\CachingStrategy\\\\WorkboxCacheStrategy\\:\\:withPreloadUrl\\(\\) expects string, mixed given\\.$#"
count: 1
path: src/CachingStrategy/ResourceCaches.php

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

Expand Down Expand Up @@ -703,19 +728,4 @@ parameters:
-
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 \\$broadcastChannel \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
count: 1
path: src/WorkboxPlugin/BackgroundSyncPlugin.php

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

-
message: "#^Part \\$queueName \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
count: 2
path: src/WorkboxPlugin/BackgroundSyncPlugin.php
path: src/Subscriber/PwaDevServerSubscriber.php
36 changes: 19 additions & 17 deletions src/CachingStrategy/AssetCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,25 @@ public function getCacheStrategies(): array
$urls = json_decode($this->serializer->serialize($this->getAssets(), 'json', [
JsonEncode::OPTIONS => $this->jsonOptions,
]), true);
return [
WorkboxCacheStrategy::create(
$this->workbox->assetCache->cacheName,
CacheStrategy::STRATEGY_CACHE_FIRST,
sprintf("({url}) => url.pathname.startsWith('%s')", $this->assetPublicPrefix),
$this->workbox->enabled && $this->workbox->assetCache->enabled,
true,
null,
[
ExpirationPlugin::create(
count($this->getAssets()) * 2,
$this->workbox->assetCache->maxAgeInSeconds() ?? 60 * 60 * 24 * 365,
),
],
$urls
),
];

$strategy = WorkboxCacheStrategy::create(
$this->workbox->enabled && $this->workbox->assetCache->enabled,
true,
CacheStrategy::STRATEGY_CACHE_FIRST,
sprintf("({url}) => url.pathname.startsWith('%s')", $this->assetPublicPrefix),
)
->withName($this->workbox->assetCache->cacheName)
->withPlugin(
ExpirationPlugin::create(
count($this->getAssets()) * 2,
$this->workbox->assetCache->maxAgeInSeconds() ?? 60 * 60 * 24 * 365,
),
);

if (count($urls) > 0) {
$strategy = $strategy->withPreloadUrl(...$urls);
}
return [$strategy];
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/CachingStrategy/BackgroundSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ public function getCacheStrategies(): array
$strategies = [];
foreach ($this->workbox->backgroundSync as $sync) {
$strategies[] = WorkboxCacheStrategy::create(
'BackgroundSync API',
CacheStrategy::STRATEGY_NETWORK_ONLY,
$this->prepareMatchCallback($sync->matchCallback),
$this->workbox->enabled,
true,
null,
[
CacheStrategy::STRATEGY_NETWORK_ONLY,
$this->prepareMatchCallback($sync->matchCallback)
)
->withName('Background Sync')
->withPlugin(
BackgroundSyncPlugin::create(
$sync->queueName,
$sync->maxRetentionTime,
$sync->forceSyncFallback,
$sync->broadcastChannel
),
]
);
)
->withMethod($sync->method);
}

return $strategies;
Expand Down
15 changes: 7 additions & 8 deletions src/CachingStrategy/CacheStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace SpomkyLabs\PwaBundle\CachingStrategy;

abstract readonly class CacheStrategy
interface CacheStrategy
{
public const STRATEGY_CACHE_FIRST = 'CacheFirst';

Expand All @@ -24,12 +24,11 @@
self::STRATEGY_STALE_WHILE_REVALIDATE,
];

public function __construct(
public string $name,
public bool $enabled,
public bool $requireWorkbox,
) {
}
public function getName(): ?string;

abstract public function render(string $cacheObjectName, int $jsonOptions = 0): string;
public function isEnabled(): bool;

public function needsWorkbox(): bool;

public function render(string $cacheObjectName, bool $debug = false): string;
}
38 changes: 20 additions & 18 deletions src/CachingStrategy/FontCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,26 @@ public function getCacheStrategies(): array
]), true);
$maxEntries = count($urls) + ($this->workbox->fontCache->maxEntries ?? 60);

return [
WorkboxCacheStrategy::create(
$this->workbox->fontCache->cacheName ?? 'fonts',
CacheStrategy::STRATEGY_CACHE_FIRST,
"({request}) => request.destination === 'font'",
$this->workbox->enabled && $this->workbox->fontCache->enabled,
true,
null,
[
ExpirationPlugin::create(
$maxEntries,
$this->workbox->fontCache->maxAgeInSeconds() ?? 60 * 60 * 24 * 365,
),
CacheableResponsePlugin::create(),
],
$urls
),
];
$strategy = WorkboxCacheStrategy::create(
$this->workbox->enabled && $this->workbox->fontCache->enabled,
true,
CacheStrategy::STRATEGY_CACHE_FIRST,
"({request}) => request.destination === 'font'"
)
->withName($this->workbox->fontCache->cacheName ?? 'fonts')
->withMethod('GET')
->withPlugin(
CacheableResponsePlugin::create(),
ExpirationPlugin::create(
$maxEntries,
$this->workbox->fontCache->maxAgeInSeconds() ?? 60 * 60 * 24 * 365
),
);
if (count($urls) > 0) {
$strategy = $strategy->withPreloadUrl(...$urls);
}

return [$strategy];
}

/**
Expand Down
21 changes: 10 additions & 11 deletions src/CachingStrategy/GoogleFontCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,26 @@ public function getCacheStrategies(): array

return [
WorkboxCacheStrategy::create(
$prefix . 'google-fonts-stylesheets',
$this->workbox->enabled && $this->workbox->googleFontCache->enabled,
true,
CacheStrategy::STRATEGY_STALE_WHILE_REVALIDATE,
"({url}) => url.origin === 'https://fonts.googleapis.com'",
$this->workbox->enabled && $this->workbox->googleFontCache->enabled,
true
),
)
->withName($prefix . 'google-fonts-stylesheets'),
WorkboxCacheStrategy::create(
$prefix . 'google-fonts-webfonts',
CacheStrategy::STRATEGY_CACHE_FIRST,
"({url}) => url.origin === 'https://fonts.gstatic.com'",
$this->workbox->enabled && $this->workbox->googleFontCache->enabled,
true,
null,
[
CacheStrategy::STRATEGY_CACHE_FIRST,
"({url}) => url.origin === 'https://fonts.gstatic.com'"
)
->withName($prefix . 'google-fonts-webfonts')
->withPlugin(
CacheableResponsePlugin::create(),
ExpirationPlugin::create(
$this->workbox->googleFontCache->maxAgeInSeconds() ?? 60 * 60 * 24 * 365,
$this->workbox->googleFontCache->maxEntries ?? 30
),
]
),
),
];
}
}
20 changes: 5 additions & 15 deletions src/CachingStrategy/ImageCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
use SpomkyLabs\PwaBundle\Dto\Workbox;
use SpomkyLabs\PwaBundle\WorkboxPlugin\CacheableResponsePlugin;
use SpomkyLabs\PwaBundle\WorkboxPlugin\ExpirationPlugin;
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolverInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

Expand All @@ -30,23 +28,15 @@ public function getCacheStrategies(): array
{
return [
WorkboxCacheStrategy::create(
$this->workbox->imageCache->cacheName ?? 'images',
$this->workbox->enabled && $this->workbox->imageCache->enabled,
true,
CacheStrategy::STRATEGY_CACHE_FIRST,
sprintf(
"({request, url}) => (request.destination === 'image' && !url.pathname.startsWith('%s'))",
$this->assetPublicPrefix
),
$this->workbox->enabled && $this->workbox->imageCache->enabled,
true,
null,
[
CacheableResponsePlugin::create(),
ExpirationPlugin::create(
$this->workbox->imageCache->maxEntries ?? 60,
$this->workbox->imageCache->maxAgeInSeconds() ?? 60 * 60 * 24 * 7
),
]
),
)
)
->withName($this->workbox->imageCache->cacheName ?? 'images'),
];
}
}
8 changes: 4 additions & 4 deletions src/CachingStrategy/ManifestCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public function getCacheStrategies(): array
{
return [
WorkboxCacheStrategy::create(
'manifest',
$this->workbox->enabled && $this->workbox->cacheManifest,
true,
CacheStrategy::STRATEGY_STALE_WHILE_REVALIDATE,
sprintf("({url}) => '%s' === url.pathname", $this->manifestPublicUrl),
$this->workbox->enabled && $this->workbox->cacheManifest,
true
),
)
->withName('manifest'),
];
}
}
33 changes: 18 additions & 15 deletions src/CachingStrategy/ResourceCaches.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\Serializer\Encoder\JsonEncode;
use Symfony\Component\Serializer\SerializerInterface;
use function count;
use const JSON_PRETTY_PRINT;
use const JSON_THROW_ON_ERROR;
use const JSON_UNESCAPED_SLASHES;
Expand Down Expand Up @@ -52,7 +53,7 @@ public function getCacheStrategies(): array
$routes = $this->serializer->serialize($resourceCache->urls, 'json', [
JsonEncode::OPTIONS => $this->jsonOptions,
]);
$url = json_decode($routes, true, 512, JSON_THROW_ON_ERROR);
$urls = json_decode($routes, true, 512, JSON_THROW_ON_ERROR);

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

Expand All @@ -72,20 +73,22 @@ public function getCacheStrategies(): array
$plugins[] = ExpirationPlugin::create($resourceCache->maxEntries, $resourceCache->maxAgeInSeconds());
}

$strategies[] =
WorkboxCacheStrategy::create(
$cacheName,
$resourceCache->strategy,
$this->prepareMatchCallback($resourceCache->matchCallback),
$this->workbox->enabled,
true,
null,
$plugins,
$url,
[
'networkTimeoutSeconds' => $resourceCache->networkTimeout,
]
);
$strategy = WorkboxCacheStrategy::create(
$this->workbox->enabled,
true,
$resourceCache->strategy,
$this->prepareMatchCallback($resourceCache->matchCallback)
)
->withName($cacheName)
->withPlugin(...$plugins)
->withOptions([
'networkTimeoutSeconds' => $resourceCache->networkTimeout,
]);
if (count($urls) > 0) {
$strategy = $strategy->withPreloadUrl(...$urls);
}

$strategies[] = $strategy;
}

return $strategies;
Expand Down
Loading

0 comments on commit 21503ac

Please sign in to comment.