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

Allow screenshots to be loaded from a folder #15

Merged
merged 1 commit into from
Dec 22, 2023
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"symfony/config": "^6.4|^7.0",
"symfony/dependency-injection": "^6.4|^7.0",
"symfony/filesystem": "^6.4|^7.0",
"symfony/finder": "^6.4",
"symfony/http-kernel": "^6.4|^7.0",
"symfony/mime": "^6.4|^7.0",
"symfony/routing": "^6.4|^7.0"
Expand Down
36 changes: 36 additions & 0 deletions src/Command/GenerateManifestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Mime\MimeTypes;
use Symfony\Component\Routing\RouterInterface;
use function count;
Expand All @@ -32,7 +33,7 @@
{
private readonly MimeTypes $mime;

public function __construct(

Check failure on line 36 in src/Command/GenerateManifestCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Method SpomkyLabs\PwaBundle\Command\GenerateManifestCommand::__construct() has parameter $config with no value type specified in iterable type array.

Check failure on line 36 in src/Command/GenerateManifestCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Method SpomkyLabs\PwaBundle\Command\GenerateManifestCommand::__construct() has parameter $dest with no value type specified in iterable type array.
private readonly null|ImageProcessor $imageProcessor,
#[Autowire('%spomky_labs_pwa.config%')]
private readonly array $config,
Expand Down Expand Up @@ -99,9 +100,9 @@
$hash = mb_substr(hash('sha256', $data), 0, 8);
file_put_contents($tempFilename, $data);
$mime = $this->mime->guessMimeType($tempFilename);
$extension = $this->mime->getExtensions($mime);

Check failure on line 103 in src/Command/GenerateManifestCommand.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 (empty($extension)) {

Check failure on line 105 in src/Command/GenerateManifestCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Construct empty() is not allowed. Use more strict comparison.
throw new RuntimeException(sprintf('Unable to guess the extension for the mime type "%s"', $mime));
}

Expand All @@ -112,7 +113,7 @@
file_put_contents($localFilename, $data);
$this->filesystem->remove($tempFilename);

return [

Check failure on line 116 in src/Command/GenerateManifestCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Method SpomkyLabs\PwaBundle\Command\GenerateManifestCommand::storeFile() should return array{src: string, type: string} but returns array{src: non-falsy-string, type: string|null}.
'src' => sprintf('%s/%s', $prefixUrl, $filename),
'type' => $mime,
];
Expand All @@ -124,10 +125,10 @@
private function storeScreenshot(string $data, ?string $format, ?string $formFactor): array
{
if ($format !== null) {
$data = $this->imageProcessor->process($data, null, null, $format);

Check failure on line 128 in src/Command/GenerateManifestCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Cannot call method process() on SpomkyLabs\PwaBundle\ImageProcessor\ImageProcessor|null.
}

['width' => $width, 'height' => $height] = $this->imageProcessor->getSizes($data);

Check failure on line 131 in src/Command/GenerateManifestCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Cannot call method getSizes() on SpomkyLabs\PwaBundle\ImageProcessor\ImageProcessor|null.
$size = sprintf('%sx%s', $width, $height);

$fileData = $this->storeFile(
Expand All @@ -142,12 +143,12 @@
];
}

return $fileData + [

Check failure on line 146 in src/Command/GenerateManifestCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Method SpomkyLabs\PwaBundle\Command\GenerateManifestCommand::storeScreenshot() should return array{src: string, type: string, sizes: string, form_factor: string|null} but returns array{sizes: non-falsy-string, form_factor?: string, src: string, type: string}.
'sizes' => $size,
];
}

private function handleSizeAndPurpose(?string $purpose, int $size, array $fileData): array

Check failure on line 151 in src/Command/GenerateManifestCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Method SpomkyLabs\PwaBundle\Command\GenerateManifestCommand::handleSizeAndPurpose() has parameter $fileData with no value type specified in iterable type array.

Check failure on line 151 in src/Command/GenerateManifestCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Method SpomkyLabs\PwaBundle\Command\GenerateManifestCommand::handleSizeAndPurpose() return type has no value type specified in iterable type array.
{
$sizes = $size === 0 ? 'any' : $size . 'x' . $size;
$fileData += [
Expand Down Expand Up @@ -243,7 +244,20 @@
$progressBar = $io->createProgressBar(count($this->config['screenshots']));
$progressBar->start();
$io->info('Processing screenshots');
$config = [];
foreach ($this->config['screenshots'] as $screenshot) {
$src = $screenshot['src'];
if (! $this->filesystem->exists($src)) {
continue;
}
foreach ($this->findImages($src) as $image) {
$data = $screenshot;
$data['src'] = $image;
$config[] = $data;
}
}

foreach ($config as $screenshot) {
$this->processProgressBar($progressBar, 'screenshot', $screenshot['src']);
$data = $this->loadFileAndConvert($screenshot['src'], null, $screenshot['format'] ?? null);
if ($data === null) {
Expand Down Expand Up @@ -391,4 +405,26 @@

return $manifest;
}

/**
* @return iterable<string}>
*/
private function findImages(string $src): iterable
{
$finder = new Finder();
if (is_file($src)) {
yield $src;
return;
}
$files = $finder->in($src)
->files()
->name('/\.(png|jpg|jpeg|gif|webp|svg)$/i');
foreach ($files as $file) {
if ($file->isFile()) {
yield $file->getRealPath();
} else {
yield from $this->findImages($file->getRealPath());
}
}
}
}
23 changes: 1 addition & 22 deletions tests/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,31 +106,10 @@
'theme_color' => 'red',
'screenshots' => [
[
'src' => sprintf('%s/images/360x800.svg', __DIR__),
'label' => 'Homescreen of Awesome App',
'src' => sprintf('%s/images/screenshots', __DIR__),
'platform' => 'android',
'format' => 'png',
],
[
'src' => sprintf('%s/images/390x844.svg', __DIR__),
'label' => 'List of Awesome Resources available in Awesome App',
'platform' => 'windows',
'format' => 'jpeg',
],
[
'src' => sprintf('%s/images/600x400.svg', __DIR__),
'label' => 'Awesome App in action (1)',
'format' => 'webp',
],
[
'src' => sprintf('%s/images/780x360.svg', __DIR__),
'label' => 'Awesome App in action (2)',
'format' => 'webp',
],
[
'src' => sprintf('%s/images/915x412.svg', __DIR__),
'label' => 'Awesome App in action (3)',
],
],
'share_target' => [
'action' => '/shared-content-receiver/',
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes