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

Add some logs #59

Merged
merged 3 commits into from
Jun 6, 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
16 changes: 9 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,22 @@
"php": ">=8.2",
"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-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",
"monolog/monolog": "^3.6",
"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/twig-bundle": "^6.4 || ^7.0",
"symfony/stopwatch": "^6.4 || ^7.0"
"symfony/stopwatch": "^6.4 || ^7.0",
"symfony/twig-bundle": "^6.4 || ^7.0"
},
"config": {
"allow-plugins": {
Expand All @@ -46,6 +47,7 @@
"sort-packages": true
},
"suggest": {
"symfony/twig-bundle": "Allows you to use Twig to render templates into PDF"
"symfony/twig-bundle": "Allows you to use Twig to render templates into PDF",
"monolog/monolog": "Enables logging througout the generating process."
}
}
8 changes: 8 additions & 0 deletions config/builder_pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@
return static function (ContainerConfigurator $container): void {
$services = $container->services();

$services->defaults()
->tag('monolog.logger', ['channel' => 'sensiolabs_gotenberg'])
;

$services->set('.sensiolabs_gotenberg.pdf_builder.html', HtmlPdfBuilder::class)
->share(false)
->args([
service('sensiolabs_gotenberg.client'),
service('sensiolabs_gotenberg.asset.base_dir_formatter'),
service('twig')->nullOnInvalid(),
])
->call('setLogger', [service('logger')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.pdf_builder')
;

Expand All @@ -28,6 +33,7 @@
service('twig')->nullOnInvalid(),
service('router')->nullOnInvalid(),
])
->call('setLogger', [service('logger')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.pdf_builder')
;

Expand All @@ -38,6 +44,7 @@
service('sensiolabs_gotenberg.asset.base_dir_formatter'),
service('twig')->nullOnInvalid(),
])
->call('setLogger', [service('logger')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.pdf_builder')
;

Expand All @@ -47,6 +54,7 @@
service('sensiolabs_gotenberg.client'),
service('sensiolabs_gotenberg.asset.base_dir_formatter'),
])
->call('setLogger', [service('logger')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.pdf_builder')
;
};
7 changes: 7 additions & 0 deletions config/builder_screenshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
return static function (ContainerConfigurator $container): void {
$services = $container->services();

$services->defaults()
->tag('monolog.logger', ['channel' => 'sensiolabs_gotenberg'])
;

$services->set('.sensiolabs_gotenberg.screenshot_builder.html', HtmlScreenshotBuilder::class)
->share(false)
->args([
service('sensiolabs_gotenberg.client'),
service('sensiolabs_gotenberg.asset.base_dir_formatter'),
service('twig')->nullOnInvalid(),
])
->call('setLogger', [service('logger')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.screenshot_builder')
;

Expand All @@ -27,6 +32,7 @@
service('twig')->nullOnInvalid(),
service('router')->nullOnInvalid(),
])
->call('setLogger', [service('logger')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.screenshot_builder')
;

Expand All @@ -37,6 +43,7 @@
service('sensiolabs_gotenberg.asset.base_dir_formatter'),
service('twig')->nullOnInvalid(),
])
->call('setLogger', [service('logger')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.screenshot_builder')
;
};
15 changes: 15 additions & 0 deletions src/Builder/Pdf/AbstractPdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Sensiolabs\GotenbergBundle\Builder\Pdf;

use Psr\Log\LoggerInterface;
use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface;
use Sensiolabs\GotenbergBundle\Client\GotenbergResponse;
use Sensiolabs\GotenbergBundle\Enum\Part;
Expand All @@ -13,6 +14,8 @@

abstract class AbstractPdfBuilder implements PdfBuilderInterface
{
protected LoggerInterface|null $logger = null;

/**
* @var array<string, mixed>
*/
Expand Down Expand Up @@ -59,6 +62,11 @@ public function __construct(
];
}

public function setLogger(LoggerInterface|null $logger): void
{
$this->logger = $logger;
}

/**
* @return array<string, mixed>
*/
Expand Down Expand Up @@ -96,6 +104,10 @@ public function fileName(string $fileName, string $headerDisposition = HeaderUti

public function generate(): GotenbergResponse
{
$this->logger?->debug('Generating PDF file using {sensiolabs_gotenberg.builder} builder.', [
'sensiolabs_gotenberg.builder' => $this::class,
]);

$pdfResponse = $this->gotenbergClient->call($this->getEndpoint(), $this->getMultipartFormData());

if (null !== $this->fileName) {
Expand Down Expand Up @@ -125,6 +137,9 @@ public function getMultipartFormData(): array
$preCallback = null;

if (\array_key_exists($key, $this->normalizers)) {
$this->logger?->debug('Normalizer found for key {sensiolabs_gotenberg.key}.', [
'sensiolabs_gotenberg.key' => $key,
]);
$preCallback = $this->normalizers[$key](...);
}

Expand Down
12 changes: 12 additions & 0 deletions src/Builder/Screenshot/AbstractScreenshotBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Sensiolabs\GotenbergBundle\Builder\Screenshot;

use Psr\Log\LoggerInterface;
use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface;
use Sensiolabs\GotenbergBundle\Client\GotenbergResponse;
use Sensiolabs\GotenbergBundle\Enum\Part;
Expand All @@ -13,6 +14,8 @@

abstract class AbstractScreenshotBuilder implements ScreenshotBuilderInterface
{
protected LoggerInterface|null $logger = null;

/**
* @var array<string, mixed>
*/
Expand Down Expand Up @@ -56,6 +59,11 @@ public function __construct(
];
}

public function setLogger(LoggerInterface|null $logger): void
{
$this->logger = $logger;
}

/**
* @return array<string, mixed>
*/
Expand Down Expand Up @@ -93,6 +101,10 @@ public function fileName(string $fileName, string $headerDisposition = HeaderUti

public function generate(): GotenbergResponse
{
$this->logger?->debug('Generating Screenshot file using {sensiolabs_gotenberg.builder} builder.', [
'sensiolabs_gotenberg.builder' => $this::class,
]);

$pdfResponse = $this->gotenbergClient->call($this->getEndpoint(), $this->getMultipartFormData());

if (null !== $this->fileName) {
Expand Down
4 changes: 2 additions & 2 deletions src/Client/GotenbergClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final readonly class GotenbergClient implements GotenbergClientInterface
final class GotenbergClient implements GotenbergClientInterface
Neirda24 marked this conversation as resolved.
Show resolved Hide resolved
{
public function __construct(private string $gotenbergBaseUri, private HttpClientInterface $client)
public function __construct(private readonly string $gotenbergBaseUri, private readonly HttpClientInterface $client)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->defaultValue('%kernel.project_dir%/assets')
->end()
->scalarNode('http_client')
->info('HTTP Client reference to use. Defaults to "http_client".')
->info('HTTP Client reference to use.')
->defaultValue('http_client')
->end()
->arrayNode('default_options')
Expand Down
2 changes: 1 addition & 1 deletion tests/Client/GotenbergClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testCallIsCorrectlyFormatted(): void
self::assertSame('http://localhost:3000/some/url', $mockResponse->getRequestUrl());

$requestHeaders = array_reduce($mockResponse->getRequestOptions()['headers'], function (array $carry, string $header): array {
[$key, $value] = \explode(': ', $header, 2);
[$key, $value] = explode(': ', $header, 2);

$carry[$key] ??= [];
$carry[$key][] = $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function testGotenbergConfiguredWithValidConfig(): void
foreach ($builder as $builderName => $expectedConfig) {
$gotenbergDefinition = $containerBuilder->getDefinition(".sensiolabs_gotenberg.{$builderType}_builder.{$builderName}");
$methodCalls = $gotenbergDefinition->getMethodCalls();
$setConfiguration = $methodCalls[0];
$setConfiguration = $methodCalls[1];

$config = $setConfiguration[1][0];

Expand Down