[TwigBundle] made global more powerful
A global can now be a service or a string: <twig:config debug="%kernel.debug%" strict-variables="%kernel.debug%"> <twig:global key="request" type="service" id="request" /> <twig:global key="PI">3.14</twig:global> </twig:config>
This commit is contained in:
parent
9a2e053cbc
commit
47b87e902e
@ -50,11 +50,21 @@ class TwigExtension extends Extension
|
||||
$globals = $this->fixConfig($config, 'global');
|
||||
if (isset($globals[0])) {
|
||||
foreach ($globals as $global) {
|
||||
if (isset($global['type']) && 'service' === $global['type']) {
|
||||
$def->addMethodCall('addGlobal', array($global['key'], new Reference($global['id'])));
|
||||
} elseif (isset($global['value'])) {
|
||||
$def->addMethodCall('addGlobal', array($global['key'], $global['value']));
|
||||
} else {
|
||||
throw new \InvalidArgumentException(sprintf('Unable to understand global configuration (%s).', var_export($global, true)));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
foreach ($globals as $key => $id) {
|
||||
$def->addMethodCall('addGlobal', array($key, new Reference($id)));
|
||||
foreach ($globals as $key => $value) {
|
||||
if ('@' === substr($value, 0, 1)) {
|
||||
$def->addMethodCall('addGlobal', array($key, new Reference(substr($value, 1))));
|
||||
} else {
|
||||
$def->addMethodCall('addGlobal', array($key, $value));
|
||||
}
|
||||
}
|
||||
}
|
||||
unset($config['globals'], $config['global']);
|
||||
|
@ -10,7 +10,7 @@
|
||||
<xsd:complexType name="config">
|
||||
<xsd:sequence>
|
||||
<xsd:element name="form" type="form" minOccurs="0" maxOccurs="1" />
|
||||
<xsd:element name="global" type="global" minOccurs="0" maxOccurs="1" />
|
||||
<xsd:element name="global" type="global" minOccurs="0" maxOccurs="unbounded" />
|
||||
<xsd:element name="extension" type="extension" minOccurs="0" maxOccurs="unbounded" />
|
||||
</xsd:sequence>
|
||||
|
||||
@ -29,8 +29,9 @@
|
||||
</xsd:sequence>
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="global">
|
||||
<xsd:attribute name="key" type="xsd:string" />
|
||||
<xsd:complexType name="global" mixed="true">
|
||||
<xsd:attribute name="key" type="xsd:string" use="required" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="id" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
|
||||
|
@ -14,6 +14,7 @@ namespace Symfony\Bundle\TwigBundle\Tests\DependencyInjection;
|
||||
use Symfony\Bundle\TwigBundle\Tests\TestCase;
|
||||
use Symfony\Bundle\TwigBundle\DependencyInjection\TwigExtension;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
class TwigExtensionTest extends TestCase
|
||||
{
|
||||
@ -36,18 +37,28 @@ class TwigExtensionTest extends TestCase
|
||||
// XML
|
||||
$container = new ContainerBuilder();
|
||||
$loader = new TwigExtension();
|
||||
$loader->configLoad(array('global' => array(array('key' => 'foo', 'id' => 'bar'))), $container);
|
||||
$loader->configLoad(array('global' => array(
|
||||
array('key' => 'foo', 'type' => 'service', 'id' => 'bar'),
|
||||
array('key' => 'pi', 'value' => 3.14),
|
||||
)), $container);
|
||||
$config = $container->getDefinition('twig')->getMethodCalls();
|
||||
$this->assertEquals('foo', $config[0][1][0]);
|
||||
$this->assertEquals('bar', (string) $config[0][1][1]);
|
||||
$this->assertEquals(new Reference('bar'), $config[0][1][1]);
|
||||
$this->assertEquals('pi', $config[1][1][0]);
|
||||
$this->assertEquals(3.14, $config[1][1][1]);
|
||||
|
||||
// YAML, PHP
|
||||
$container = new ContainerBuilder();
|
||||
$loader = new TwigExtension();
|
||||
$loader->configLoad(array('globals' => array('foo' => 'bar')), $container);
|
||||
$loader->configLoad(array('globals' => array(
|
||||
'foo' => '@bar',
|
||||
'pi' => 3.14,
|
||||
)), $container);
|
||||
$config = $container->getDefinition('twig')->getMethodCalls();
|
||||
$this->assertEquals('foo', $config[0][1][0]);
|
||||
$this->assertEquals('bar', (string) $config[0][1][1]);
|
||||
$this->assertEquals(new Reference('bar'), $config[0][1][1]);
|
||||
$this->assertEquals('pi', $config[1][1][0]);
|
||||
$this->assertEquals(3.14, $config[1][1][1]);
|
||||
}
|
||||
|
||||
public function testConfigExtensions()
|
||||
|
Reference in New Issue
Block a user