Skip to content

Commit

Permalink
Adds Windows10/11 Widgets, edge_side_panel, iarc_rating_id, scope_ext…
Browse files Browse the repository at this point in the history
…ensions and handle_links parameters
  • Loading branch information
Spomky committed Jan 10, 2024
1 parent bf29e1b commit 5e59efe
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 119 deletions.
128 changes: 69 additions & 59 deletions src/Command/GenerateManifestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
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;
Expand Down Expand Up @@ -80,6 +80,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
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 @@ -179,21 +183,6 @@ private function handleSizeAndPurpose(?string $purpose, int $size, array $fileDa
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 @@ private function storeIcon(string $data, int $size, ?string $purpose): array
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 @@ private function processIcons(SymfonyStyle $io, array $manifest): array|int
}

$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 @@ private function processScreenshots(SymfonyStyle $io, array $manifest): array|in
}
}
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 @@ private function processScreenshots(SymfonyStyle $io, array $manifest): array|in
}
}

$result = [];
foreach ($config as $screenshot) {
$data = $this->loadFileAndConvert($screenshot['src'], null, $screenshot['format'] ?? null);
if ($data === null) {
Expand All @@ -305,12 +302,27 @@ private function processScreenshots(SymfonyStyle $io, array $manifest): array|in
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 @@ private function processShortcutIcons(SymfonyStyle $io, array|int $manifest): ar
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 @@ private function processShortcutIcons(SymfonyStyle $io, array|int $manifest): ar
}

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 @@ private function processServiceWorker(SymfonyStyle $io, array $manifest): int|ar

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

0 comments on commit 5e59efe

Please sign in to comment.