Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d66bfb

Browse files
xabbuhfabpot
authored andcommitted
add option for configuring custom HTML to text converter services
1 parent 5f55af8 commit 4d66bfb

File tree

8 files changed

+82
-4
lines changed

8 files changed

+82
-4
lines changed

src/Symfony/Bundle/TwigBundle/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
6.2
5+
---
6+
7+
* Add the `twig.mailer.html_to_text_converter` option to allow configuring custom `HtmlToTextConverterInterface`
8+
implementations to be used by the `twig.mime_body_renderer` service
9+
410
6.1
511
---
612

src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php

+18
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
1616
use Symfony\Component\Config\Definition\ConfigurationInterface;
1717
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
18+
use Symfony\Component\Mime\HtmlToTextConverter\HtmlToTextConverterInterface;
1819

1920
/**
2021
* TwigExtension configuration structure.
@@ -48,6 +49,7 @@ public function getConfigTreeBuilder(): TreeBuilder
4849
$this->addGlobalsSection($rootNode);
4950
$this->addTwigOptions($rootNode);
5051
$this->addTwigFormatOptions($rootNode);
52+
$this->addMailerSection($rootNode);
5153

5254
return $treeBuilder;
5355
}
@@ -213,4 +215,20 @@ private function addTwigFormatOptions(ArrayNodeDefinition $rootNode)
213215
->end()
214216
;
215217
}
218+
219+
private function addMailerSection(ArrayNodeDefinition $rootNode)
220+
{
221+
$rootNode
222+
->children()
223+
->arrayNode('mailer')
224+
->children()
225+
->scalarNode('html_to_text_converter')
226+
->info(sprintf('A service implementing the "%s"', HtmlToTextConverterInterface::class))
227+
->defaultNull()
228+
->end()
229+
->end()
230+
->end()
231+
->end()
232+
;
233+
}
216234
}

src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ public function load(array $configs, ContainerBuilder $container)
5454
$loader->load('console.php');
5555
}
5656

57-
if ($container::willBeAvailable('symfony/mailer', Mailer::class, ['symfony/twig-bundle'])) {
58-
$loader->load('mailer.php');
59-
}
60-
6157
if (!$container::willBeAvailable('symfony/translation', Translator::class, ['symfony/twig-bundle'])) {
6258
$container->removeDefinition('twig.translation.extractor');
6359
}
@@ -79,6 +75,14 @@ public function load(array $configs, ContainerBuilder $container)
7975

8076
$config = $this->processConfiguration($configuration, $configs);
8177

78+
if ($container::willBeAvailable('symfony/mailer', Mailer::class, ['symfony/twig-bundle'])) {
79+
$loader->load('mailer.php');
80+
81+
if ($htmlToTextConverter = $config['mailer']['html_to_text_converter'] ?? null) {
82+
$container->getDefinition('twig.mime_body_renderer')->setArgument('$converter', new Reference($htmlToTextConverter));
83+
}
84+
}
85+
8286
$container->setParameter('twig.form.resources', $config['form_themes']);
8387
$container->setParameter('twig.default_path', $config['default_path']);
8488
$defaultTwigPath = $container->getParameterBag()->resolveValue($config['default_path']);

src/Symfony/Bundle/TwigBundle/Resources/config/schema/twig-1.0.xsd

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<xsd:element name="form-theme" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
1515
<xsd:element name="global" type="global" minOccurs="0" maxOccurs="unbounded" />
1616
<xsd:element name="path" type="path" minOccurs="0" maxOccurs="unbounded" />
17+
<xsd:element name="mailer" type="mailer" minOccurs="0" maxOccurs="1" />
1718
</xsd:sequence>
1819

1920
<xsd:attribute name="auto-reload" type="xsd:string" />
@@ -55,4 +56,8 @@
5556
<xsd:enumeration value="service" />
5657
</xsd:restriction>
5758
</xsd:simpleType>
59+
60+
<xsd:complexType name="mailer">
61+
<xsd:attribute name="html-to-text-converter" type="xsd:string" />
62+
</xsd:complexType>
5863
</xsd:schema>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
$container->loadFromExtension('twig', [
4+
'mailer' => [
5+
'html_to_text_converter' => 'my_converter',
6+
],
7+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:twig="http://symfony.com/schema/dic/twig"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
6+
http://symfony.com/schema/dic/twig https://symfony.com/schema/dic/twig/twig-1.0.xsd">
7+
8+
<twig:config>
9+
<twig:mailer html-to-text-converter="my_converter" />
10+
</twig:config>
11+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
twig:
2+
mailer:
3+
html_to_text_converter: 'my_converter'

src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2525
use Symfony\Component\DependencyInjection\Reference;
2626
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
27+
use Symfony\Component\Mailer\Mailer;
2728

2829
class TwigExtensionTest extends TestCase
2930
{
@@ -43,6 +44,10 @@ public function testLoadEmptyConfiguration()
4344
$this->assertEquals('%kernel.cache_dir%/twig', $options['cache'], '->load() sets default value for cache option');
4445
$this->assertEquals('%kernel.charset%', $options['charset'], '->load() sets default value for charset option');
4546
$this->assertEquals('%kernel.debug%', $options['debug'], '->load() sets default value for debug option');
47+
48+
if (class_exists(Mailer::class)) {
49+
$this->assertCount(1, $container->getDefinition('twig.mime_body_renderer')->getArguments());
50+
}
4651
}
4752

4853
/**
@@ -263,6 +268,25 @@ public function testRuntimeLoader()
263268
$this->assertEquals('foo', $args['FooClass']->getValues()[0]);
264269
}
265270

271+
/**
272+
* @dataProvider getFormats
273+
*/
274+
public function testCustomHtmlToTextConverterService(string $format)
275+
{
276+
if (!class_exists(Mailer::class)) {
277+
$this->markTestSkipped('The "twig.mime_body_renderer" service requires the Mailer component');
278+
}
279+
280+
$container = $this->createContainer();
281+
$container->registerExtension(new TwigExtension());
282+
$this->loadFromFile($container, 'mailer', $format);
283+
$this->compileContainer($container);
284+
285+
$bodyRenderer = $container->getDefinition('twig.mime_body_renderer');
286+
$this->assertCount(2, $bodyRenderer->getArguments());
287+
$this->assertEquals(new Reference('my_converter'), $bodyRenderer->getArgument('$converter'));
288+
}
289+
266290
private function createContainer()
267291
{
268292
$container = new ContainerBuilder(new ParameterBag([

0 commit comments

Comments
 (0)