Skip to content

Commit

Permalink
Refactor Data class and update method calls (#203)
Browse files Browse the repository at this point in the history
The Data class has been refactored to accept a Closure for the data field and expose getData and getRawData methods. Consequently, adjustments were made to the FaviconsCompiler, FileCompileEventListener, and PwaDevServerSubscriber classes to accommodate these changes.
  • Loading branch information
Spomky authored May 20, 2024
1 parent 1328149 commit e5800f4
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 20 deletions.
9 changes: 8 additions & 1 deletion src/DataCollector/PwaCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ public function collect(Request $request, Response $response, Throwable $excepti
$this->data['favicons'] = [
'enabled' => $this->favicons->enabled,
'data' => $this->favicons,
'files' => $faviconsFiles,
'files' => array_map(
static fn (\SpomkyLabs\PwaBundle\Service\Data $data): array => [
'url' => $data->url,
'headers' => $data->headers,
'html' => $data->html,
],
$faviconsFiles
),
];
}

Expand Down
34 changes: 27 additions & 7 deletions src/Service/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,47 @@

namespace SpomkyLabs\PwaBundle\Service;

use Closure;

/**
* @internal
*/
final readonly class Data
final class Data
{
/**
* @param array<string, string|bool> $headers
*/
public function __construct(
public string $url,
public string $data,
public array $headers,
public null|string $html = null,
public readonly string $url,
private string|Closure $data,
public readonly array $headers,
public readonly null|string $html = null,
) {
}

/**
* @param array<string, string|bool> $headers
*/
public static function create(string $url, string $data, array $headers = [], null|string $html = null): self
{
public static function create(
string $url,
string|Closure $data,
array $headers = [],
null|string $html = null
): self {
return new self($url, $data, $headers, $html);
}

public function getData(): string
{
if ($this->data instanceof Closure) {
$this->data = ($this->data)();
}

return $this->data;
}

public function getRawData(): string|Closure
{
return $this->data;
}
}
14 changes: 6 additions & 8 deletions src/Service/FaviconsCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ public function getFiles(): array
}

private function processIcon(
string $content,
string $asset,
string $publicUrl,
Configuration $configuration,
string $mimeType,
null|string $rel,
): Data {
$closure = fn (): string => $this->imageProcessor->process($asset, null, null, null, $configuration);
if ($this->debug === true) {
$data = $this->imageProcessor->process($content, null, null, null, $configuration);
$html = $rel === null ? null : sprintf(
'<link rel="%s" sizes="%dx%d" type="%s" href="%s">',
$rel,
Expand All @@ -267,7 +267,7 @@ private function processIcon(
);
return Data::create(
$publicUrl,
$data,
$closure,
[
'Cache-Control' => 'public, max-age=604800, immutable',
'Content-Type' => $mimeType,
Expand All @@ -277,15 +277,13 @@ private function processIcon(
);
}
assert($this->imageProcessor !== null);
$data = $this->imageProcessor->process($content, null, null, null, $configuration);
return Data::create(
$publicUrl,
$data,
$closure,
[
'Cache-Control' => 'public, max-age=604800, immutable',
'Content-Type' => $mimeType,
'X-Favicons-Dev' => true,
'Etag' => hash('xxh128', $data),
],
sprintf(
'<link rel="%s" sizes="%dx%d" type="%s" href="%s">',
Expand Down Expand Up @@ -339,7 +337,7 @@ private function processBrowserConfig(string $asset): array
'/favicons/icon-144x144.png',
Configuration::create(144, 144, 'png', null, null, $this->favicons->imageScale),
'image/png',
null
null//'<meta name="msapplication-TileImage" content="/favicons/icon-144x144.png">'
);

if ($this->favicons->tileColor === null) {
Expand Down Expand Up @@ -381,7 +379,7 @@ private function processBrowserConfig(string $asset): array
$icon310x150,
Data::create(
$icon144x144->url,
$icon144x144->data,
$icon144x144->getRawData(),
$icon144x144->headers,
sprintf('<meta name="msapplication-TileImage" content="%s">', $icon144x144->url)
),
Expand Down
2 changes: 1 addition & 1 deletion src/Subscriber/FileCompileEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __invoke(PreAssetsCompileEvent $event): void
{
foreach ($this->fileCompilers as $fileCompiler) {
foreach ($fileCompiler->getFiles() as $data) {
$this->assetsFilesystem->write($data->url, $data->data);
$this->assetsFilesystem->write($data->url, $data->getData());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Subscriber/PwaDevServerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static function getSubscribedEvents(): array
private function serveFile(RequestEvent $event, Data $data): void
{
$this->profiler?->disable();
$response = new Response($data->data, Response::HTTP_OK, $data->headers);
$response = new Response($data->getData(), Response::HTTP_OK, $data->headers);
$event->setResponse($response);
$event->stopPropagation();
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Functional/DevServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\Attributes\Test;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Request;

/**
* @internal
Expand All @@ -19,7 +20,7 @@ public static function theManifestIsServed(): void
$client = static::createClient();

// When
$client->request('GET', '/site.webmanifest');
$client->request(Request::METHOD_GET, '/site.webmanifest');

// Then
static::assertResponseIsSuccessful();
Expand All @@ -33,7 +34,7 @@ public static function theServiceWorkerIsServed(): void
$client = static::createClient();

// When
$client->request('GET', '/sw.js');
$client->request(Request::METHOD_GET, '/sw.js');

// Then
static::assertResponseIsSuccessful();
Expand Down

0 comments on commit e5800f4

Please sign in to comment.