Skip to content

Commit

Permalink
Optional Manifest file
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Feb 3, 2024
1 parent 34bbc68 commit 0affdbb
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,11 @@ private function setupShortcuts(): ArrayNodeDefinition
->end()
->scalarNode('short_name')
->info('The short name of the shortcut.')
->example('Awesome shortcut')
->example('shortcut')
->end()
->scalarNode('description')
->info('The description of the shortcut.')
->example('Awesome shortcut')
->example('This is an awesome shortcut')
->end()
->append($this->getUrlNode('url', 'The URL of the shortcut.'))
->append($this->getIconsNode('The icons of the shortcut.'))
Expand Down Expand Up @@ -465,7 +465,7 @@ private function setupManifest(ArrayNodeDefinition $node): void
->end()
->scalarNode('short_name')
->info('The short name of the application.')
->example('My awesome application')
->example('awesome_app')
->end()
->scalarNode('scope')
->info('The scope of the application.')
Expand Down
2 changes: 2 additions & 0 deletions src/Dto/ServiceWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

final class ServiceWorker
{
public bool $enabled;

Check failure on line 11 in src/Dto/ServiceWorker.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Class SpomkyLabs\PwaBundle\Dto\ServiceWorker has an uninitialized property $enabled. Give it default value or assign it in the constructor.

public string $src;

public string $dest;
Expand Down
3 changes: 0 additions & 3 deletions src/Service/ServiceWorkerCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ public function compile(): ?string
return null;
}
$serviceWorker = $this->serviceWorker;
if ($serviceWorker === null) {
return null;
}

