Skip to content

Commit

Permalink
Command examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Jan 21, 2024
1 parent 13f5c51 commit 1ad6d2c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
31 changes: 27 additions & 4 deletions src/Command/CreateIconsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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%')]
Expand All @@ -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,
Expand Down Expand Up @@ -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);

Check failure on line 71 in src/Command/CreateIconsCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Parameter #1 $source of method SpomkyLabs\PwaBundle\Command\CreateIconsCommand::getSourcePath() expects string, mixed given.
if (! is_string($sourcePath)) {
$io->info('The source image does not exist.');
return self::FAILURE;
}
Expand All @@ -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);

Check failure on line 86 in src/Command/CreateIconsCommand.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));
Expand All @@ -82,12 +93,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int

foreach ($sizes as $size) {

Check failure on line 94 in src/Command/CreateIconsCommand.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);
$tmp = $this->imageProcessor->process(file_get_contents($sourcePath), (int) $size, (int) $size, $format);

Check failure on line 96 in src/Command/CreateIconsCommand.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 96 in src/Command/CreateIconsCommand.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 96 in src/Command/CreateIconsCommand.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.

Check failure on line 96 in src/Command/CreateIconsCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Parameter #4 $format of method SpomkyLabs\PwaBundle\ImageProcessor\ImageProcessor::process() expects string|null, mixed given.
$this->filesystem->dumpFile(sprintf('%s/%s-%sx%s.%s', $dest, $filename, $size, $size, $format), $tmp);

Check failure on line 97 in src/Command/CreateIconsCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.
$io->info('Icon ' . $size . 'x' . $size . ' generated.');
}
$io->info('Done.');

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;
}
}
8 changes: 7 additions & 1 deletion src/Command/CreateScreenshotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1ad6d2c

Please sign in to comment.