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

Adds Windows10/11 Widgets, edge_side_panel, iarc_rating_id, scope_ext… #18

Merged
merged 1 commit into from
Jan 10, 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
128 changes: 69 additions & 59 deletions src/Command/GenerateManifestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;

#[AsCommand(name: 'pwa:build', description: 'Generate the Progressive Web App Manifest',)]
#[AsCommand(name: 'pwa:build', description: 'Generate the Progressive Web App Manifest')]
final class GenerateManifestCommand extends Command
{
private readonly MimeTypes $mime;

private readonly null|Client $webClient;

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

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Property SpomkyLabs\PwaBundle\Command\GenerateManifestCommand::$webClient has unknown class Symfony\Component\Panther\Client as its type.

public function __construct(

Check failure on line 40 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 40 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('@pwa.web_client')]

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

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Parameter $webClient of method SpomkyLabs\PwaBundle\Command\GenerateManifestCommand::__construct() has invalid type Symfony\Component\Panther\Client.
null|Client $webClient,
#[Autowire('%spomky_labs_pwa.config%')]
private readonly array $config,
Expand Down Expand Up @@ -80,6 +80,10 @@
if (! is_array($manifest)) {
return self::FAILURE;
}
$manifest = $this->processWidgets($io, $manifest);
if (! is_array($manifest)) {
return self::FAILURE;
}
$manifest = $this->processServiceWorker($io, $manifest);
if (! is_array($manifest)) {
return self::FAILURE;
Expand Down Expand Up @@ -115,9 +119,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 122 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 124 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 @@ -128,7 +132,7 @@
file_put_contents($localFilename, $data);
$this->filesystem->remove($tempFilename);

return [

Check failure on line 135 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 @@ -140,10 +144,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 147 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 150 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 @@ -158,7 +162,7 @@
];
}

return $fileData + [

Check failure on line 165 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,
];
}
Expand All @@ -179,21 +183,6 @@
return $fileData;
}

/**
* @return array{src: string, sizes: string, type: string, purpose: ?string}
*/
private function storeShortcutIcon(string $data, int $size, ?string $purpose): array
{
$fileData = $this->storeFile(
$data,
$this->dest['shortcut_icon_prefix_url'],
$this->dest['shortcut_icon_folder'],
['shortcut-icon', $purpose, $size === 0 ? 'any' : $size . 'x' . $size]
);

return $this->handleSizeAndPurpose($purpose, $size, $fileData);
}

/**
* @return array{src: string, sizes: string, type: string, purpose: ?string}
*/
Expand All @@ -209,16 +198,13 @@
return $this->handleSizeAndPurpose($purpose, $size, $fileData);
}

