Skip to content

Commit

Permalink
Added logging capabilities for debugging (#197)
Browse files Browse the repository at this point in the history
Implemented the CanLogInterface to enable logging capabilities. This provides more insight into events and actions for easier debugging. Updated various classes, including ManifestCache, ResourceCaches, GoogleFontCache, and more.
  • Loading branch information
Spomky authored May 25, 2024
1 parent ac880cb commit b192221
Show file tree
Hide file tree
Showing 38 changed files with 679 additions and 87 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"require": {
"php": ">=8.2",
"phpdocumentor/reflection-docblock": "^5.3",
"psr/log": "^1.0|^2.0|^3.0",
"symfony/asset-mapper": "^6.4|^7.0",
"symfony/config": "^6.4|^7.0",
"symfony/dependency-injection": "^6.4|^7.0",
Expand Down Expand Up @@ -67,6 +68,7 @@
"symfony/filesystem": "^6.4|^7.0",
"symfony/framework-bundle": "^6.4|^7.0",
"symfony/mime": "^6.4|^7.0",
"symfony/monolog-bundle": "^3.10",
"symfony/panther": "^2.1",
"symfony/phpunit-bridge": "^6.4|^7.0",
"symfony/translation": "^7.0",
Expand Down
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,11 @@ parameters:
count: 1
path: src/Resources/config/definition/image_processor.php

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

-
message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeDefinition\\:\\:children\\(\\)\\.$#"
count: 1
Expand Down
32 changes: 26 additions & 6 deletions src/CachingStrategy/AssetCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace SpomkyLabs\PwaBundle\CachingStrategy;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
use SpomkyLabs\PwaBundle\Dto\Workbox;
use SpomkyLabs\PwaBundle\Service\CanLogInterface;
use SpomkyLabs\PwaBundle\WorkboxPlugin\ExpirationPlugin;
use Symfony\Component\AssetMapper\AssetMapperInterface;
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolverInterface;
Expand All @@ -18,20 +21,22 @@
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;

final readonly class AssetCache implements HasCacheStrategiesInterface
final class AssetCache implements HasCacheStrategiesInterface, CanLogInterface
{
private int $jsonOptions;
private readonly int $jsonOptions;

private string $assetPublicPrefix;
private readonly string $assetPublicPrefix;

private Workbox $workbox;
private readonly Workbox $workbox;

private LoggerInterface $logger;

public function __construct(
ServiceWorker $serviceWorker,
#[Autowire(service: 'asset_mapper.public_assets_path_resolver')]
PublicAssetsPathResolverInterface $publicAssetsPathResolver,
private AssetMapperInterface $assetMapper,
private SerializerInterface $serializer,
private readonly AssetMapperInterface $assetMapper,
private readonly SerializerInterface $serializer,
#[Autowire('%kernel.debug%')]
bool $debug,
) {
Expand All @@ -42,10 +47,12 @@ public function __construct(
$options |= JSON_PRETTY_PRINT;
}
$this->jsonOptions = $options;
$this->logger = new NullLogger();
}

public function getCacheStrategies(): array
{
$this->logger->debug('Getting cache strategies for assets');
$urls = json_decode($this->serializer->serialize($this->getAssets(), 'json', [
JsonEncode::OPTIONS => $this->jsonOptions,
]), true);
Expand All @@ -67,9 +74,18 @@ public function getCacheStrategies(): array
if (count($urls) > 0) {
$strategy = $strategy->withPreloadUrl(...$urls);
}
$this->logger->debug('Cache strategy for assets', [
'strategies' => [$strategy],
]);

return [$strategy];
}

public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}

/**
* @return array<string>
*/
Expand All @@ -81,6 +97,10 @@ private function getAssets(): array
$assets[] = $asset->publicPath;
}
}
$this->logger->debug('Preloading assets', [
'assets' => $assets,
]);

return $assets;
}
}
21 changes: 18 additions & 3 deletions src/CachingStrategy/BackgroundSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,39 @@

namespace SpomkyLabs\PwaBundle\CachingStrategy;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
use SpomkyLabs\PwaBundle\Dto\Workbox;
use SpomkyLabs\PwaBundle\MatchCallbackHandler\MatchCallbackHandlerInterface;
use SpomkyLabs\PwaBundle\Service\CanLogInterface;
use SpomkyLabs\PwaBundle\WorkboxPlugin\BackgroundSyncPlugin;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;

final readonly class BackgroundSync implements HasCacheStrategiesInterface
final class BackgroundSync implements HasCacheStrategiesInterface, CanLogInterface
{
private Workbox $workbox;
private readonly Workbox $workbox;

private LoggerInterface $logger;

/**
* @param iterable<MatchCallbackHandlerInterface> $matchCallbackHandlers
*/
public function __construct(
ServiceWorker $serviceWorker,
#[TaggedIterator('spomky_labs_pwa.match_callback_handler')]
private iterable $matchCallbackHandlers,
private readonly iterable $matchCallbackHandlers,
) {
$this->workbox = $serviceWorker->workbox;
$this->logger = new NullLogger();
}

/**
* @return array<CacheStrategyInterface>
*/
public function getCacheStrategies(): array
{
$this->logger->debug('Getting cache strategies for background sync');
$strategies = [];
foreach ($this->workbox->backgroundSync as $sync) {
$strategies[] = WorkboxCacheStrategy::create(
Expand All @@ -49,10 +56,18 @@ public function getCacheStrategies(): array
)
->withMethod($sync->method);
}
$this->logger->debug('Background sync strategies', [
'strategies' => $strategies,
]);

return $strategies;
}

public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}

