Skip to content

Commit

Permalink
Refactored code and added logging capabilities
Browse files Browse the repository at this point in the history
Re-structured several classes introducing logging capabilities to better track application operation. Separated some classes for clean code organization and enhanced debugging information.
  • Loading branch information
Spomky committed May 11, 2024
1 parent d2b3a71 commit 3b6cefb
Show file tree
Hide file tree
Showing 37 changed files with 657 additions and 100 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
15 changes: 0 additions & 15 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -590,21 +590,6 @@ parameters:
count: 2
path: src/Service/FaviconsCompiler.php

-
message: "#^Parameter \\#1 \\$asset of method SpomkyLabs\\\\PwaBundle\\\\Service\\\\FaviconsCompiler\\:\\:processBrowserConfig\\(\\) expects SpomkyLabs\\\\PwaBundle\\\\Service\\\\MappedAsset, string given\\.$#"
count: 1
path: src/Service/FaviconsCompiler.php

-
message: "#^Parameter \\#1 \\$content of method SpomkyLabs\\\\PwaBundle\\\\Service\\\\FaviconsCompiler\\:\\:processIcon\\(\\) expects string, SpomkyLabs\\\\PwaBundle\\\\Service\\\\MappedAsset given\\.$#"
count: 5
path: src/Service/FaviconsCompiler.php

-
message: "#^Parameter \\$asset of method SpomkyLabs\\\\PwaBundle\\\\Service\\\\FaviconsCompiler\\:\\:processBrowserConfig\\(\\) has invalid type SpomkyLabs\\\\PwaBundle\\\\Service\\\\MappedAsset\\.$#"
count: 1
path: src/Service/FaviconsCompiler.php

-
message: "#^Strict comparison using \\!\\=\\= between string and null will always evaluate to true\\.$#"
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 3b6cefb

Please sign in to comment.