private function processIcons(SymfonyStyle $io, array $manifest): array|int
/**
* @param array{src: string, sizes: array<int>, format: ?string, purpose: ?string} $icons
*/
private function processIcon(SymfonyStyle $io, array $icons): array|int
{
if ($this->config['icons'] === []) {
return $manifest;
}
if (! $this->createDirectoryIfNotExists($this->dest['icon_folder']) || ! $this->checkImageProcessor($io)) {
return self::FAILURE;
}
$manifest['icons'] = [];
foreach ($this->config['icons'] as $icon) {
$result = [];
foreach ($icons as $icon) {
foreach ($icon['sizes'] as $size) {
if (! is_int($size) || $size < 0) {
$io->error('The icon size must be a positive integer');
Expand All @@ -231,27 +217,31 @@
}

$iconManifest = $this->storeIcon($data, $size, $icon['purpose'] ?? null);
$manifest['icons'][] = $iconManifest;
$result[] = $iconManifest;
}
}
$io->info('Icons are built');

return $manifest;
return $result;
}

private function processScreenshots(SymfonyStyle $io, array $manifest): array|int
private function processIcons(SymfonyStyle $io, array $manifest): array|int
{
if ($this->config['screenshots'] === []) {
if ($this->config['icons'] === []) {
return $manifest;
}
if (! $this->createDirectoryIfNotExists($this->dest['screenshot_folder']) || ! $this->checkImageProcessor(
$io
)) {
if (! $this->createDirectoryIfNotExists($this->dest['icon_folder']) || ! $this->checkImageProcessor($io)) {
return self::FAILURE;
}
$manifest['screenshots'] = [];
$manifest['icons'] = $this->processIcon($io, $this->config['icons']);
$io->info('Icons are built');

return $manifest;
}

private function processScreenshot(SymfonyStyle $io, array $screenshots): array|int
{
$config = [];
foreach ($this->config['screenshots'] as $screenshot) {
foreach ($screenshots as $screenshot) {
if (isset($screenshot['src'])) {
$src = $screenshot['src'];
if (! $this->filesystem->exists($src)) {
Expand All @@ -264,6 +254,12 @@
}
}
if (isset($screenshot['path'])) {
if ($this->webClient === null) {
$io->error(
'The web client is not available. Unable to take a screenshot. Please install "symfony/panther" and a web driver.'
);
return self::FAILURE;
}
$path = $screenshot['path'];
$height = $screenshot['height'];
$width = $screenshot['width'];
Expand All @@ -286,6 +282,7 @@
}
}

$result = [];
foreach ($config as $screenshot) {
$data = $this->loadFileAndConvert($screenshot['src'], null, $screenshot['format'] ?? null);
if ($data === null) {
Expand All @@ -305,12 +302,27 @@
if (isset($screenshot['platform'])) {
$screenshotManifest['platform'] = $screenshot['platform'];
}
$manifest['screenshots'][] = $screenshotManifest;
$result[] = $screenshotManifest;
if ($delete) {
$this->filesystem->remove($screenshot['src']);
}
}

return $result;
}

private function processScreenshots(SymfonyStyle $io, array $manifest): array|int
{
if ($this->config['screenshots'] === []) {
return $manifest;
}
if (! $this->createDirectoryIfNotExists($this->dest['screenshot_folder']) || ! $this->checkImageProcessor(
$io
)) {
return self::FAILURE;
}
$manifest['screenshots'] = $this->processScreenshot($io, $this->config['screenshots']);

return $manifest;
}

Expand All @@ -319,9 +331,7 @@
if ($this->config['shortcuts'] === []) {
return $manifest;
}
if (! $this->createDirectoryIfNotExists($this->dest['shortcut_icon_folder']) || ! $this->checkImageProcessor(
$io
)) {
if (! $this->createDirectoryIfNotExists($this->dest['icon_folder']) || ! $this->checkImageProcessor($io)) {
return self::FAILURE;
}
$manifest['shortcuts'] = [];
Expand All @@ -339,26 +349,7 @@
}

if (isset($shortcutConfig['icons'])) {
if (! $this->checkImageProcessor($io)) {
return self::FAILURE;
}
foreach ($shortcutConfig['icons'] as $icon) {
foreach ($icon['sizes'] as $size) {
if (! is_int($size) || $size < 0) {
$io->error('The icon size must be a positive integer');
return self::FAILURE;
}

$data = $this->loadFileAndConvert($icon['src'], $size, $icon['format'] ?? null);
if ($data === null) {
$io->error(sprintf('Unable to read the icon "%s"', $icon['src']));
return self::FAILURE;
}

$iconManifest = $this->storeShortcutIcon($data, $size, $icon['purpose'] ?? null);
$shortcut['icons'][] = $iconManifest;
}
}
$shortcut['icons'] = $this->processIcon($io, $shortcutConfig['icons']);
}
$manifest['shortcuts'][] = $shortcut;
}
Expand Down Expand Up @@ -498,4 +489,23 @@

return $manifest;
}

private function processWidgets(SymfonyStyle $io, array $manifest): int|array
{
if ($this->config['widgets'] === []) {
return $manifest;
}
$manifest['widgets'] = [];
foreach ($this->config['widgets'] as $widget) {
if (isset($widget['icons'])) {
$widget['icons'] = $this->processIcon($io, $widget['icons']);
}
if (isset($widget['screenshots'])) {
$widget['screenshots'] = $this->processScreenshot($io, $widget['screenshots']);
}
$manifest['widgets'][] = $widget;
}

return $manifest;
}
}
Loading