allow to configure custom formats in XML configs

This commit is contained in:
Christian Flothmann 2017-07-09 15:51:00 +02:00
parent 3c9958cbc3
commit 5a3a24b0ac
5 changed files with 70 additions and 0 deletions

View File

@ -9,6 +9,9 @@
<xsd:complexType name="config">
<xsd:sequence>
<xsd:element name="date" type="date" minOccurs="0" maxOccurs="1" />
<xsd:element name="number-format" type="number_format" minOccurs="0" maxOccurs="1" />
<!-- @deprecated since version 2.6, to be removed in 3.0 -->
<xsd:element name="form" type="form" minOccurs="0" maxOccurs="1" />
<xsd:element name="form-theme" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
@ -28,6 +31,18 @@
<xsd:attribute name="exception-controller" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="date">
<xsd:attribute name="format" type="xsd:string" />
<xsd:attribute name="interval-format" type="xsd:string" />
<xsd:attribute name="timezone" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="number_format">
<xsd:attribute name="decimals" type="xsd:integer" />
<xsd:attribute name="decimal-point" type="xsd:string" />
<xsd:attribute name="thousands-separator" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="form">
<xsd:choice minOccurs="1" maxOccurs="unbounded">
<xsd:element name="resource" type="xsd:string" />

View File

@ -0,0 +1,14 @@
<?php
$container->loadFromExtension('twig', array(
'date' => array(
'format' => 'Y-m-d',
'interval_format' => '%d',
'timezone' => 'Europe/Berlin',
),
'number_format' => array(
'decimals' => 2,
'decimal_point' => ',',
'thousands_separator' => '.',
),
));

View File

@ -0,0 +1,12 @@
<?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 http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/twig http://symfony.com/schema/dic/twig/twig-1.0.xsd">
<twig:config>
<twig:date format="Y-m-d" interval-format="%d" timezone="Europe/Berlin" />
<twig:number-format decimals="2" decimal-point="," thousands-separator="." />
</twig:config>
</container>

View File

@ -0,0 +1,9 @@
twig:
date:
format: Y-m-d
interval_format: '%d'
timezone: Europe/Berlin
number_format:
decimals: 2
decimal_point: ','
thousands_separator: .

View File

@ -150,6 +150,26 @@ class TwigExtensionTest extends TestCase
$this->assertEquals('name', $options['autoescape']);
}
/**
* @dataProvider getFormats
*/
public function testLoadCustomDateFormats($fileFormat)
{
$container = $this->createContainer();
$container->registerExtension(new TwigExtension());
$this->loadFromFile($container, 'formats', $fileFormat);
$this->compileContainer($container);
$environmentConfigurator = $container->getDefinition('twig.configurator.environment');
$this->assertSame('Y-m-d', $environmentConfigurator->getArgument(0));
$this->assertSame('%d', $environmentConfigurator->getArgument(1));
$this->assertSame('Europe/Berlin', $environmentConfigurator->getArgument(2));
$this->assertSame(2, $environmentConfigurator->getArgument(3));
$this->assertSame(',', $environmentConfigurator->getArgument(4));
$this->assertSame('.', $environmentConfigurator->getArgument(5));
}
public function testGlobalsWithDifferentTypesAndValues()
{
$globals = array(