private function prepareMatchCallback(string $matchCallback): string
{
foreach ($this->matchCallbackHandlers as $handler) {
Expand Down
29 changes: 24 additions & 5 deletions src/CachingStrategy/FontCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace SpomkyLabs\PwaBundle\CachingStrategy;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
use SpomkyLabs\PwaBundle\Dto\Workbox;
use SpomkyLabs\PwaBundle\Service\CanLogInterface;
use SpomkyLabs\PwaBundle\WorkboxPlugin\CacheableResponsePlugin;
use SpomkyLabs\PwaBundle\WorkboxPlugin\ExpirationPlugin;
use Symfony\Component\AssetMapper\AssetMapperInterface;
Expand All @@ -18,16 +21,18 @@
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;

final readonly class FontCache implements HasCacheStrategiesInterface
final class FontCache implements HasCacheStrategiesInterface, CanLogInterface
{
private int $jsonOptions;
private readonly int $jsonOptions;

private Workbox $workbox;
private readonly Workbox $workbox;

private LoggerInterface $logger;

public function __construct(
ServiceWorker $serviceWorker,
private AssetMapperInterface $assetMapper,
private SerializerInterface $serializer,
private readonly AssetMapperInterface $assetMapper,
private readonly SerializerInterface $serializer,
#[Autowire('%kernel.debug%')]
bool $debug,
) {
Expand All @@ -37,10 +42,12 @@ public function __construct(
$options |= JSON_PRETTY_PRINT;
}
$this->jsonOptions = $options;
$this->logger = new NullLogger();
}

public function getCacheStrategies(): array
{
$this->logger->debug('Getting cache strategies for fonts');
$urls = json_decode($this->serializer->serialize($this->getFonts(), 'json', [
JsonEncode::OPTIONS => $this->jsonOptions,
]), true);
Expand All @@ -64,10 +71,18 @@ public function getCacheStrategies(): array
if (count($urls) > 0) {
$strategy = $strategy->withPreloadUrl(...$urls);
}
$this->logger->debug('Font cache strategy', [
'strategy' => $strategy,
]);

return [$strategy];
}

public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}

/**
* @return array<string>
*/
Expand All @@ -79,6 +94,10 @@ private function getFonts(): array
$fonts[] = $asset->publicPath;
}
}
$this->logger->debug('Preloading fonts', [
'fonts' => $fonts,
]);

return $fonts;
}
}
23 changes: 20 additions & 3 deletions src/CachingStrategy/GoogleFontCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,36 @@

namespace SpomkyLabs\PwaBundle\CachingStrategy;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
use SpomkyLabs\PwaBundle\Dto\Workbox;
use SpomkyLabs\PwaBundle\Service\CanLogInterface;
use SpomkyLabs\PwaBundle\WorkboxPlugin\CacheableResponsePlugin;
use SpomkyLabs\PwaBundle\WorkboxPlugin\ExpirationPlugin;

final readonly class GoogleFontCache implements HasCacheStrategiesInterface
final class GoogleFontCache implements HasCacheStrategiesInterface, CanLogInterface
{
private Workbox $workbox;
private readonly Workbox $workbox;

private LoggerInterface $logger;

public function __construct(
ServiceWorker $serviceWorker,
) {
$this->workbox = $serviceWorker->workbox;
$this->logger = new NullLogger();
}

public function getCacheStrategies(): array
{
$this->logger->debug('Getting cache strategies for Google Fonts');
$prefix = $this->workbox->googleFontCache->cachePrefix ?? '';
if ($prefix !== '') {
$prefix .= '-';
}

return [
$strategies = [
WorkboxCacheStrategy::create(
$this->workbox->enabled && $this->workbox->googleFontCache->enabled,
true,
Expand All @@ -49,5 +56,15 @@ public function getCacheStrategies(): array
),
),
];
$this->logger->debug('Google Fonts cache strategies', [
'strategies' => $strategies,
]);

return $strategies;
}

public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
}
24 changes: 20 additions & 4 deletions src/CachingStrategy/ImageCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@

namespace SpomkyLabs\PwaBundle\CachingStrategy;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
use SpomkyLabs\PwaBundle\Dto\Workbox;
use SpomkyLabs\PwaBundle\Service\CanLogInterface;
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolverInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

final readonly class ImageCache implements HasCacheStrategiesInterface
final class ImageCache implements HasCacheStrategiesInterface, CanLogInterface
{
private string $assetPublicPrefix;
private readonly string $assetPublicPrefix;

private Workbox $workbox;
private readonly Workbox $workbox;

private LoggerInterface $logger;

public function __construct(
ServiceWorker $serviceWorker,
Expand All @@ -22,11 +27,12 @@ public function __construct(
) {
$this->workbox = $serviceWorker->workbox;
$this->assetPublicPrefix = rtrim($publicAssetsPathResolver->resolvePublicPath(''), '/');
$this->logger = new NullLogger();
}

public function getCacheStrategies(): array
{
return [
$strategies = [
WorkboxCacheStrategy::create(
$this->workbox->enabled && $this->workbox->imageCache->enabled,
true,
Expand All @@ -38,5 +44,15 @@ public function getCacheStrategies(): array
)
->withName($this->workbox->imageCache->cacheName ?? 'images'),
];
$this->logger->debug('Image cache strategies', [
'strategies' => $strategies,
]);

return $strategies;
}

public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
}
Loading

0 comments on commit b192221

Please sign in to comment.