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

features: Screenshot set page title as label and use URL as reference… #139

Merged
merged 1 commit into from
Mar 18, 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
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