Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leverage on Asset Mapper #33

Merged
merged 3 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,22 @@
},
"require": {
"php": ">=8.2",
"phpdocumentor/reflection-docblock": "^5.3",
"symfony/asset-mapper": "^6.4|^7.0",
"symfony/config": "^6.4|^7.0",
"symfony/dependency-injection": "^6.4|^7.0",
"symfony/filesystem": "^6.4|^7.0",
"symfony/finder": "^6.4|^7.0",
"symfony/http-kernel": "^6.4|^7.0",
"symfony/mime": "^6.4|^7.0",
"symfony/routing": "^6.4|^7.0"
"symfony/property-access": "^6.4|^7.0",
"symfony/property-info": "^7.0",
"symfony/routing": "^6.4|^7.0",
"symfony/serializer": "^6.4|^7.0",
"twig/twig": "^3.8"
},
"require-dev": {
"dbrekelmans/bdi": "^1.1",
"infection/infection": "^0.27",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^1.0",
Expand All @@ -47,6 +54,7 @@
"symfony/framework-bundle": "^6.4|^7.0",
"symfony/panther": "^2.1",
"symfony/phpunit-bridge": "^6.4|^7.0",
"symfony/yaml": "^6.4|^7.0",
"symplify/easy-coding-standard": "^12.0"
},
"config": {
Expand Down
3 changes: 3 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
]);
$config->phpVersion(PhpVersion::PHP_82);
$config->paths([__DIR__ . '/src', __DIR__ . '/tests']);
$config->skip([
__DIR__ . '/tests/Controller/DummyController.php',
]);
$config->parallel();
$config->importNames();
$config->importShortClasses();
Expand Down
84 changes: 84 additions & 0 deletions src/Command/GenerateIconsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Command;

use SpomkyLabs\PwaBundle\ImageProcessor\ImageProcessor;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Mime\MimeTypes;
use function count;

