From 4d66bfb511f2aabba8b69a88ed25ad5595e50dc4 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sat, 10 Sep 2022 11:39:30 +0300 Subject: [PATCH] add option for configuring custom HTML to text converter services --- src/Symfony/Bundle/TwigBundle/CHANGELOG.md | 6 +++++ .../DependencyInjection/Configuration.php | 18 ++++++++++++++ .../DependencyInjection/TwigExtension.php | 12 ++++++---- .../Resources/config/schema/twig-1.0.xsd | 5 ++++ .../Fixtures/php/mailer.php | 7 ++++++ .../Fixtures/xml/mailer.xml | 11 +++++++++ .../Fixtures/yml/mailer.yml | 3 +++ .../DependencyInjection/TwigExtensionTest.php | 24 +++++++++++++++++++ 8 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/php/mailer.php create mode 100644 src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/xml/mailer.xml create mode 100644 src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/yml/mailer.yml diff --git a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md index aaa0ae400f7a1..5502812c27ec1 100644 --- a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG ========= +6.2 +--- + + * Add the `twig.mailer.html_to_text_converter` option to allow configuring custom `HtmlToTextConverterInterface` + implementations to be used by the `twig.mime_body_renderer` service + 6.1 --- diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php index b85a3bf4b1958..0655a51e7c159 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php @@ -15,6 +15,7 @@ use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; +use Symfony\Component\Mime\HtmlToTextConverter\HtmlToTextConverterInterface; /** * TwigExtension configuration structure. @@ -48,6 +49,7 @@ public function getConfigTreeBuilder(): TreeBuilder $this->addGlobalsSection($rootNode); $this->addTwigOptions($rootNode); $this->addTwigFormatOptions($rootNode); + $this->addMailerSection($rootNode); return $treeBuilder; } @@ -213,4 +215,20 @@ private function addTwigFormatOptions(ArrayNodeDefinition $rootNode) ->end() ; } + + private function addMailerSection(ArrayNodeDefinition $rootNode) + { + $rootNode + ->children() + ->arrayNode('mailer') + ->children() + ->scalarNode('html_to_text_converter') + ->info(sprintf('A service implementing the "%s"', HtmlToTextConverterInterface::class)) + ->defaultNull() + ->end() + ->end() + ->end() + ->end() + ; + } } diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php index 607136872a849..b323bf808946e 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php @@ -54,10 +54,6 @@ public function load(array $configs, ContainerBuilder $container) $loader->load('console.php'); } - if ($container::willBeAvailable('symfony/mailer', Mailer::class, ['symfony/twig-bundle'])) { - $loader->load('mailer.php'); - } - if (!$container::willBeAvailable('symfony/translation', Translator::class, ['symfony/twig-bundle'])) { $container->removeDefinition('twig.translation.extractor'); } @@ -79,6 +75,14 @@ public function load(array $configs, ContainerBuilder $container) $config = $this->processConfiguration($configuration, $configs); + if ($container::willBeAvailable('symfony/mailer', Mailer::class, ['symfony/twig-bundle'])) { + $loader->load('mailer.php'); + + if ($htmlToTextConverter = $config['mailer']['html_to_text_converter'] ?? null) { + $container->getDefinition('twig.mime_body_renderer')->setArgument('$converter', new Reference($htmlToTextConverter)); + } + } + $container->setParameter('twig.form.resources', $config['form_themes']); $container->setParameter('twig.default_path', $config['default_path']); $defaultTwigPath = $container->getParameterBag()->resolveValue($config['default_path']); diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/schema/twig-1.0.xsd b/src/Symfony/Bundle/TwigBundle/Resources/config/schema/twig-1.0.xsd index 429c91db67b74..50eff2bc29923 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/schema/twig-1.0.xsd +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/schema/twig-1.0.xsd @@ -14,6 +14,7 @@ + @@ -55,4 +56,8 @@ + + + + diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/php/mailer.php b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/php/mailer.php new file mode 100644 index 0000000000000..2404f221ad680 --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/php/mailer.php @@ -0,0 +1,7 @@ +loadFromExtension('twig', [ + 'mailer' => [ + 'html_to_text_converter' => 'my_converter', + ], +]); diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/xml/mailer.xml b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/xml/mailer.xml new file mode 100644 index 0000000000000..25c2a9453663e --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/xml/mailer.xml @@ -0,0 +1,11 @@ + + + + + + + diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/yml/mailer.yml b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/yml/mailer.yml new file mode 100644 index 0000000000000..139e2077280ba --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/yml/mailer.yml @@ -0,0 +1,3 @@ +twig: + mailer: + html_to_text_converter: 'my_converter' diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php index cd60cbb7fd9e3..bfb215b488715 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php @@ -24,6 +24,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer; +use Symfony\Component\Mailer\Mailer; class TwigExtensionTest extends TestCase { @@ -43,6 +44,10 @@ public function testLoadEmptyConfiguration() $this->assertEquals('%kernel.cache_dir%/twig', $options['cache'], '->load() sets default value for cache option'); $this->assertEquals('%kernel.charset%', $options['charset'], '->load() sets default value for charset option'); $this->assertEquals('%kernel.debug%', $options['debug'], '->load() sets default value for debug option'); + + if (class_exists(Mailer::class)) { + $this->assertCount(1, $container->getDefinition('twig.mime_body_renderer')->getArguments()); + } } /** @@ -263,6 +268,25 @@ public function testRuntimeLoader() $this->assertEquals('foo', $args['FooClass']->getValues()[0]); } + /** + * @dataProvider getFormats + */ + public function testCustomHtmlToTextConverterService(string $format) + { + if (!class_exists(Mailer::class)) { + $this->markTestSkipped('The "twig.mime_body_renderer" service requires the Mailer component'); + } + + $container = $this->createContainer(); + $container->registerExtension(new TwigExtension()); + $this->loadFromFile($container, 'mailer', $format); + $this->compileContainer($container); + + $bodyRenderer = $container->getDefinition('twig.mime_body_renderer'); + $this->assertCount(2, $bodyRenderer->getArguments()); + $this->assertEquals(new Reference('my_converter'), $bodyRenderer->getArgument('$converter')); + } + private function createContainer() { $container = new ContainerBuilder(new ParameterBag([