From c8a8aee539035e196d5d236a79bddfddd98242be Mon Sep 17 00:00:00 2001 From: Kevin Kaniaburka Date: Mon, 28 Mar 2022 22:11:30 +0200 Subject: [PATCH 1/7] Empty options in KnpSnappy pdf configuration --- tests/Application/config/packages/knp_snappy.yaml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Application/config/packages/knp_snappy.yaml b/tests/Application/config/packages/knp_snappy.yaml index 3b277fcd..cb0b804a 100644 --- a/tests/Application/config/packages/knp_snappy.yaml +++ b/tests/Application/config/packages/knp_snappy.yaml @@ -2,5 +2,4 @@ knp_snappy: pdf: enabled: true binary: '%env(resolve:WKHTMLTOPDF_PATH)%' - options: - enable-local-file-access: true + options: [] From 91070d5fb2ca320b2efc39319e3ddf3b4c0bb3bf Mon Sep 17 00:00:00 2001 From: Kevin Kaniaburka Date: Mon, 28 Mar 2022 22:54:50 +0200 Subject: [PATCH 2/7] Make allowed files configurable for PDF generator --- src/DependencyInjection/Configuration.php | 124 +++++++++++------- .../SyliusInvoicingExtension.php | 2 + src/Resources/config/config.yml | 5 + .../SyliusInvoicingConfigurationTest.php | 48 +++++++ 4 files changed, 129 insertions(+), 50 deletions(-) create mode 100644 tests/Unit/DependencyInjection/SyliusInvoicingConfigurationTest.php diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 58e4f4fd..69274dfd 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -32,6 +32,7 @@ use Sylius\InvoicingPlugin\Factory\InvoiceShopBillingDataFactory; use Sylius\InvoicingPlugin\Factory\LineItemFactory; use Sylius\InvoicingPlugin\Factory\TaxItemFactory; +use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; @@ -42,7 +43,15 @@ public function getConfigTreeBuilder(): TreeBuilder $treeBuilder = new TreeBuilder('sylius_invoicing_plugin'); $rootNode = $treeBuilder->getRootNode(); - $rootNode + $this->addResourcesSection($rootNode); + $this->addPdfGeneratorSection($rootNode); + + return $treeBuilder; + } + + private function addResourcesSection(ArrayNodeDefinition $node): void + { + $node ->children() ->arrayNode('resources') ->addDefaultsIfNotSet() @@ -78,63 +87,63 @@ public function getConfigTreeBuilder(): TreeBuilder ->end() ->end() ->end() - ->end() - ->arrayNode('shop_billing_data') - ->addDefaultsIfNotSet() - ->children() - ->variableNode('options')->end() - ->arrayNode('classes') - ->addDefaultsIfNotSet() - ->children() - ->scalarNode('model')->defaultValue(InvoiceShopBillingData::class)->cannotBeEmpty()->end() - ->scalarNode('interface')->defaultValue(InvoiceShopBillingDataInterface::class)->cannotBeEmpty()->end() - ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end() - ->scalarNode('factory')->defaultValue(InvoiceShopBillingDataFactory::class)->cannotBeEmpty()->end() - ->scalarNode('repository')->cannotBeEmpty()->end() - ->end() + ->end() + ->arrayNode('shop_billing_data') + ->addDefaultsIfNotSet() + ->children() + ->variableNode('options')->end() + ->arrayNode('classes') + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('model')->defaultValue(InvoiceShopBillingData::class)->cannotBeEmpty()->end() + ->scalarNode('interface')->defaultValue(InvoiceShopBillingDataInterface::class)->cannotBeEmpty()->end() + ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end() + ->scalarNode('factory')->defaultValue(InvoiceShopBillingDataFactory::class)->cannotBeEmpty()->end() + ->scalarNode('repository')->cannotBeEmpty()->end() ->end() ->end() ->end() - ->arrayNode('line_item') - ->addDefaultsIfNotSet() - ->children() - ->variableNode('options')->end() - ->arrayNode('classes') - ->addDefaultsIfNotSet() - ->children() - ->scalarNode('model')->defaultValue(LineItem::class)->cannotBeEmpty()->end() - ->scalarNode('interface')->defaultValue(LineItemInterface::class)->cannotBeEmpty()->end() - ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end() - ->scalarNode('factory')->defaultValue(LineItemFactory::class)->cannotBeEmpty()->end() - ->scalarNode('repository')->cannotBeEmpty()->end() - ->end() + ->end() + ->arrayNode('line_item') + ->addDefaultsIfNotSet() + ->children() + ->variableNode('options')->end() + ->arrayNode('classes') + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('model')->defaultValue(LineItem::class)->cannotBeEmpty()->end() + ->scalarNode('interface')->defaultValue(LineItemInterface::class)->cannotBeEmpty()->end() + ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end() + ->scalarNode('factory')->defaultValue(LineItemFactory::class)->cannotBeEmpty()->end() + ->scalarNode('repository')->cannotBeEmpty()->end() ->end() ->end() ->end() - ->arrayNode('tax_item') - ->addDefaultsIfNotSet() - ->children() - ->variableNode('options')->end() - ->arrayNode('classes') - ->addDefaultsIfNotSet() - ->children() - ->scalarNode('model')->defaultValue(TaxItem::class)->cannotBeEmpty()->end() - ->scalarNode('interface')->defaultValue(TaxItemInterface::class)->cannotBeEmpty()->end() - ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end() - ->scalarNode('factory')->defaultValue(TaxItemFactory::class)->cannotBeEmpty()->end() - ->scalarNode('repository')->cannotBeEmpty()->end() - ->end() + ->end() + ->arrayNode('tax_item') + ->addDefaultsIfNotSet() + ->children() + ->variableNode('options')->end() + ->arrayNode('classes') + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('model')->defaultValue(TaxItem::class)->cannotBeEmpty()->end() + ->scalarNode('interface')->defaultValue(TaxItemInterface::class)->cannotBeEmpty()->end() + ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end() + ->scalarNode('factory')->defaultValue(TaxItemFactory::class)->cannotBeEmpty()->end() + ->scalarNode('repository')->cannotBeEmpty()->end() ->end() ->end() ->end() - ->arrayNode('invoice_sequence') - ->addDefaultsIfNotSet() - ->children() - ->variableNode('options')->end() - ->arrayNode('classes') - ->addDefaultsIfNotSet() - ->children() - ->scalarNode('model')->defaultValue(InvoiceSequence::class)->cannotBeEmpty()->end() + ->end() + ->arrayNode('invoice_sequence') + ->addDefaultsIfNotSet() + ->children() + ->variableNode('options')->end() + ->arrayNode('classes') + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('model')->defaultValue(InvoiceSequence::class)->cannotBeEmpty()->end() ->scalarNode('interface')->defaultValue(InvoiceSequenceInterface::class)->cannotBeEmpty()->end() ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end() ->scalarNode('factory')->defaultValue(Factory::class)->cannotBeEmpty()->end() @@ -147,7 +156,22 @@ public function getConfigTreeBuilder(): TreeBuilder ->end() ->end() ; + } - return $treeBuilder; + private function addPdfGeneratorSection(ArrayNodeDefinition $node): void + { + $node + ->children() + ->arrayNode('pdf_generator') + ->addDefaultsIfNotSet() + ->children() + ->arrayNode('allowed_files') + ->useAttributeAsKey('name') + ->variablePrototype()->end() + ->end() + ->end() + ->end() + ->end() + ; } } diff --git a/src/DependencyInjection/SyliusInvoicingExtension.php b/src/DependencyInjection/SyliusInvoicingExtension.php index 56dc6846..0f95e355 100644 --- a/src/DependencyInjection/SyliusInvoicingExtension.php +++ b/src/DependencyInjection/SyliusInvoicingExtension.php @@ -32,6 +32,8 @@ public function load(array $config, ContainerBuilder $container): void $this->registerResources('sylius_invoicing_plugin', 'doctrine/orm', $config['resources'], $container); $loader->load('services.xml'); + + $container->setParameter('sylius_invoicing.pdf_generator.allowed_files', $config['pdf_generator']['allowed_files']); } public function prepend(ContainerBuilder $container): void diff --git a/src/Resources/config/config.yml b/src/Resources/config/config.yml index af85cd27..d20f1990 100644 --- a/src/Resources/config/config.yml +++ b/src/Resources/config/config.yml @@ -2,6 +2,11 @@ parameters: sylius_invoicing.invoice_save_path: "%kernel.project_dir%/private/invoices/" sylius_invoicing.filesystem_adapter.invoice: "sylius_invoicing_invoice" +sylius_invoicing: + pdf_generator: + allowed_files: + - '%env(default:default_logo_file:resolve:SYLIUS_INVOICING_LOGO_FILE)%' + sylius_mailer: emails: invoice_generated: diff --git a/tests/Unit/DependencyInjection/SyliusInvoicingConfigurationTest.php b/tests/Unit/DependencyInjection/SyliusInvoicingConfigurationTest.php new file mode 100644 index 00000000..5c48b473 --- /dev/null +++ b/tests/Unit/DependencyInjection/SyliusInvoicingConfigurationTest.php @@ -0,0 +1,48 @@ +assertProcessedConfigurationEquals( + [[]], + ['pdf_generator' => ['allowed_files' => []]], + 'pdf_generator' + ); + } + + /** @test */ + public function it_allows_to_define_allowed_files(): void + { + $this->assertProcessedConfigurationEquals( + [['pdf_generator' => ['allowed_files' => ['swans.png', 'product.png']]]], + ['pdf_generator' => ['allowed_files' => ['swans.png', 'product.png']]], + 'pdf_generator' + ); + } + + protected function getConfiguration(): Configuration + { + return new Configuration(); + } +} From 1eeda81ef2d53ddd78bc0710b41a1cc6c4083e70 Mon Sep 17 00:00:00 2001 From: Kevin Kaniaburka Date: Mon, 28 Mar 2022 23:06:32 +0200 Subject: [PATCH 3/7] Add TwigToPdfGenerator adapter --- .../Generator/InvoicePdfFileGeneratorSpec.php | 23 +++--- spec/Generator/TwigToPdfGeneratorSpec.php | 70 +++++++++++++++++++ src/Generator/InvoicePdfFileGenerator.php | 19 ++--- src/Generator/TwigToPdfGenerator.php | 48 +++++++++++++ src/Generator/TwigToPdfGeneratorInterface.php | 19 +++++ src/Resources/config/services/generators.xml | 10 ++- 6 files changed, 162 insertions(+), 27 deletions(-) create mode 100644 spec/Generator/TwigToPdfGeneratorSpec.php create mode 100644 src/Generator/TwigToPdfGenerator.php create mode 100644 src/Generator/TwigToPdfGeneratorInterface.php diff --git a/spec/Generator/InvoicePdfFileGeneratorSpec.php b/spec/Generator/InvoicePdfFileGeneratorSpec.php index 1da9e4a0..0fa695f8 100644 --- a/spec/Generator/InvoicePdfFileGeneratorSpec.php +++ b/spec/Generator/InvoicePdfFileGeneratorSpec.php @@ -13,27 +13,24 @@ namespace spec\Sylius\InvoicingPlugin\Generator; -use Knp\Snappy\GeneratorInterface; use PhpSpec\ObjectBehavior; use Sylius\Component\Core\Model\ChannelInterface; use Sylius\InvoicingPlugin\Entity\InvoiceInterface; use Sylius\InvoicingPlugin\Generator\InvoiceFileNameGeneratorInterface; use Sylius\InvoicingPlugin\Generator\InvoicePdfFileGeneratorInterface; +use Sylius\InvoicingPlugin\Generator\TwigToPdfGeneratorInterface; use Sylius\InvoicingPlugin\Model\InvoicePdf; use Symfony\Component\Config\FileLocatorInterface; -use Twig\Environment; final class InvoicePdfFileGeneratorSpec extends ObjectBehavior { function let( - Environment $twig, - GeneratorInterface $pdfGenerator, + TwigToPdfGeneratorInterface $twigToPdfGenerator, FileLocatorInterface $fileLocator, InvoiceFileNameGeneratorInterface $invoiceFileNameGenerator ): void { $this->beConstructedWith( - $twig, - $pdfGenerator, + $twigToPdfGenerator, $fileLocator, $invoiceFileNameGenerator, 'invoiceTemplate.html.twig', @@ -48,8 +45,7 @@ function it_implements_invoice_pdf_file_generator_interface(): void function it_creates_invoice_pdf_with_generated_content_and_filename_basing_on_invoice_number( FileLocatorInterface $fileLocator, - Environment $twig, - GeneratorInterface $pdfGenerator, + TwigToPdfGeneratorInterface $twigToPdfGenerator, InvoiceFileNameGeneratorInterface $invoiceFileNameGenerator, InvoiceInterface $invoice, ChannelInterface $channel @@ -59,13 +55,14 @@ function it_creates_invoice_pdf_with_generated_content_and_filename_basing_on_in $fileLocator->locate('@SyliusInvoicingPlugin/Resources/assets/sylius-logo.png')->willReturn('located-path/sylius-logo.png'); - $twig - ->render('invoiceTemplate.html.twig', ['invoice' => $invoice, 'channel' => $channel, 'invoiceLogoPath' => 'located-path/sylius-logo.png']) - ->willReturn('I am an invoice pdf file content') + $twigToPdfGenerator + ->generate( + 'invoiceTemplate.html.twig', + ['invoice' => $invoice, 'channel' => $channel, 'invoiceLogoPath' => 'located-path/sylius-logo.png'] + ) + ->willReturn('PDF FILE') ; - $pdfGenerator->getOutputFromHtml('I am an invoice pdf file content')->willReturn('PDF FILE'); - $this->generate($invoice)->shouldBeLike(new InvoicePdf('2015_05_00004444.pdf', 'PDF FILE')); } } diff --git a/spec/Generator/TwigToPdfGeneratorSpec.php b/spec/Generator/TwigToPdfGeneratorSpec.php new file mode 100644 index 00000000..52efaeba --- /dev/null +++ b/spec/Generator/TwigToPdfGeneratorSpec.php @@ -0,0 +1,70 @@ +beConstructedWith( + $twig, + $pdfGenerator, + $fileLocator, + ['swans.png'] + ); + } + + function it_is_twig_to_pdf_generator_interface(): void + { + $this->shouldImplement(TwigToPdfGeneratorInterface::class); + } + + function it_generates_pdf_from_twig_template( + Environment $twig, + GeneratorInterface $pdfGenerator, + FileLocatorInterface $fileLocator + ): void { + $twig + ->render('template.html.twig', ['figcaption' => 'Swans', 'imgPath' => 'located-path/swans.png']) + ->willReturn('I am a pdf file generated from twig template') + ; + + $pdfGenerator + ->getOutputFromHtml( + 'I am a pdf file generated from twig template', + ['allow' => ['located-path/swans.png']] + ) + ->willReturn('PDF FILE') + ; + + $fileLocator + ->locate('swans.png') + ->willReturn('located-path/swans.png') + ; + + $this + ->generate('template.html.twig', ['figcaption' => 'Swans', 'imgPath' => 'located-path/swans.png']) + ->shouldBeLike('PDF FILE') + ; + } +} diff --git a/src/Generator/InvoicePdfFileGenerator.php b/src/Generator/InvoicePdfFileGenerator.php index a828e0ee..e48cf80f 100644 --- a/src/Generator/InvoicePdfFileGenerator.php +++ b/src/Generator/InvoicePdfFileGenerator.php @@ -13,17 +13,13 @@ namespace Sylius\InvoicingPlugin\Generator; -use Knp\Snappy\GeneratorInterface; use Sylius\InvoicingPlugin\Entity\InvoiceInterface; use Sylius\InvoicingPlugin\Model\InvoicePdf; use Symfony\Component\Config\FileLocatorInterface; -use Twig\Environment; final class InvoicePdfFileGenerator implements InvoicePdfFileGeneratorInterface { - private Environment $templatingEngine; - - private GeneratorInterface $pdfGenerator; + private TwigToPdfGeneratorInterface $twigToPdfGenerator; private FileLocatorInterface $fileLocator; @@ -34,15 +30,13 @@ final class InvoicePdfFileGenerator implements InvoicePdfFileGeneratorInterface private string $invoiceLogoPath; public function __construct( - Environment $templatingEngine, - GeneratorInterface $pdfGenerator, + TwigToPdfGeneratorInterface $twigToPdfGenerator, FileLocatorInterface $fileLocator, InvoiceFileNameGeneratorInterface $invoiceFileNameGenerator, string $template, string $invoiceLogoPath ) { - $this->templatingEngine = $templatingEngine; - $this->pdfGenerator = $pdfGenerator; + $this->twigToPdfGenerator = $twigToPdfGenerator; $this->fileLocator = $fileLocator; $this->invoiceFileNameGenerator = $invoiceFileNameGenerator; $this->template = $template; @@ -53,12 +47,13 @@ public function generate(InvoiceInterface $invoice): InvoicePdf { $filename = $this->invoiceFileNameGenerator->generateForPdf($invoice); - $pdf = $this->pdfGenerator->getOutputFromHtml( - $this->templatingEngine->render($this->template, [ + $pdf = $this->twigToPdfGenerator->generate( + $this->template, + [ 'invoice' => $invoice, 'channel' => $invoice->channel(), 'invoiceLogoPath' => $this->fileLocator->locate($this->invoiceLogoPath), - ]) + ] ); return new InvoicePdf($filename, $pdf); diff --git a/src/Generator/TwigToPdfGenerator.php b/src/Generator/TwigToPdfGenerator.php new file mode 100644 index 00000000..7f1733dc --- /dev/null +++ b/src/Generator/TwigToPdfGenerator.php @@ -0,0 +1,48 @@ +pdfGenerator->getOutputFromHtml( + $this->twig->render($templateName, $templateParams), + $this->getOptions() + ); + } + + private function getOptions(): array + { + if (empty($this->allowedFiles)) { + return []; + } + + return [ + 'allow' => array_map(fn ($file) => $this->fileLocator->locate($file), $this->allowedFiles), + ]; + } +} diff --git a/src/Generator/TwigToPdfGeneratorInterface.php b/src/Generator/TwigToPdfGeneratorInterface.php new file mode 100644 index 00000000..7797943d --- /dev/null +++ b/src/Generator/TwigToPdfGeneratorInterface.php @@ -0,0 +1,19 @@ + - - + @SyliusInvoicingPlugin/Invoice/Download/pdf.html.twig @@ -50,5 +49,12 @@ + + + + + + %sylius_invoicing.pdf_generator.allowed_files% + From 0b0d54805dcc3f36cb72eb6e3a6981e66418c297 Mon Sep 17 00:00:00 2001 From: Kevin Kaniaburka Date: Wed, 30 Mar 2022 14:10:18 +0200 Subject: [PATCH 4/7] Update UPGRADE.md --- UPGRADE.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/UPGRADE.md b/UPGRADE.md index 3930e5c9..c725a7db 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,3 +1,7 @@ +### UPGRADE FROM 0.20.0 TO 0.21.0 + +1. `Sylius\InvoicingPlugin\Generator\InvoicePdfFileGenerator` takes `Sylius\RefundPlugin\Generator\TwigToPdfGeneratorInterface` in place of `Twig\Environment` and `Knp\Snappy\GeneratorInterface`. + ### UPGRADE FROM 0.19.0 TO 0.20.0 1. Since 0.20.0, the recommended Sylius version to use with InvoicingPlugin is `1.11.*`. If you would like to upgrade Sylius to v1.11.0, From b640c6b3bd7779a6810197e07ab516204fdb7058 Mon Sep 17 00:00:00 2001 From: Kevin Kaniaburka Date: Wed, 30 Mar 2022 14:42:04 +0200 Subject: [PATCH 5/7] Extend KnpSnappy pdf options instead of overriding them --- spec/Generator/TwigToPdfGeneratorSpec.php | 3 ++- src/Generator/TwigToPdfGenerator.php | 20 ++++++++++++++++---- src/Resources/config/services/generators.xml | 1 + 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/spec/Generator/TwigToPdfGeneratorSpec.php b/spec/Generator/TwigToPdfGeneratorSpec.php index 52efaeba..0cc42c3e 100644 --- a/spec/Generator/TwigToPdfGeneratorSpec.php +++ b/spec/Generator/TwigToPdfGeneratorSpec.php @@ -30,6 +30,7 @@ function let( $twig, $pdfGenerator, $fileLocator, + ['allow' => 'allowed_file_in_knp_snappy_config.png'], ['swans.png'] ); } @@ -52,7 +53,7 @@ function it_generates_pdf_from_twig_template( $pdfGenerator ->getOutputFromHtml( 'I am a pdf file generated from twig template', - ['allow' => ['located-path/swans.png']] + ['allow' => ['allowed_file_in_knp_snappy_config.png', 'located-path/swans.png']] ) ->willReturn('PDF FILE') ; diff --git a/src/Generator/TwigToPdfGenerator.php b/src/Generator/TwigToPdfGenerator.php index 7f1733dc..fbab2a12 100644 --- a/src/Generator/TwigToPdfGenerator.php +++ b/src/Generator/TwigToPdfGenerator.php @@ -23,6 +23,7 @@ public function __construct( private Environment $twig, private GeneratorInterface $pdfGenerator, private FileLocatorInterface $fileLocator, + private array $knpSnappyOptions, private array $allowedFiles ) { } @@ -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; } } diff --git a/src/Resources/config/services/generators.xml b/src/Resources/config/services/generators.xml index 26dbe4ae..8420175d 100644 --- a/src/Resources/config/services/generators.xml +++ b/src/Resources/config/services/generators.xml @@ -54,6 +54,7 @@ + %knp_snappy.pdf.options% %sylius_invoicing.pdf_generator.allowed_files% From 47b8a1cbfd6410e7903ae25f686c0302d3270b70 Mon Sep 17 00:00:00 2001 From: Kevin Kaniaburka Date: Thu, 31 Mar 2022 11:44:18 +0200 Subject: [PATCH 6/7] Add PdfOptionsGenerator in place of TwigToPdfGenerator --- .../Generator/InvoicePdfFileGeneratorSpec.php | 38 +++++++--- spec/Generator/PdfOptionsGeneratorSpec.php | 52 ++++++++++++++ spec/Generator/TwigToPdfGeneratorSpec.php | 71 ------------------- src/Generator/InvoicePdfFileGenerator.php | 37 ++++------ ...fGenerator.php => PdfOptionsGenerator.php} | 16 +---- ...e.php => PdfOptionsGeneratorInterface.php} | 4 +- src/Resources/config/services/generators.xml | 8 +-- 7 files changed, 101 insertions(+), 125 deletions(-) create mode 100644 spec/Generator/PdfOptionsGeneratorSpec.php delete mode 100644 spec/Generator/TwigToPdfGeneratorSpec.php rename src/Generator/{TwigToPdfGenerator.php => PdfOptionsGenerator.php} (67%) rename src/Generator/{TwigToPdfGeneratorInterface.php => PdfOptionsGeneratorInterface.php} (70%) diff --git a/spec/Generator/InvoicePdfFileGeneratorSpec.php b/spec/Generator/InvoicePdfFileGeneratorSpec.php index 0fa695f8..1018c9bf 100644 --- a/spec/Generator/InvoicePdfFileGeneratorSpec.php +++ b/spec/Generator/InvoicePdfFileGeneratorSpec.php @@ -13,24 +13,30 @@ namespace spec\Sylius\InvoicingPlugin\Generator; +use Knp\Snappy\GeneratorInterface; use PhpSpec\ObjectBehavior; use Sylius\Component\Core\Model\ChannelInterface; use Sylius\InvoicingPlugin\Entity\InvoiceInterface; use Sylius\InvoicingPlugin\Generator\InvoiceFileNameGeneratorInterface; use Sylius\InvoicingPlugin\Generator\InvoicePdfFileGeneratorInterface; -use Sylius\InvoicingPlugin\Generator\TwigToPdfGeneratorInterface; +use Sylius\InvoicingPlugin\Generator\PdfOptionsGeneratorInterface; use Sylius\InvoicingPlugin\Model\InvoicePdf; use Symfony\Component\Config\FileLocatorInterface; +use Twig\Environment; final class InvoicePdfFileGeneratorSpec extends ObjectBehavior { function let( - TwigToPdfGeneratorInterface $twigToPdfGenerator, + Environment $twig, + GeneratorInterface $pdfGenerator, + PdfOptionsGeneratorInterface $pdfOptionsGenerator, FileLocatorInterface $fileLocator, InvoiceFileNameGeneratorInterface $invoiceFileNameGenerator ): void { $this->beConstructedWith( - $twigToPdfGenerator, + $twig, + $pdfGenerator, + $pdfOptionsGenerator, $fileLocator, $invoiceFileNameGenerator, 'invoiceTemplate.html.twig', @@ -45,7 +51,9 @@ function it_implements_invoice_pdf_file_generator_interface(): void function it_creates_invoice_pdf_with_generated_content_and_filename_basing_on_invoice_number( FileLocatorInterface $fileLocator, - TwigToPdfGeneratorInterface $twigToPdfGenerator, + Environment $twig, + GeneratorInterface $pdfGenerator, + PdfOptionsGeneratorInterface $pdfOptionsGenerator, InvoiceFileNameGeneratorInterface $invoiceFileNameGenerator, InvoiceInterface $invoice, ChannelInterface $channel @@ -55,14 +63,24 @@ function it_creates_invoice_pdf_with_generated_content_and_filename_basing_on_in $fileLocator->locate('@SyliusInvoicingPlugin/Resources/assets/sylius-logo.png')->willReturn('located-path/sylius-logo.png'); - $twigToPdfGenerator - ->generate( - 'invoiceTemplate.html.twig', - ['invoice' => $invoice, 'channel' => $channel, 'invoiceLogoPath' => 'located-path/sylius-logo.png'] - ) + $twig + ->render('invoiceTemplate.html.twig', ['invoice' => $invoice, 'channel' => $channel, 'invoiceLogoPath' => 'located-path/sylius-logo.png']) + ->willReturn('I am an invoice pdf file content') + ; + + $pdfOptionsGenerator + ->generate() + ->willReturn(['allow' => ['located-path/sylius-logo.png']]) + ; + + $pdfGenerator + ->getOutputFromHtml('I am an invoice pdf file content', ['allow' => ['located-path/sylius-logo.png']]) ->willReturn('PDF FILE') ; - $this->generate($invoice)->shouldBeLike(new InvoicePdf('2015_05_00004444.pdf', 'PDF FILE')); + $this + ->generate($invoice) + ->shouldBeLike(new InvoicePdf('2015_05_00004444.pdf', 'PDF FILE')) + ; } } diff --git a/spec/Generator/PdfOptionsGeneratorSpec.php b/spec/Generator/PdfOptionsGeneratorSpec.php new file mode 100644 index 00000000..27cec88d --- /dev/null +++ b/spec/Generator/PdfOptionsGeneratorSpec.php @@ -0,0 +1,52 @@ +beConstructedWith( + $fileLocator, + ['allow' => 'allowed_file_in_knp_snappy_config.png'], + ['swans.png'] + ); + } + + function it_is_pdf_options_generator_interface(): void + { + $this->shouldImplement(PdfOptionsGeneratorInterface::class); + } + + function it_generates_pdf_options(FileLocatorInterface $fileLocator): void + { + $fileLocator + ->locate('swans.png') + ->willReturn('located-path/swans.png'); + + $this + ->generate() + ->shouldBeLike([ + 'allow' => [ + 'allowed_file_in_knp_snappy_config.png', + 'located-path/swans.png', + ], + ]) + ; + } +} diff --git a/spec/Generator/TwigToPdfGeneratorSpec.php b/spec/Generator/TwigToPdfGeneratorSpec.php deleted file mode 100644 index 0cc42c3e..00000000 --- a/spec/Generator/TwigToPdfGeneratorSpec.php +++ /dev/null @@ -1,71 +0,0 @@ -beConstructedWith( - $twig, - $pdfGenerator, - $fileLocator, - ['allow' => 'allowed_file_in_knp_snappy_config.png'], - ['swans.png'] - ); - } - - function it_is_twig_to_pdf_generator_interface(): void - { - $this->shouldImplement(TwigToPdfGeneratorInterface::class); - } - - function it_generates_pdf_from_twig_template( - Environment $twig, - GeneratorInterface $pdfGenerator, - FileLocatorInterface $fileLocator - ): void { - $twig - ->render('template.html.twig', ['figcaption' => 'Swans', 'imgPath' => 'located-path/swans.png']) - ->willReturn('I am a pdf file generated from twig template') - ; - - $pdfGenerator - ->getOutputFromHtml( - 'I am a pdf file generated from twig template', - ['allow' => ['allowed_file_in_knp_snappy_config.png', 'located-path/swans.png']] - ) - ->willReturn('PDF FILE') - ; - - $fileLocator - ->locate('swans.png') - ->willReturn('located-path/swans.png') - ; - - $this - ->generate('template.html.twig', ['figcaption' => 'Swans', 'imgPath' => 'located-path/swans.png']) - ->shouldBeLike('PDF FILE') - ; - } -} diff --git a/src/Generator/InvoicePdfFileGenerator.php b/src/Generator/InvoicePdfFileGenerator.php index e48cf80f..17fca564 100644 --- a/src/Generator/InvoicePdfFileGenerator.php +++ b/src/Generator/InvoicePdfFileGenerator.php @@ -13,47 +13,36 @@ namespace Sylius\InvoicingPlugin\Generator; +use Knp\Snappy\GeneratorInterface; use Sylius\InvoicingPlugin\Entity\InvoiceInterface; use Sylius\InvoicingPlugin\Model\InvoicePdf; use Symfony\Component\Config\FileLocatorInterface; +use Twig\Environment; final class InvoicePdfFileGenerator implements InvoicePdfFileGeneratorInterface { - private TwigToPdfGeneratorInterface $twigToPdfGenerator; - - private FileLocatorInterface $fileLocator; - - private InvoiceFileNameGeneratorInterface $invoiceFileNameGenerator; - - private string $template; - - private string $invoiceLogoPath; - public function __construct( - TwigToPdfGeneratorInterface $twigToPdfGenerator, - FileLocatorInterface $fileLocator, - InvoiceFileNameGeneratorInterface $invoiceFileNameGenerator, - string $template, - string $invoiceLogoPath + private Environment $templatingEngine, + private GeneratorInterface $pdfGenerator, + private PdfOptionsGeneratorInterface $pdfOptionsGenerator, + private FileLocatorInterface $fileLocator, + private InvoiceFileNameGeneratorInterface $invoiceFileNameGenerator, + private string $template, + private string $invoiceLogoPath ) { - $this->twigToPdfGenerator = $twigToPdfGenerator; - $this->fileLocator = $fileLocator; - $this->invoiceFileNameGenerator = $invoiceFileNameGenerator; - $this->template = $template; - $this->invoiceLogoPath = $invoiceLogoPath; } public function generate(InvoiceInterface $invoice): InvoicePdf { $filename = $this->invoiceFileNameGenerator->generateForPdf($invoice); - $pdf = $this->twigToPdfGenerator->generate( - $this->template, - [ + $pdf = $this->pdfGenerator->getOutputFromHtml( + $this->templatingEngine->render($this->template, [ 'invoice' => $invoice, 'channel' => $invoice->channel(), 'invoiceLogoPath' => $this->fileLocator->locate($this->invoiceLogoPath), - ] + ]), + $this->pdfOptionsGenerator->generate() ); return new InvoicePdf($filename, $pdf); diff --git a/src/Generator/TwigToPdfGenerator.php b/src/Generator/PdfOptionsGenerator.php similarity index 67% rename from src/Generator/TwigToPdfGenerator.php rename to src/Generator/PdfOptionsGenerator.php index fbab2a12..ed20ffc6 100644 --- a/src/Generator/TwigToPdfGenerator.php +++ b/src/Generator/PdfOptionsGenerator.php @@ -13,30 +13,18 @@ namespace Sylius\InvoicingPlugin\Generator; -use Knp\Snappy\GeneratorInterface; use Symfony\Component\Config\FileLocatorInterface; -use Twig\Environment; -final class TwigToPdfGenerator implements TwigToPdfGeneratorInterface +final class PdfOptionsGenerator implements PdfOptionsGeneratorInterface { public function __construct( - private Environment $twig, - private GeneratorInterface $pdfGenerator, private FileLocatorInterface $fileLocator, private array $knpSnappyOptions, private array $allowedFiles ) { } - public function generate(string $templateName, array $templateParams): string - { - return $this->pdfGenerator->getOutputFromHtml( - $this->twig->render($templateName, $templateParams), - $this->getOptions() - ); - } - - private function getOptions(): array + public function generate(): array { $options = $this->knpSnappyOptions; diff --git a/src/Generator/TwigToPdfGeneratorInterface.php b/src/Generator/PdfOptionsGeneratorInterface.php similarity index 70% rename from src/Generator/TwigToPdfGeneratorInterface.php rename to src/Generator/PdfOptionsGeneratorInterface.php index 7797943d..f88b494b 100644 --- a/src/Generator/TwigToPdfGeneratorInterface.php +++ b/src/Generator/PdfOptionsGeneratorInterface.php @@ -13,7 +13,7 @@ namespace Sylius\InvoicingPlugin\Generator; -interface TwigToPdfGeneratorInterface +interface PdfOptionsGeneratorInterface { - public function generate(string $templateName, array $templateParams): string; + public function generate(): array; } diff --git a/src/Resources/config/services/generators.xml b/src/Resources/config/services/generators.xml index 8420175d..2ef5b78f 100644 --- a/src/Resources/config/services/generators.xml +++ b/src/Resources/config/services/generators.xml @@ -30,7 +30,9 @@ /> - + + + @SyliusInvoicingPlugin/Invoice/Download/pdf.html.twig @@ -50,9 +52,7 @@ - - - + %knp_snappy.pdf.options% %sylius_invoicing.pdf_generator.allowed_files% From 264b90965a95577f284a7b3ce333d7d3e3200c12 Mon Sep 17 00:00:00 2001 From: Kevin Kaniaburka Date: Thu, 31 Mar 2022 12:04:17 +0200 Subject: [PATCH 7/7] Update UPGRADE.md --- UPGRADE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UPGRADE.md b/UPGRADE.md index c725a7db..72ce1ff0 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,6 +1,6 @@ ### UPGRADE FROM 0.20.0 TO 0.21.0 -1. `Sylius\InvoicingPlugin\Generator\InvoicePdfFileGenerator` takes `Sylius\RefundPlugin\Generator\TwigToPdfGeneratorInterface` in place of `Twig\Environment` and `Knp\Snappy\GeneratorInterface`. +1. `Sylius\InvoicingPlugin\Generator\InvoicePdfFileGenerator` takes `Sylius\InvoicingPlugin\Generator\PdfOptionsGeneratorInterface` as the third argument after `Knp\Snappy\GeneratorInterface`. ### UPGRADE FROM 0.19.0 TO 0.20.0