#[AsCommand(name: 'pwa:generate-icons', description: 'Generate icons for your PWA')]
final class GenerateIconsCommand extends Command
{
public function __construct(
private readonly Filesystem $filesystem,
private readonly ImageProcessor $imageProcessor,
) {
parent::__construct();
}

protected function configure(): void
{
$this->addArgument('source', InputArgument::REQUIRED, 'The source image');
$this->addArgument('output', InputArgument::REQUIRED, 'The output directory');
$this->addOption('format', null, InputOption::VALUE_OPTIONAL, 'The format of the icons');
$this->addArgument(
'sizes',
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
'The sizes of the icons',
['192', '512']
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->title('PWA Icons Generator');

$source = $input->getArgument('source');
$dest = $input->getArgument('output');
$format = $input->getOption('format');
$sizes = $input->getArgument('sizes');

if (! $this->filesystem->exists($source)) {

Check failure on line 52 in src/Command/GenerateIconsCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Parameter #1 $files of method Symfony\Component\Filesystem\Filesystem::exists() expects iterable|string, mixed given.
$io->info('The source image does not exist.');
return self::FAILURE;
}

if (! $this->filesystem->exists($dest)) {

Check failure on line 57 in src/Command/GenerateIconsCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Parameter #1 $files of method Symfony\Component\Filesystem\Filesystem::exists() expects iterable|string, mixed given.
$io->info('The output directory does not exist. It will be created.');
$this->filesystem->mkdir($dest);

Check failure on line 59 in src/Command/GenerateIconsCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Parameter #1 $dirs of method Symfony\Component\Filesystem\Filesystem::mkdir() expects iterable|string, mixed given.
}

$mime = MimeTypes::getDefault();
if ($format === null) {
$mimeType = $mime->guessMimeType($source);

Check failure on line 64 in src/Command/GenerateIconsCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Parameter #1 $path of method Symfony\Component\Mime\MimeTypes::guessMimeType() expects string, mixed given.
$extensions = $mime->getExtensions($mimeType);

Check failure on line 65 in src/Command/GenerateIconsCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Parameter #1 $mimeType of method Symfony\Component\Mime\MimeTypes::getExtensions() expects string, string|null given.
if (count($extensions) === 0) {
$io->error(sprintf('Unable to guess the extension for the mime type "%s".', $mimeType));
return self::FAILURE;
}
$format = current($extensions);
}

foreach ($sizes as $size) {

Check failure on line 73 in src/Command/GenerateIconsCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Argument of an invalid type mixed supplied for foreach, only iterables are supported.
$io->info('Generating icon ' . $size . 'x' . $size . '...');
$tmp = $this->imageProcessor->process(file_get_contents($source), (int) $size, (int) $size, $format);

Check failure on line 75 in src/Command/GenerateIconsCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Cannot cast mixed to int.

Check failure on line 75 in src/Command/GenerateIconsCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Cannot cast mixed to int.

Check failure on line 75 in src/Command/GenerateIconsCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Parameter #1 $filename of function file_get_contents expects string, mixed given.

Check failure on line 75 in src/Command/GenerateIconsCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Parameter #1 $image of method SpomkyLabs\PwaBundle\ImageProcessor\ImageProcessor::process() expects string, string|false given.
$filename = sprintf('%s/icon-%sx%s.%s', $dest, $size, $size, $format);
$this->filesystem->dumpFile($filename, $tmp);
$io->info('Icon ' . $size . 'x' . $size . ' generated.');
}
$io->info('Done.');

return self::SUCCESS;
}
}
74 changes: 0 additions & 74 deletions src/Command/GenerateManifestCommand.php

This file was deleted.

31 changes: 3 additions & 28 deletions src/Command/GenerateServiceWorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Config\FileLocator;
use function count;
use function dirname;

#[AsCommand(name: 'pwa:sw', description: 'Generate a basic Service Worker')]
final class GenerateServiceWorkerCommand extends Command
{
public function __construct(
#[Autowire('%spomky_labs_pwa.config%')]
private readonly array $config,
private readonly Filesystem $filesystem,
private readonly FileLocator $fileLocator,
) {
Expand All @@ -30,6 +27,7 @@ public function __construct(

protected function configure(): void
{
$this->addArgument('output', InputArgument::REQUIRED, 'The output file');
$this->addOption('force', 'f', InputOption::VALUE_NONE, 'Force the generation of the service worker');
}

Expand All @@ -38,20 +36,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io = new SymfonyStyle($input, $output);
$io->title('PWA Service Worker Generator');

if (! isset($this->config['serviceworker'])) {
$io->info('Service worker section is missing.');
return self::SUCCESS;
}

$dest = $input->getArgument('output');
$force = $input->getOption('force');

$dest = $this->config['serviceworker']['filepath'];
$scope = $this->config['serviceworker']['scope'];
$src = $this->config['serviceworker']['src'];

if (! $this->filesystem->exists(dirname((string) $dest))) {
$this->filesystem->mkdir(dirname((string) $dest));
}
if ($this->filesystem->exists($dest) && ! $force) {
$io->info('Service worker already exists. Skipping.');
return self::SUCCESS;
Expand All @@ -65,18 +52,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$resourcePath = $resourcePath[0];
$this->filesystem->copy($resourcePath, $dest);
$io->info('Service worker generated.');
$io->comment('You can now configure your web server to serve the service worker file.');
$io->section('# assets/app.js (or any other entrypoint)');
$jsTemplate = <<<JS
if (navigator.serviceWorker) {
window.addEventListener("load", () => {
navigator.serviceWorker.register("{$src}", {scope: '{$scope}'});
})
}
JS;

$io->writeln($jsTemplate);
$io->section('# End of file');

return self::SUCCESS;
}
Expand Down
40 changes: 0 additions & 40 deletions src/Command/SectionProcessor/ActionsSectionProcessor.php

This file was deleted.

59 changes: 0 additions & 59 deletions src/Command/SectionProcessor/ApplicationIconsSectionProcessor.php

This file was deleted.

Loading
Loading