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

[Feature] Async generation #84

Merged
merged 26 commits into from
Nov 19, 2024
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9915a29
Add ability to generate asynchronously using webhooks
maelanleborgne Jun 19, 2024
ec46e92
switch to constructor injection
maelanleborgne Sep 9, 2024
b26c12f
cleanup after review
maelanleborgne Oct 3, 2024
f97d4f5
Merge branch 'main' of github.com:Neirda24/GotenbergBundle into featu…
Neirda24 Oct 24, 2024
abd6e47
[UPDATE] Allow to set extra headers when using webhook
Neirda24 Oct 24, 2024
0c6cb61
[UPDATE] Auto docs
Neirda24 Oct 24, 2024
9fb37d7
[UPDATE] Remove custom id generator for the moment
Neirda24 Oct 31, 2024
ed70e51
[UPDATE] Remove strict types
Neirda24 Oct 31, 2024
f6509f6
[UPDATE] Docs
Neirda24 Oct 31, 2024
a40ec71
[UPDATE] Doc & doc generation
Neirda24 Nov 1, 2024
9faba1a
Merge branch 'main' of github.com:sensiolabs/GotenbergBundle into fea…
Neirda24 Nov 1, 2024
967bfad
[Fix] A few stuff
Neirda24 Nov 1, 2024
823faba
[UPDATE] A few stuff
Neirda24 Nov 1, 2024
83e1d26
[UPDATE] Add webhook methods
Neirda24 Nov 3, 2024
42a8544
[UPDATE] Exclude some methods to auto docs
Neirda24 Nov 3, 2024
a574f51
[UPDATE] Exclude some methods to auto docs
Neirda24 Nov 3, 2024
a9694cb
[UPDATE] Change equality check
Neirda24 Nov 3, 2024
8d39bd9
Update profiler icons + Fix webhook configuration + fix some tests
Neirda24 Nov 15, 2024
ebb4656
[Update] a few things
Neirda24 Nov 15, 2024
0f00fdf
fix typo
Neirda24 Nov 15, 2024
ee72e12
fix typo
Neirda24 Nov 15, 2024
eb474ad
Add documentation for webhook
Neirda24 Nov 16, 2024
44764dd
Update docs for webhooks
Neirda24 Nov 16, 2024
f8a1de9
Update docs for webhooks
Neirda24 Nov 16, 2024
c461a88
Add tests on async trait
Neirda24 Nov 16, 2024
931943a
[FIX] CI
Neirda24 Nov 19, 2024
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
6 changes: 6 additions & 0 deletions config/builder_pdf.php
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
service('twig')->nullOnInvalid(),
])
->call('setLogger', [service('logger')->nullOnInvalid()])
->call('setWebhookConfigurationRegistry', [service('.sensiolabs_gotenberg.webhook_configuration_registry')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.pdf_builder')
;

@@ -38,6 +39,7 @@
service('router')->nullOnInvalid(),
])
->call('setLogger', [service('logger')->nullOnInvalid()])
->call('setWebhookConfigurationRegistry', [service('.sensiolabs_gotenberg.webhook_configuration_registry')->nullOnInvalid()])
->call('setRequestContext', [service('.sensiolabs_gotenberg.request_context')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.pdf_builder')
;
@@ -50,6 +52,7 @@
service('request_stack'),
service('twig')->nullOnInvalid(),
])
->call('setWebhookConfigurationRegistry', [service('.sensiolabs_gotenberg.webhook_configuration_registry')->nullOnInvalid()])
->call('setLogger', [service('logger')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.pdf_builder')
;
@@ -61,6 +64,7 @@
service('sensiolabs_gotenberg.asset.base_dir_formatter'),
])
->call('setLogger', [service('logger')->nullOnInvalid()])
->call('setWebhookConfigurationRegistry', [service('.sensiolabs_gotenberg.webhook_configuration_registry')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.pdf_builder')
;

@@ -71,6 +75,7 @@
service('sensiolabs_gotenberg.asset.base_dir_formatter'),
])
->call('setLogger', [service('logger')->nullOnInvalid()])
->call('setWebhookConfigurationRegistry', [service('.sensiolabs_gotenberg.webhook_configuration_registry')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.pdf_builder')
;

@@ -81,6 +86,7 @@
service('sensiolabs_gotenberg.asset.base_dir_formatter'),
])
->call('setLogger', [service('logger')->nullOnInvalid()])
->call('setWebhookConfigurationRegistry', [service('.sensiolabs_gotenberg.webhook_configuration_registry')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.pdf_builder')
;
};
3 changes: 3 additions & 0 deletions config/builder_screenshot.php
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
service('twig')->nullOnInvalid(),
])
->call('setLogger', [service('logger')->nullOnInvalid()])
->call('setWebhookConfigurationRegistry', [service('.sensiolabs_gotenberg.webhook_configuration_registry')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.screenshot_builder')
;

@@ -35,6 +36,7 @@
service('router')->nullOnInvalid(),
])
->call('setLogger', [service('logger')->nullOnInvalid()])
->call('setWebhookConfigurationRegistry', [service('.sensiolabs_gotenberg.webhook_configuration_registry')->nullOnInvalid()])
->call('setRequestContext', [service('.sensiolabs_gotenberg.request_context')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.screenshot_builder')
;
@@ -48,6 +50,7 @@
service('twig')->nullOnInvalid(),
])
->call('setLogger', [service('logger')->nullOnInvalid()])
->call('setWebhookConfigurationRegistry', [service('.sensiolabs_gotenberg.webhook_configuration_registry')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.screenshot_builder')
;
};
11 changes: 11 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@

use Sensiolabs\GotenbergBundle\Client\GotenbergClient;
use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface;
use Sensiolabs\GotenbergBundle\DependencyInjection\WebhookConfiguration\WebhookConfigurationRegistry;
use Sensiolabs\GotenbergBundle\DependencyInjection\WebhookConfiguration\WebhookConfigurationRegistryInterface;
use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter;
use Sensiolabs\GotenbergBundle\Gotenberg;
use Sensiolabs\GotenbergBundle\GotenbergInterface;
@@ -63,4 +65,13 @@
])
->alias(GotenbergInterface::class, 'sensiolabs_gotenberg')
;

$services->set('.sensiolabs_gotenberg.webhook_configuration_registry', WebhookConfigurationRegistry::class)
->args([
service('router'),
service('.sensiolabs_gotenberg.request_context')->nullOnInvalid(),
])
->tag('sensiolabs_gotenberg.webhook_configuration_registry')
->alias(WebhookConfigurationRegistryInterface::class, '.sensiolabs_gotenberg.webhook_configuration_registry')
;
};
11 changes: 11 additions & 0 deletions src/Builder/AsyncBuilderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Sensiolabs\GotenbergBundle\Builder;

interface AsyncBuilderInterface
{
/**
* Generates a file asynchronously.
*/
public function generateAsync(): string;
}
88 changes: 88 additions & 0 deletions src/Builder/AsyncBuilderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Sensiolabs\GotenbergBundle\Builder;

use Sensiolabs\GotenbergBundle\DependencyInjection\WebhookConfiguration\WebhookConfigurationRegistry;
use Sensiolabs\GotenbergBundle\Exception\WebhookConfigurationException;

trait AsyncBuilderTrait
{
use DefaultBuilderTrait;
private string $webhookUrl;
private string $errorWebhookUrl;
/**
* @var array<string, mixed>
*/
private array $webhookExtraHeaders = [];
private \Closure $operationIdGenerator;
private WebhookConfigurationRegistry|null $webhookConfigurationRegistry = null;

public function generateAsync(): string
{
$operationId = ($this->operationIdGenerator ?? self::defaultOperationIdGenerator(...))();
$this->logger?->debug('Generating a file asynchronously with operation id {sensiolabs_gotenberg.operation_id} using {sensiolabs_gotenberg.builder} builder.', [
'sensiolabs_gotenberg.operation_id' => $operationId,
'sensiolabs_gotenberg.builder' => $this::class,
]);

$this->webhookExtraHeaders['X-Gotenberg-Operation-Id'] = $operationId;
$headers = [
'Gotenberg-Webhook-Url' => $this->webhookUrl,
'Gotenberg-Webhook-Error-Url' => $this->errorWebhookUrl,
'Gotenberg-Webhook-Extra-Http-Headers' => json_encode($this->webhookExtraHeaders, \JSON_THROW_ON_ERROR),
];
if (null !== $this->fileName) {
$headers['Gotenberg-Output-Filename'] = basename($this->fileName, '.pdf');
}
$this->client->call($this->getEndpoint(), $this->getMultipartFormData(), $headers);

return $operationId;
}

public function setWebhookConfigurationRegistry(WebhookConfigurationRegistry $registry): static
{
$this->webhookConfigurationRegistry = $registry;

return $this;
}

public function webhookConfiguration(string $webhook): static
{
if (null === $this->webhookConfigurationRegistry) {
throw new WebhookConfigurationException('The WebhookConfigurationRegistry is not available.');
}
$webhookConfiguration = $this->webhookConfigurationRegistry->get($webhook);

return $this->webhookUrls($webhookConfiguration['success'], $webhookConfiguration['error']);
}

public function webhookUrls(string $successWebhook, string|null $errorWebhook = null): static
{
$this->webhookUrl = $successWebhook;
$this->errorWebhookUrl = $errorWebhook ?? $successWebhook;

return $this;
}

/**
* @param array<string, mixed> $extraHeaders
*/
public function webhookExtraHeaders(array $extraHeaders): static
{
$this->webhookExtraHeaders = array_merge($this->webhookExtraHeaders, $extraHeaders);

return $this;
}

public function operationIdGenerator(\Closure $operationIdGenerator): static
{
$this->operationIdGenerator = $operationIdGenerator;

return $this;
}

protected static function defaultOperationIdGenerator(): string
{
return 'gotenberg_'.bin2hex(random_bytes(16)).microtime(true);
}
}
1 change: 1 addition & 0 deletions src/Builder/Pdf/AbstractChromiumPdfBuilder.php
Original file line number Diff line number Diff line change
@@ -586,6 +586,7 @@ protected function addConfiguration(string $configurationName, mixed $value): vo
'fail_on_console_exceptions' => $this->failOnConsoleExceptions($value),
'skip_network_idle_event' => $this->skipNetworkIdleEvent($value),
'metadata' => $this->metadata($value),
'webhook' => null,
default => throw new InvalidBuilderConfiguration(sprintf('Invalid option "%s": no method does not exist in class "%s" to configured it.', $configurationName, static::class)),
};
}
5 changes: 4 additions & 1 deletion src/Builder/Pdf/AbstractPdfBuilder.php
Original file line number Diff line number Diff line change
@@ -2,12 +2,15 @@

