Skip to content

Commit

Permalink
features: Screenshot set page titel as label and use URL as reference…
Browse files Browse the repository at this point in the history
… (for information only) (#139)
  • Loading branch information
Spomky authored Mar 18, 2024
1 parent ade7573 commit 2a312a7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ parameters:

-
message: "#^Cannot cast mixed to int\\.$#"
count: 4
count: 6
path: src/Command/CreateScreenshotCommand.php

-
Expand Down
19 changes: 16 additions & 3 deletions src/Command/CreateScreenshotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand All @@ -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>(.+)<\/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);
Expand Down Expand Up @@ -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' => [
Expand Down
2 changes: 2 additions & 0 deletions src/Dto/Screenshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/definition/utils/screenshots.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 2a312a7

Please sign in to comment.