Skip to content

Commit

Permalink
Extend KnpSnappy pdf options instead of overriding them
Browse files Browse the repository at this point in the history
  • Loading branch information
coldic3 committed Mar 30, 2022
1 parent 0b0d548 commit b640c6b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
3 changes: 2 additions & 1 deletion spec/Generator/TwigToPdfGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function let(
$twig,
$pdfGenerator,
$fileLocator,
['allow' => 'allowed_file_in_knp_snappy_config.png'],
['swans.png']
);
}
Expand All @@ -52,7 +53,7 @@ function it_generates_pdf_from_twig_template(
$pdfGenerator
->getOutputFromHtml(
'<html>I am a pdf file generated from twig template</html>',
['allow' => ['located-path/swans.png']]
['allow' => ['allowed_file_in_knp_snappy_config.png', 'located-path/swans.png']]
)
->willReturn('PDF FILE')
;
Expand Down
20 changes: 16 additions & 4 deletions src/Generator/TwigToPdfGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct(
private Environment $twig,
private GeneratorInterface $pdfGenerator,
private FileLocatorInterface $fileLocator,
private array $knpSnappyOptions,
private array $allowedFiles
) {
}
Expand All @@ -37,12 +38,23 @@ public function generate(string $templateName, array $templateParams): string

private function getOptions(): array
{
$options = $this->knpSnappyOptions;

if (empty($this->allowedFiles)) {
return [];
return $options;
}

if (!isset($options['allow'])) {
$options['allow'] = [];
} elseif (!is_array($options['allow'])) {
$options['allow'] = [$options['allow']];
}

return [
'allow' => array_map(fn ($file) => $this->fileLocator->locate($file), $this->allowedFiles),
];
$options['allow'] = array_merge(
$options['allow'],
array_map(fn ($file) => $this->fileLocator->locate($file), $this->allowedFiles)
);

return $options;
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services/generators.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<argument type="service" id="twig" />
<argument type="service" id="knp_snappy.pdf" />
<argument type="service" id="file_locator" />
<argument>%knp_snappy.pdf.options%</argument>
<argument>%sylius_invoicing.pdf_generator.allowed_files%</argument>
</service>
</services>
Expand Down

0 comments on commit b640c6b

Please sign in to comment.