Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit932a1e8

Browse files
committed
add option for configuring custom HTML to text converter services
1 parent5f55af8 commit932a1e8

File tree

8 files changed

+82
-4
lines changed

8 files changed

+82
-4
lines changed

‎src/Symfony/Bundle/TwigBundle/CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
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‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
useSymfony\Component\Config\Definition\Builder\TreeBuilder;
1616
useSymfony\Component\Config\Definition\ConfigurationInterface;
1717
useSymfony\Component\Config\Definition\Exception\InvalidConfigurationException;
18+
useSymfony\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+
privatefunctionaddMailerSection(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‎

Lines changed: 8 additions & 4 deletions
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',newReference($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‎

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

1920
<xsd:attributename="auto-reload"type="xsd:string" />
@@ -55,4 +56,8 @@
5556
<xsd:enumerationvalue="service" />
5657
</xsd:restriction>
5758
</xsd:simpleType>
59+
60+
<xsd:complexTypename="mailer">
61+
<xsd:attributename="html-to-text-converter"type="xsd:string" />
62+
</xsd:complexType>
5863
</xsd:schema>
Lines changed: 7 additions & 0 deletions
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+
]);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" ?>
2+
<containerxmlns="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:mailerhtml-to-text-converter="my_converter" />
10+
</twig:config>
11+
</container>
Lines changed: 3 additions & 0 deletions
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‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
useSymfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2525
useSymfony\Component\DependencyInjection\Reference;
2626
useSymfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
27+
useSymfony\Component\Mailer\Mailer;
2728

2829
class TwigExtensionTestextends 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+
publicfunctiontestCustomHtmlToTextConverterService(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(newTwigExtension());
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(newReference('my_converter'),$bodyRenderer->getArgument('$converter'));
288+
}
289+
266290
privatefunctioncreateContainer()
267291
{
268292
$container =newContainerBuilder(newParameterBag([

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp