Skip to content

Commit

Permalink
Add support for custom user agent in screenshot generation
Browse files Browse the repository at this point in the history
This update includes a new service, `ScreenshotSubscriber`, that disables profiler if the user agent matches a specified one. Also, a custom user agent can now be used when using the `CreateScreenshotCommand`, and this setting is configurable in `web_client.php`. Finally, some PHPStan warnings were addressed and the "ext-sockets" package was added to the dev dependencies.
  • Loading branch information
Spomky committed Apr 7, 2024
1 parent dcff6d9 commit 5e0c38b
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/Command/CreateScreenshotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,18 @@
)]
final class CreateScreenshotCommand extends Command
{
private readonly Client $webClient;

public function __construct(
private readonly AssetMapperInterface $assetMapper,
private readonly Filesystem $filesystem,
#[Autowire('%kernel.project_dir%')]
private readonly string $projectDir,
private readonly null|ImageProcessorInterface $imageProcessor,
#[Autowire('@pwa.web_client')]
null|Client $webClient = null,
private readonly null|Client $webClient = null,
#[Autowire(param: 'spomky_labs_pwa.screenshot_user_agent')]
private readonly null|string $userAgent = null,
) {
parent::__construct();
if ($webClient === null) {
$options = [
'port' => $this->getAvailablePort(),
'capabilities' => [
'acceptInsecureCerts' => true,
],
];
$arguments = $this->getDefaultArguments();
if ($this->userAgent !== null) {
$arguments[] = sprintf('--user-agent=%s', $this->userAgent);
}
$webClient = Client::createChromeClient(arguments: $arguments, options: $options);
}
$this->webClient = $webClient;
}

public function isEnabled(): bool
Expand Down Expand Up @@ -108,12 +92,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$width = $input->getOption('width');
$format = $input->getOption('format');

$client = clone $this->webClient;
$client = $this->getClient();
$crawler = $client->request('GET', $url);

$tmpName = $this->filesystem
->tempnam('', 'pwa-');
if ($width !== null && $height !== null) {
if ($width < 0 || $height < 0) {
$io->error('Width and height must be positive integers.');
return self::FAILURE;
}
$client->manage()
->window()
->setSize(new WebDriverDimension((int) $width, (int) $height));
Expand Down Expand Up @@ -217,4 +205,23 @@ private function getDefaultArguments(): array

return $args;
}

private function getClient(): Client
{
if ($this->webClient !== null) {
return clone $this->webClient;
}
$options = [
'port' => $this->getAvailablePort(),
'capabilities' => [
'acceptInsecureCerts' => true,
],
];
$arguments = $this->getDefaultArguments();
if ($this->userAgent !== null) {
$arguments[] = sprintf('--user-agent=%s', $this->userAgent);
}

return Client::createChromeClient(arguments: $arguments, options: $options);
}
}

0 comments on commit 5e0c38b

Please sign in to comment.