From 2a312a7fdbcad2666206d69a124fc365a5bc5590 Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Mon, 18 Mar 2024 21:50:54 +0100 Subject: [PATCH] features: Screenshot set page titel as label and use URL as reference (for information only) (#139) --- phpstan-baseline.neon | 2 +- src/Command/CreateScreenshotCommand.php | 19 ++++++++++++++++--- src/Dto/Screenshot.php | 2 ++ .../config/definition/utils/screenshots.php | 4 ++++ 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 1df498e..33ad337 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -77,7 +77,7 @@ parameters: - message: "#^Cannot cast mixed to int\\.$#" - count: 4 + count: 6 path: src/Command/CreateScreenshotCommand.php - diff --git a/src/Command/CreateScreenshotCommand.php b/src/Command/CreateScreenshotCommand.php index 4286da7..42363a5 100644 --- a/src/Command/CreateScreenshotCommand.php +++ b/src/Command/CreateScreenshotCommand.php @@ -19,6 +19,7 @@ use Symfony\Component\Mime\MimeTypes; use Symfony\Component\Panther\Client; use Symfony\Component\Yaml\Yaml; +use Throwable; use function count; #[AsCommand( @@ -95,7 +96,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $format = $input->getOption('format'); $client = clone $this->webClient; - $client->request('GET', $url); + $crawler = $client->request('GET', $url); + $tmpName = $this->filesystem ->tempnam('', 'pwa-'); if ($width !== null && $height !== null) { @@ -107,6 +109,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int ->window() ->fullscreen(); $client->takeScreenshot($tmpName); + try { + $client->waitFor('title', 5, 500); + $result = preg_match("/(.+)<\/title>/i", $crawler->html(), $title); + $title = $result === 1 ? $title[1] : null; + } catch (Throwable) { + $title = null; + } if ($format !== null) { $data = $this->imageProcessor->process(file_get_contents($tmpName), null, null, $format); @@ -138,12 +147,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int $config = [ 'src' => $asset === null ? $filename : $asset->logicalPath, - 'width' => $width, - 'height' => $height, + 'width' => (int) $width, + 'height' => (int) $height, + 'reference' => $url, ]; if ($outputMimeType !== null) { $config['type'] = $outputMimeType; } + if ($title !== null && $title !== '') { + $config['label'] = $title; + } $io->success('Screenshot saved. You can now use it in your application configuration file.'); $io->writeln(Yaml::dump([ 'pwa' => [ diff --git a/src/Dto/Screenshot.php b/src/Dto/Screenshot.php index b58fac2..7ab30c7 100644 --- a/src/Dto/Screenshot.php +++ b/src/Dto/Screenshot.php @@ -22,6 +22,8 @@ final class Screenshot public null|string $label = null; + public null|string $reference = null; + public null|string $platform = null; public null|string $type = null; diff --git a/src/Resources/config/definition/utils/screenshots.php b/src/Resources/config/definition/utils/screenshots.php index 7a27ad6..4132117 100644 --- a/src/Resources/config/definition/utils/screenshots.php +++ b/src/Resources/config/definition/utils/screenshots.php @@ -51,6 +51,10 @@ function getScreenshotsNode(string $info): ArrayNodeDefinition ->info('The format of the screenshot. Will convert the file if set.') ->example(['image/jpg', 'image/png', 'image/webp']) ->end() + ->scalarNode('reference') + ->defaultNull() + ->info('The URL of the screenshot. Only for reference and not used by the bundle.') + ->end() ->end() ->end();