namespace Sensiolabs\GotenbergBundle\Builder\Pdf;

use Sensiolabs\GotenbergBundle\Builder\AsyncBuilderInterface;
use Sensiolabs\GotenbergBundle\Builder\AsyncBuilderTrait;
use Sensiolabs\GotenbergBundle\Builder\DefaultBuilderTrait;
use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface;
use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter;

abstract class AbstractPdfBuilder implements PdfBuilderInterface
abstract class AbstractPdfBuilder implements PdfBuilderInterface, AsyncBuilderInterface
{
use AsyncBuilderTrait;
use DefaultBuilderTrait;

public function __construct(
5 changes: 4 additions & 1 deletion src/Builder/Screenshot/AbstractScreenshotBuilder.php
Original file line number Diff line number Diff line change
@@ -2,14 +2,17 @@

namespace Sensiolabs\GotenbergBundle\Builder\Screenshot;

use Sensiolabs\GotenbergBundle\Builder\AsyncBuilderInterface;
use Sensiolabs\GotenbergBundle\Builder\AsyncBuilderTrait;
use Sensiolabs\GotenbergBundle\Builder\DefaultBuilderTrait;
use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface;
use Sensiolabs\GotenbergBundle\Enumeration\Part;
use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter;
use Symfony\Component\Mime\Part\DataPart;

abstract class AbstractScreenshotBuilder implements ScreenshotBuilderInterface
abstract class AbstractScreenshotBuilder implements ScreenshotBuilderInterface, AsyncBuilderInterface
{
use AsyncBuilderTrait;
use DefaultBuilderTrait;

public function __construct(
2 changes: 1 addition & 1 deletion src/Client/GotenbergClient.php
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ public function call(string $endpoint, array $multipartFormData, array $headers
],
);

if (200 !== $response->getStatusCode()) {
if (!\in_array($response->getStatusCode(), [200, 204], true)) {
throw new ClientException($response->getContent(false), $response->getStatusCode());
}

27 changes: 27 additions & 0 deletions src/Debug/Builder/TraceablePdfBuilder.php
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

namespace Sensiolabs\GotenbergBundle\Debug\Builder;

use Sensiolabs\GotenbergBundle\Builder\AsyncBuilderInterface;
use Sensiolabs\GotenbergBundle\Builder\GotenbergFileResult;
use Sensiolabs\GotenbergBundle\Builder\Pdf\PdfBuilderInterface;
use Symfony\Component\Stopwatch\Stopwatch;
@@ -50,6 +51,32 @@ public function generate(): GotenbergFileResult
return $response;
}

public function generateAsync(): string
{
if (!$this->inner instanceof AsyncBuilderInterface) {
throw new \LogicException(sprintf('The inner builder of %s must implement %s.', self::class, AsyncBuilderInterface::class));
}

$name = self::$count.'.'.$this->inner::class.'::'.__FUNCTION__;
++self::$count;

$swEvent = $this->stopwatch?->start($name, 'gotenberg.generate_pdf');
$operationId = $this->inner->generateAsync();
$swEvent?->stop();

$this->pdfs[] = [
'calls' => $this->calls,
'time' => $swEvent?->getDuration(),
'memory' => $swEvent?->getMemory(),
'size' => null,
'fileName' => null,
];

++$this->totalGenerated;

return $operationId;
}

/**
* @param array<mixed> $arguments
*/
41 changes: 34 additions & 7 deletions src/Debug/Builder/TraceableScreenshotBuilder.php
Original file line number Diff line number Diff line change
@@ -2,14 +2,15 @@

namespace Sensiolabs\GotenbergBundle\Debug\Builder;

use Sensiolabs\GotenbergBundle\Builder\AsyncBuilderInterface;
use Sensiolabs\GotenbergBundle\Builder\GotenbergFileResult;
use Sensiolabs\GotenbergBundle\Builder\Screenshot\ScreenshotBuilderInterface;
use Symfony\Component\Stopwatch\Stopwatch;

final class TraceableScreenshotBuilder implements ScreenshotBuilderInterface
{
/**
* @var list<array{'time': float, 'memory': int, 'size': int<0, max>|null, 'fileName': string, 'calls': list<array{'method': string, 'class': class-string<ScreenshotBuilderInterface>, 'arguments': array<mixed>}>}>
* @var list<array{'time': float|null, 'memory': int|null, 'size': int<0, max>|null, 'fileName': string|null, 'calls': list<array{'method': string, 'class': class-string<ScreenshotBuilderInterface>, 'arguments': array<mixed>}>}>
*/
private array $screenshots = [];

@@ -24,7 +25,7 @@

public function __construct(
private readonly ScreenshotBuilderInterface $inner,
private readonly Stopwatch $stopwatch,
private readonly Stopwatch|null $stopwatch,
) {
}

@@ -33,9 +34,9 @@
$name = self::$count.'.'.$this->inner::class.'::'.__FUNCTION__;
++self::$count;

$swEvent = $this->stopwatch->start($name, 'gotenberg.generate_screenshot');
$swEvent = $this->stopwatch?->start($name, 'gotenberg.generate_screenshot');
$response = $this->inner->generate();
$swEvent->stop();
$swEvent?->stop();

$fileName = 'Unknown';
if ($response->getHeaders()->has('Content-Disposition')) {
@@ -43,7 +44,7 @@

/* @see https://onlinephp.io/c/c2606 */
preg_match('#[^;]*;\sfilename="?(?P<fileName>[^"]*)"?#', $response->getHeaders()->get('Content-Disposition', ''), $matches);
$fileName = $matches['fileName'];

Check failure on line 47 in src/Debug/Builder/TraceableScreenshotBuilder.php

GitHub Actions / PHPStan

Offset 'fileName' does not exist on array{0?: string, fileName?: string, 1?: string}.
}

$lengthInBytes = null;
@@ -53,8 +54,8 @@

$this->screenshots[] = [
'calls' => $this->calls,
'time' => $swEvent->getDuration(),
'memory' => $swEvent->getMemory(),
'time' => $swEvent?->getDuration(),
'memory' => $swEvent?->getMemory(),
'status' => $response->getStatusCode(),
'size' => $lengthInBytes,
'fileName' => $fileName,
@@ -65,6 +66,32 @@
return $response;
}

public function generateAsync(): string
{
if (!$this->inner instanceof AsyncBuilderInterface) {
throw new \LogicException(sprintf('The inner builder of %s must implement %s.', self::class, AsyncBuilderInterface::class));
}

$name = self::$count.'.'.$this->inner::class.'::'.__FUNCTION__;
++self::$count;

$swEvent = $this->stopwatch?->start($name, 'gotenberg.generate_screenshot');
$operationId = $this->inner->generateAsync();
$swEvent?->stop();

$this->screenshots[] = [
'calls' => $this->calls,
'time' => $swEvent?->getDuration(),
'memory' => $swEvent?->getMemory(),
'size' => null,
'fileName' => null,
];

++$this->totalGenerated;

return $operationId;
}

/**
* @param array<mixed> $arguments
*/
@@ -86,7 +113,7 @@
}

/**
* @return list<array{'time': float, 'memory': int, 'size': int<0, max>|null, 'fileName': string, 'calls': list<array{'class': class-string<ScreenshotBuilderInterface>, 'method': string, 'arguments': array<mixed>}>}>
* @return list<array{'time': float|null, 'memory': int|null, 'size': int<0, max>|null, 'fileName': string|null, 'calls': list<array{'class': class-string<ScreenshotBuilderInterface>, 'method': string, 'arguments': array<mixed>}>}>
*/
public function getFiles(): array
{
Loading

Unchanged files with check annotations Beta

/* @see https://onlinephp.io/c/c2606 */
preg_match('#[^;]*;\sfilename="?(?P<fileName>[^"]*)"?#', $disposition, $matches);
return $matches['fileName'];

Check failure on line 39 in src/Client/GotenbergResponse.php

GitHub Actions / PHPStan

Offset 'fileName' does not exist on array{0?: string, fileName?: string, 1?: string}.
}
return null;
/* @see https://onlinephp.io/c/e8233 */
preg_match('#^multipart/form-data; boundary=(?P<boundary>.*)$#', $requestContentType, $matches);
$boundary = $matches['boundary'];

Check failure on line 66 in tests/Client/GotenbergClientTest.php

GitHub Actions / PHPStan

Offset 'boundary' does not exist on array{0?: string, boundary?: string, 1?: string}.
$requestBody = $mockResponse->getRequestOptions()['body'];
self::assertSame(