From 1ad6d2cb557f9756e7888d326db7b83b36a8496a Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Sun, 21 Jan 2024 11:26:08 +0100 Subject: [PATCH] Command examples --- src/Command/CreateIconsCommand.php | 31 +++++++++++++++++++++---- src/Command/CreateScreenshotCommand.php | 8 ++++++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/Command/CreateIconsCommand.php b/src/Command/CreateIconsCommand.php index adf788e..2cb102f 100644 --- a/src/Command/CreateIconsCommand.php +++ b/src/Command/CreateIconsCommand.php @@ -5,6 +5,7 @@ namespace SpomkyLabs\PwaBundle\Command; use SpomkyLabs\PwaBundle\ImageProcessor\ImageProcessor; +use Symfony\Component\AssetMapper\AssetMapperInterface; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -16,11 +17,13 @@ use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Mime\MimeTypes; use function count; +use function is_string; #[AsCommand(name: 'pwa:create:icons', description: 'Generate icons for your PWA')] final class CreateIconsCommand extends Command { public function __construct( + private readonly AssetMapperInterface $assetMapper, private readonly Filesystem $filesystem, private readonly ImageProcessor $imageProcessor, #[Autowire('%kernel.project_dir%')] @@ -31,7 +34,13 @@ public function __construct( protected function configure(): void { - $this->addArgument('source', InputArgument::REQUIRED, 'The source image'); + $this->addArgument( + 'source', + InputArgument::REQUIRED, + 'The source image or an asset logical path', + null, + ['images/logo.svg', '/home/foo/projetA/favicon.png'] + ); $this->addArgument( 'output', InputArgument::OPTIONAL, @@ -59,7 +68,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $format = $input->getOption('format'); $sizes = $input->getArgument('sizes'); - if (! $this->filesystem->exists($source)) { + $sourcePath = $this->getSourcePath($source); + if (! is_string($sourcePath)) { $io->info('The source image does not exist.'); return self::FAILURE; } @@ -71,7 +81,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $mime = MimeTypes::getDefault(); if ($format === null) { - $mimeType = $mime->guessMimeType($source); + dump($sourcePath); + $mimeType = $mime->guessMimeType($sourcePath); $extensions = $mime->getExtensions($mimeType); if (count($extensions) === 0) { $io->error(sprintf('Unable to guess the extension for the mime type "%s".', $mimeType)); @@ -82,7 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int foreach ($sizes as $size) { $io->info('Generating icon ' . $size . 'x' . $size . '...'); - $tmp = $this->imageProcessor->process(file_get_contents($source), (int) $size, (int) $size, $format); + $tmp = $this->imageProcessor->process(file_get_contents($sourcePath), (int) $size, (int) $size, $format); $this->filesystem->dumpFile(sprintf('%s/%s-%sx%s.%s', $dest, $filename, $size, $size, $format), $tmp); $io->info('Icon ' . $size . 'x' . $size . ' generated.'); } @@ -90,4 +101,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int return self::SUCCESS; } + + private function getSourcePath(string $source): int|string + { + $asset = $this->assetMapper->getAsset($source); + if ($asset !== null) { + return $asset->sourcePath; + } + if (! $this->filesystem->exists($source)) { + return self::FAILURE; + } + return $source; + } } diff --git a/src/Command/CreateScreenshotCommand.php b/src/Command/CreateScreenshotCommand.php index 66a33a2..4f27ad3 100644 --- a/src/Command/CreateScreenshotCommand.php +++ b/src/Command/CreateScreenshotCommand.php @@ -42,7 +42,13 @@ public function __construct( protected function configure(): void { - $this->addArgument('url', InputArgument::REQUIRED, 'The URL to take a screenshot from'); + $this->addArgument( + 'url', + InputArgument::REQUIRED, + 'The URL to take a screenshot from', + null, + ['https://example.com', 'https://example.com/feature1'] + ); $this->addArgument( 'output', InputArgument::OPTIONAL,