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] Allow changing url generator context for url*builder #60

Merged
merged 3 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 12 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "sensiolabs/gotenberg-bundle",
"description": "Gotenberg support for Symfony",
"keywords": ["gotenberg", "symfony", "pdf", "screenshot", "office", "bundle"],
"type": "symfony-bundle",
"license": "MIT",
"autoload": {
Expand All @@ -20,24 +21,28 @@
}
],
"require": {
"php": ">=8.2",
"php": ">=8.1",
"ext-json": "*",
"symfony/config": "^6.4 || ^7.0",
"symfony/dependency-injection": "^6.4 || ^7.0",
"symfony/filesystem": "^6.4 || ^7.0",
"symfony/http-client": "^6.4 || ^7.0",
"symfony/http-foundation": "^6.4 || ^7.0",
"symfony/http-kernel": "^6.4 || ^7.0",
"symfony/mime": "^6.4 || ^7.0",
"symfony/string": "^6.4 || ^7.0",
"symfony/http-client": "^6.4 || ^7.0",
"symfony/filesystem": "^6.4 || ^7.0"
"symfony/string": "^6.4 || ^7.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.41",
"phpstan/phpstan": "^1.10",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-symfony": "^1.3",
"phpunit/phpunit": "^10.4",
"symfony/framework-bundle": "^6.4 || ^7.0",
"symfony/monolog-bundle": "^3.10",
"symfony/stopwatch": "^6.4 || ^7.0",
"symfony/twig-bundle": "^6.4 || ^7.0",
"symfony/stopwatch": "^6.4 || ^7.0"
"symfony/var-dumper": "^6.4 || ^7.0"
},
"config": {
"allow-plugins": {
Expand All @@ -46,6 +51,7 @@
"sort-packages": true
},
"suggest": {
"symfony/monolog-bundle": "Enables logging througout the generating process.",
"symfony/twig-bundle": "Allows you to use Twig to render templates into PDF"
}
}
12 changes: 12 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ parameters:
- 'src'
- 'tests'
ignoreErrors:
-
message: "#^Cannot call method generate\\(\\) on Symfony\\\\Component\\\\Routing\\\\Generator\\\\UrlGeneratorInterface\\|null\\.$#"
count: 1
path: src/Builder/Pdf/UrlPdfBuilder.php
-
message: "#^Cannot call method getContext\\(\\) on Symfony\\\\Component\\\\Routing\\\\Generator\\\\UrlGeneratorInterface\\|null\\.$#"
count: 1
path: src/Builder/Pdf/UrlPdfBuilder.php
-
message: "#^Cannot call method setContext\\(\\) on Symfony\\\\Component\\\\Routing\\\\Generator\\\\UrlGeneratorInterface\\|null\\.$#"
count: 2
path: src/Builder/Pdf/UrlPdfBuilder.php
-
message: "#^Cannot use array destructuring on array\\<int, string\\|null\\>\\|null\\.$#"
count: 1
Expand Down
2 changes: 1 addition & 1 deletion src/Builder/Pdf/AbstractChromiumPdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ protected function withRenderedPart(Part $pdfPart, string $template, array $cont
return $this;
}

private function addConfiguration(string $configurationName, mixed $value): void
protected function addConfiguration(string $configurationName, mixed $value): void
{
match ($configurationName) {
'single_page' => $this->singlePage($value),
Expand Down
55 changes: 52 additions & 3 deletions src/Builder/Pdf/UrlPdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,34 @@
use Sensiolabs\GotenbergBundle\Exception\MissingRequiredFieldException;
use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RequestContext;
use Twig\Environment;

final class UrlPdfBuilder extends AbstractChromiumPdfBuilder
{
private const ENDPOINT = '/forms/chromium/convert/url';

private RequestContext|null $requestContext = null;

public function __construct(
GotenbergClientInterface $gotenbergClient,
AssetBaseDirFormatter $asset,
Environment|null $twig = null,
private readonly UrlGeneratorInterface|null $urlGenerator = null,
) {
parent::__construct($gotenbergClient, $asset, $twig);

$this->addNormalizer('route', $this->generateUrlFromRoute(...));
}

/**
* @param array{base_url: string} $requestContext
*/
public function setRequestContext(array $requestContext): self
{
$this->requestContext = RequestContext::fromUri($requestContext['base_url']);
Jean-Beru marked this conversation as resolved.
Show resolved Hide resolved

return $this;
}

/**
Expand All @@ -34,20 +49,46 @@ public function url(string $url): self
/**
* @param string $name #Route
* @param array<mixed> $parameters
*
* @phpstan-assert !null $this->urlGenerator
*/
public function route(string $name, array $parameters = []): self
{
if (null === $this->urlGenerator) {
throw new \LogicException(sprintf('Router is required to use "%s" method. Try to run "composer require symfony/routing".', __METHOD__));
}

return $this->url($this->urlGenerator->generate($name, $parameters, UrlGeneratorInterface::ABSOLUTE_URL));
$this->formFields['route'] = [$name, $parameters];

return $this;
}

/**
* @param array{string, array<mixed>} $value
*
* @return array{url: string}
*/
private function generateUrlFromRoute(array $value): array
{
[$route, $parameters] = $value;

$requestContext = $this->urlGenerator->getContext();

if (null !== $this->requestContext) {
$this->urlGenerator->setContext($this->requestContext);
Jean-Beru marked this conversation as resolved.
Show resolved Hide resolved
}

try {
return ['url' => $this->urlGenerator->generate($route, $parameters, UrlGeneratorInterface::ABSOLUTE_URL)];
} finally {
$this->urlGenerator->setContext($requestContext);
}
}

public function getMultipartFormData(): array
{
if (!\array_key_exists('url', $this->formFields)) {
throw new MissingRequiredFieldException('URL is required');
if (!\array_key_exists('url', $this->formFields) && !\array_key_exists('route', $this->formFields)) {
throw new MissingRequiredFieldException('URL (or route) is required');
Jean-Beru marked this conversation as resolved.
Show resolved Hide resolved
}

return parent::getMultipartFormData();
Expand All @@ -57,4 +98,12 @@ protected function getEndpoint(): string
{
return self::ENDPOINT;
}

protected function addConfiguration(string $configurationName, mixed $value): void
{
match ($configurationName) {
'request_context' => $this->setRequestContext($value),
default => parent::addConfiguration($configurationName, $value),
};
}
}
Loading