Skip to content

Commit

Permalink
[TwigBundle] made global more powerful
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
fabpot committed Jan 11, 2011
1 parent 9a2e053 commit 47b87e9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
Expand Up @@ -50,11 +50,21 @@ public function configLoad($config, ContainerBuilder $container)
$globals = $this->fixConfig($config, 'global');
if (isset($globals[0])) {
foreach ($globals as $global) {
$def->addMethodCall('addGlobal', array($global['key'], new Reference($global['id'])));
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']);
Expand Down
Expand Up @@ -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>

Expand All @@ -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>

Expand Down
Expand Up @@ -14,6 +14,7 @@
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
{
Expand All @@ -36,18 +37,28 @@ public function testConfigGlobals()
// 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()
Expand Down

0 comments on commit 47b87e9

Please sign in to comment.