Skip to content

Commit

Permalink
Add some logs (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neirda24 committed Jun 6, 2024
1 parent 8e75b85 commit 7447f99
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 5 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
},
"suggest": {
"symfony/monolog-bundle": "Enables logging througout the generating process.",
"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()])
->call('setRequestContext', [service('.sensiolabs_gotenberg.request_context')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.pdf_builder')
;
Expand All @@ -39,6 +45,7 @@
service('sensiolabs_gotenberg.asset.base_dir_formatter'),
service('twig')->nullOnInvalid(),
])
->call('setLogger', [service('logger')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.pdf_builder')
;

Expand All @@ -48,6 +55,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()])
->call('setRequestContext', [service('.sensiolabs_gotenberg.request_context')->nullOnInvalid()])
->tag('sensiolabs_gotenberg.screenshot_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.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\Enumeration\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\Enumeration\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 @@ -50,6 +53,11 @@ public function __construct(
];
}

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

/**
* @return array<string, mixed>
*/
Expand Down Expand Up @@ -87,6 +95,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
{
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('request_context')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public function testUrlBuildersCanChangeTheirRequestContext(string $serviceName)
self::assertSame('https://sensiolabs.com', $requestContextDefinition->getArgument(0));

$urlBuilderDefinition = $containerBuilder->getDefinition($serviceName);
self::assertCount(2, $urlBuilderDefinition->getMethodCalls());
self::assertCount(3, $urlBuilderDefinition->getMethodCalls());

$indexedMethodCalls = [];
foreach ($urlBuilderDefinition->getMethodCalls() as $methodCall) {
Expand Down
3 changes: 3 additions & 0 deletions tests/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Sensiolabs\GotenbergBundle\Tests;

use Psr\Log\NullLogger;
use Sensiolabs\GotenbergBundle\GotenbergInterface;
use Sensiolabs\GotenbergBundle\SensiolabsGotenbergBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

Expand Down Expand Up @@ -43,5 +45,6 @@ public function registerBundles(): iterable
public function process(ContainerBuilder $container): void
{
$container->getAlias(GotenbergInterface::class)->setPublic(true);
$container->setDefinition('logger', new Definition(NullLogger::class));
}
}

0 comments on commit 7447f99

Please sign in to comment.