Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Dec 17, 2023
1 parent 5daa066 commit 370e6ce
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/vendor/
/composer.lock
/.phpunit.result.cache
/.phpunit.cache
165 changes: 101 additions & 64 deletions src/Command/GenerateManifestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SpomkyLabs\PwaBundle\ImageProcessor\ImageProcessor;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -18,6 +19,8 @@
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Mime\MimeTypes;
use function count;
use function is_int;
use const JSON_PRETTY_PRINT;
use const JSON_THROW_ON_ERROR;
use const JSON_UNESCAPED_SLASHES;
Expand Down Expand Up @@ -62,7 +65,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$assetFolder = '/' . trim((string) $input->getArgument('asset_folder'), '/');

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

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Cannot cast mixed to string.
$outputFile = '/' . trim((string) $input->getArgument('output'), '/');

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

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Cannot cast mixed to string.

$this->filesystem->mkdir($publicFolder);
if (! $this->filesystem->exists($publicFolder)) {
$this->filesystem->mkdir($publicFolder);
}

$manifest = $this->processIcons($io, $manifest, $publicUrl, $publicFolder, $assetFolder);
if ($manifest === self::FAILURE) {
Expand All @@ -86,7 +91,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
)
);
} catch (JsonException $exception) {
echo 'An error occurred while creating your directory at ' . $exception->getPath();
$io->error('An error occurred while creating your directory at ' . $exception->getPath());

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

View workflow job for this annotation

GitHub Actions / PHP 8.2 Test on ubuntu-latest

Call to an undefined method JsonException::getPath().
return self::FAILURE;
}

return self::SUCCESS;
Expand Down Expand Up @@ -161,6 +167,22 @@ private function storeScreenshot(
];
}

private function handleSizeAndPurpose(?string $purpose, int $size, array $fileData): array
{
$sizes = $size === 0 ? 'any' : $size . 'x' . $size;
$fileData += [
'sizes' => $sizes,
];

if ($purpose !== null) {
$fileData += [
'purpose' => $purpose,
];
}

return $fileData;
}

/**
* @return array{src: string, sizes: string, type: string, purpose: ?string}
*/
Expand All @@ -169,7 +191,7 @@ private function storeShortcutIcon(
string $publicUrl,
string $publicFolder,
string $assetFolder,
string $sizes,
int $size,
?string $purpose
): array {
$fileData = $this->storeFile(
Expand All @@ -178,17 +200,10 @@ private function storeShortcutIcon(
$publicFolder,
$assetFolder,
'shortcut-icon',
['shortcut-icon', $purpose, $sizes]
['shortcut-icon', $purpose, $size === 0 ? 'any' : $size . 'x' . $size]
);

return ($purpose !== null)
? $fileData + [
'sizes' => $sizes,
'purpose' => $purpose,
]
: $fileData + [
'sizes' => $sizes,
];
return $this->handleSizeAndPurpose($purpose, $size, $fileData);
}

/**
Expand All @@ -199,19 +214,19 @@ private function storeIcon(
string $publicUrl,
string $publicFolder,
string $assetFolder,
string $sizes,
int $size,
?string $purpose
): array {
$fileData = $this->storeFile($data, $publicUrl, $publicFolder, $assetFolder, 'icon', ['icon', $purpose, $sizes]);
$fileData = $this->storeFile(
$data,
$publicUrl,
$publicFolder,
$assetFolder,
'icon',
['icon', $purpose, $size === 0 ? 'any' : $size . 'x' . $size]
);

return ($purpose !== null)
? $fileData + [
'sizes' => $sizes,
'purpose' => $purpose,
]
: $fileData + [
'sizes' => $sizes,
];
return $this->handleSizeAndPurpose($purpose, $size, $fileData);
}

private function processIcons(
Expand All @@ -224,45 +239,40 @@ private function processIcons(
if ($this->config['icons'] === []) {
return $manifest;
}

try {
$this->filesystem->mkdir(sprintf('%s%s', $publicFolder, $assetFolder));
} catch (IOExceptionInterface $exception) {
echo 'An error occurred while creating your directory at ' . $exception->getPath();
if (! $this->createDirectoryIfNotExists($publicFolder, $assetFolder) || ! $this->checkImageProcessor($io)) {
return self::FAILURE;
}
$manifest['icons'] = [];
$progressBar = $io->createProgressBar(count($this->config['icons']));
$progressBar->start();
$io->info('Processing icons');
if ($this->imageProcessor === null) {
$io->error('Image processor not found');
return self::FAILURE;
}
$progressBar->start();
foreach ($this->config['icons'] as $icon) {
$this->processProgressBar($progressBar, 'icon', $icon['src']);
foreach ($icon['sizes'] as $size) {
if (!is_int($size) || $size < 0) {
if (! is_int($size) || $size < 0) {
$io->error('The icon size must be a positive integer');
return self::FAILURE;
}
$data = file_get_contents($icon['src']);
if ($data === false) {
$data = $this->loadFile($icon['src'], $size, $icon['format'] ?? null);
if ($data === null) {
$io->error(sprintf('Unable to read the icon "%s"', $icon['src']));
return self::FAILURE;
}
if ($size !== 0) {
$data = $this->imageProcessor->process($data, $size, $size, $icon['format'] ?? null);
}
$sizes = $size === 0 ? 'any' : $size . 'x' . $size;

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

return $manifest;
}
Expand All @@ -277,21 +287,18 @@ private function processScreenshots(
if ($this->config['screenshots'] === []) {
return $manifest;
}
try {
$this->filesystem->mkdir(sprintf('%s%s', $publicFolder, $assetFolder));
} catch (IOExceptionInterface $exception) {
echo 'An error occurred while creating your directory at ' . $exception->getPath();
if (! $this->createDirectoryIfNotExists($publicFolder, $assetFolder) || ! $this->checkImageProcessor($io)) {
return self::FAILURE;
}
$manifest['screenshots'] = [];
$progressBar = $io->createProgressBar(count($this->config['screenshots']));
$progressBar->start();
$io->info('Processing screenshots');
if ($this->imageProcessor === null) {
$io->error('Image processor not found');
return self::FAILURE;
}
foreach ($this->config['screenshots'] as $screenshot) {
$data = file_get_contents($screenshot['src']);
if ($data === false) {
$io->error(sprintf('Unable to read the screenshot "%s"', $screenshot['src']));
$this->processProgressBar($progressBar, 'screenshot', $screenshot['src']);
$data = $this->loadFile($screenshot['src'], null, $screenshot['format'] ?? null);
if ($data === null) {
$io->error(sprintf('Unable to read the icon "%s"', $screenshot['src']));
return self::FAILURE;
}
$screenshotManifest = $this->storeScreenshot(
Expand All @@ -310,6 +317,7 @@ private function processScreenshots(
}
$manifest['screenshots'][] = $screenshotManifest;
}
$progressBar->finish();

return $manifest;
}
Expand All @@ -324,14 +332,15 @@ private function processShortcutIcons(
if ($this->config['shortcuts'] === []) {
return $manifest;
}
try {
$this->filesystem->mkdir(sprintf('%s%s', $publicFolder, $assetFolder));
} catch (IOExceptionInterface $exception) {
echo 'An error occurred while creating your directory at ' . $exception->getPath();
if (! $this->createDirectoryIfNotExists($publicFolder, $assetFolder) || ! $this->checkImageProcessor($io)) {
return self::FAILURE;
}
$manifest['shortcuts'] = [];
$io->info('Processing schortcuts');
$progressBar = $io->createProgressBar(count($this->config['shortcuts']));
$io->info('Processing shortcuts');
$progressBar->start();
foreach ($this->config['shortcuts'] as $shortcutConfig) {
$this->processProgressBar($progressBar, 'shortcuts', $shortcutConfig['name']);
$shortcut = $shortcutConfig;
if (isset($shortcut['icons'])) {
unset($shortcut['icons']);
Expand All @@ -342,27 +351,23 @@ private function processShortcutIcons(
}
foreach ($shortcutConfig['icons'] as $icon) {
foreach ($icon['sizes'] as $size) {
if (!is_int($size) || $size < 0) {
if (! is_int($size) || $size < 0) {
$io->error('The icon size must be a positive integer');
return self::FAILURE;
}

$data = file_get_contents($icon['src']);
if ($data === false) {
$io->error(sprintf('Unable to read the screenshot "%s"', $icon['src']));
$data = $this->loadFile($icon['src'], $size, $icon['format'] ?? null);
if ($data === null) {
$io->error(sprintf('Unable to read the icon "%s"', $icon['src']));
return self::FAILURE;
}
if ($size !== 0) {
$data = $this->imageProcessor->process($data, $size, $size, $icon['format'] ?? null);
}
$sizes = $size === 0 ? 'any' : $size . 'x' . $size;

$iconManifest = $this->storeShortcutIcon(
$data,
$publicUrl,
$publicFolder,
$assetFolder,
$sizes,
$size,
$icon['purpose'] ?? null
);
$shortcut['icons'][] = $iconManifest;
Expand All @@ -371,11 +376,25 @@ private function processShortcutIcons(
}
$manifest['shortcuts'][] = $shortcut;
}
$progressBar->finish();
$manifest['shortcuts'] = array_values($manifest['shortcuts']);

return $manifest;
}

private function loadFile(string $src, ?int $size, ?string $format): ?string
{
$data = file_get_contents($src);
if ($data === false) {
return null;
}
if ($size !== 0 && $size !== null) {
$data = $this->imageProcessor->process($data, $size, $size, $format);
}

return $data;
}

private function checkImageProcessor(SymfonyStyle $io): bool
{
if ($this->imageProcessor === null) {
Expand All @@ -385,4 +404,22 @@ private function checkImageProcessor(SymfonyStyle $io): bool

return true;
}

private function createDirectoryIfNotExists(string $publicFolder, string $assetFolder): bool
{
try {
$this->filesystem->mkdir(sprintf('%s%s', $publicFolder, $assetFolder));
} catch (IOExceptionInterface $exception) {
echo 'An error occurred while creating your directory at ' . $exception->getPath();
return false;
}

return true;
}

private function processProgressBar(ProgressBar $progressBar, string $type, string $src): void
{
$progressBar->advance();
$progressBar->setMessage(sprintf('Processing %s %s', $type, $src));
}
}

0 comments on commit 370e6ce

Please sign in to comment.