if (! str_starts_with($serviceWorker->src, '/')) {
$asset = $this->assetMapper->getAsset($serviceWorker->src);
Expand Down
15 changes: 6 additions & 9 deletions src/Subscriber/PwaDevServerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,9 @@ public function __construct(
private null|Profiler $profiler,
) {
$this->manifestPublicUrl = '/' . trim($manifestPublicUrl, '/');
$serviceWorkerPublicUrl = $serviceWorker?->dest;
$this->serviceWorkerPublicUrl = $serviceWorkerPublicUrl === null ? null : '/' . trim(
$serviceWorkerPublicUrl,
'/'
);
if ($serviceWorker?->workbox->enabled === true) {
$serviceWorkerPublicUrl = $serviceWorker->dest;
$this->serviceWorkerPublicUrl = '/' . trim($serviceWorkerPublicUrl, '/');
if ($serviceWorker->workbox->enabled === true) {
$this->workboxVersion = $serviceWorker->workbox->version;
$workboxPublicUrl = $serviceWorker->workbox->workboxPublicUrl;
$this->workboxPublicUrl = '/' . trim($workboxPublicUrl, '/');
Expand All @@ -78,16 +75,16 @@ public function onKernelRequest(RequestEvent $event): void
->getPathInfo();

switch (true) {
case $this->manifestEnabled === true && $pathInfo === $this->manifestPublicUrl :
case $this->manifestEnabled === true && $pathInfo === $this->manifestPublicUrl:
$this->serveManifest($event);
break;
case $this->serviceWorkerEnabled === true && $pathInfo === $this->serviceWorkerPublicUrl :
case $this->serviceWorkerEnabled === true && $pathInfo === $this->serviceWorkerPublicUrl:
$this->serveServiceWorker($event);
break;
case $this->serviceWorkerEnabled === true && $this->workboxVersion !== null && $this->workboxPublicUrl !== null && str_starts_with(
$pathInfo,
$this->workboxPublicUrl
) :
):
$this->serveWorkboxFile($event, $pathInfo);
break;
}
Expand Down
1 change: 1 addition & 0 deletions tests/Functional/AbstractPwaTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ private static function cleanupFolder(): void
{
$filesystem = self::getContainer()->get(Filesystem::class);
$filesystem->remove(sprintf('%s/samples', self::$kernel->getCacheDir()));
$filesystem->remove(sprintf('%s/output', self::$kernel->getCacheDir()));
}
}
48 changes: 48 additions & 0 deletions tests/Functional/CompileCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Tests\Functional;

use PHPUnit\Framework\Attributes\Test;
use Symfony\Component\Console\Tester\CommandTester;

/**
* @internal
*/
final class CompileCommandTest extends AbstractPwaTestCase
{
#[Test]
public static function aScreenshotIsCorrectlyTake(): void
{
// Given
$command = self::$application->find('asset-map:compile');
$commandTester = new CommandTester($command);

// When
$commandTester->execute([]);

// Then
$commandTester->assertCommandIsSuccessful();
static::assertFileExists(self::$kernel->getCacheDir() . '/output/site.webmanifest');
static::assertFileExists(self::$kernel->getCacheDir() . '/output/sw.js');
static::assertFileExists(
self::$kernel->getCacheDir() . '/output/assets/pwa/screenshots/360x800-86e5e530cd7674b4f1137a418b6d0264.svg'
);
static::assertFileExists(
self::$kernel->getCacheDir() . '/output/assets/pwa/screenshots/390x844-3f5c4bdccd303b49c95aa4344651c7e2.svg'
);
static::assertFileExists(
self::$kernel->getCacheDir() . '/output/assets/pwa/screenshots/600x400-a6d84c84616946feb5f92f8ca0ae4047.svg'
);
static::assertFileExists(
self::$kernel->getCacheDir() . '/output/assets/pwa/screenshots/780x360-5bf5dc07ede9d26a9b2e9dc9f53d1959.svg'
);
static::assertFileExists(
self::$kernel->getCacheDir() . '/output/assets/pwa/screenshots/915x412-6141e808964c20e880f141190100d6e6.svg'
);
static::assertFileExists(
self::$kernel->getCacheDir() . '/output/assets/pwa/1920x1920-862cb89ba358ac021badfbe32a89bbfb.svg'
);
}
}
1 change: 0 additions & 1 deletion tests/Functional/DevServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,5 @@ public static function theServiceWorkerIsServed(): void
// Then
static::assertResponseIsSuccessful();
static::assertResponseHeaderSame('Content-Type', 'application/javascript');
dump($client->getResponse()->getContent());
}
}
44 changes: 44 additions & 0 deletions tests/TestFilesystem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Tests;

use Symfony\Component\AssetMapper\Path\PublicAssetsFilesystemInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use function dirname;

final class TestFilesystem implements PublicAssetsFilesystemInterface
{
private readonly string $output;

public function __construct(
#[Autowire('%kernel.cache_dir%')]
string $cacheDir,
) {
$this->output = sprintf('%s/output', $cacheDir);
}

public function write(string $path, string $contents): void
{
$dest = sprintf('%s/%s', $this->output, $path);
if (! is_dir(dirname($dest))) {
mkdir(dirname($dest), 0755, true);
}
file_put_contents($dest, $contents);
}

public function copy(string $originPath, string $path): void
{
$dest = sprintf('%s/%s', $this->output, $path);
if (! is_dir(dirname($dest))) {
mkdir(dirname($dest), 0755, true);
}
copy($originPath, $dest);
}

public function getDestinationPath(): string
{
return $this->output;
}
}
5 changes: 5 additions & 0 deletions tests/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use SpomkyLabs\PwaBundle\Tests\DummyImageProcessor;
use SpomkyLabs\PwaBundle\Tests\TestFilesystem;

return static function (ContainerConfigurator $container) {
$container->services()
->set(DummyImageProcessor::class);
$container->services()
->set('asset_mapper.local_public_assets_filesystem', TestFilesystem::class)
->autoconfigure()
->autowire();

$container->services()
->load('SpomkyLabs\\PwaBundle\\Tests\\Controller\\', __DIR__ . '/Controller/')
Expand Down

0 comments on commit 0affdbb

Please sign in to comment.