-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for custom user agent in screenshot generation (#166)
* Add support for custom user agent in screenshot generation 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
Showing
7 changed files
with
137 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpomkyLabs\PwaBundle\EventSubscriber; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
use Symfony\Component\HttpKernel\Profiler\Profiler; | ||
|
||
final readonly class ScreenshotSubscriber implements EventSubscriberInterface | ||
{ | ||
public function __construct( | ||
#[Autowire(service: 'profiler')] | ||
private ?Profiler $profiler = null, | ||
#[Autowire(param: 'spomky_labs_pwa.screenshot_user_agent')] | ||
private null|string $userAgent = null, | ||
) { | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
RequestEvent::class => 'onRequest', | ||
]; | ||
} | ||
|
||
public function onRequest(RequestEvent $event): void | ||
{ | ||
if (! $event->isMainRequest() || $this->profiler === null) { | ||
return; | ||
} | ||
|
||
$userAgent = $event->getRequest() | ||
->headers->get('user-agent'); | ||
if ($userAgent === null) { | ||
return; | ||
} | ||
$userAgentToFind = $this->userAgent ?? 'HeadlessChrome'; | ||
if (! str_contains($userAgent, $userAgentToFind)) { | ||
return; | ||
} | ||
|
||
$this->profiler->disable(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters