Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Symfony/Bundle/TwigBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -48,6 +49,7 @@ public function getConfigTreeBuilder(): TreeBuilder
$this->addGlobalsSection($rootNode);
$this->addTwigOptions($rootNode);
$this->addTwigFormatOptions($rootNode);
$this->addMailerSection($rootNode);

return $treeBuilder;
}
Expand Down Expand Up @@ -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()
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<xsd:element name="form-theme" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="global" type="global" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="path" type="path" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="mailer" type="mailer" minOccurs="0" maxOccurs="1" />
</xsd:sequence>

<xsd:attribute name="auto-reload" type="xsd:string" />
Expand Down Expand Up @@ -55,4 +56,8 @@
<xsd:enumeration value="service" />
</xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="mailer">
<xsd:attribute name="html-to-text-converter" type="xsd:string" />
</xsd:complexType>
</xsd:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$container->loadFromExtension('twig', [
'mailer' => [
'html_to_text_converter' => 'my_converter',
],
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:twig="http://symfony.com/schema/dic/twig"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/twig https://symfony.com/schema/dic/twig/twig-1.0.xsd">

<twig:config>
<twig:mailer html-to-text-converter="my_converter" />
</twig:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
twig:
mailer:
html_to_text_converter: 'my_converter'
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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());
}
}

/**
Expand Down Expand Up @@ -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([
Expand Down