Skip to content

Commit

Permalink
[TwigBundle] added a way to register Twig globals from configuration
Browse files Browse the repository at this point in the history
    <twig:config debug="%kernel.debug%" strict-variables="%kernel.debug%">
        <twig:global key="foo" id="request" />
    </twig:config>

    twig.config:
        globals:
          foo: request
  • Loading branch information
fabpot committed Jan 4, 2011
1 parent 7b7e83f commit 7fdc61f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/*
* This file is part of the Symfony framework.
Expand Down Expand Up @@ -44,6 +45,20 @@ public function configLoad($config, ContainerBuilder $container)
}
}

// globals
$def = $container->getDefinition('twig');
$globals = $this->fixConfig($config, 'global');
if (isset($globals[0])) {
foreach ($globals as $global) {
$def->addMethodCall('addGlobal', array($global['key'], new Reference($global['id'])));
}
} else {
foreach ($globals as $key => $id) {
$def->addMethodCall('addGlobal', array($key, new Reference($id)));
}
}
unset($config['globals'], $config['global']);

// convert - to _
foreach ($config as $key => $value) {
$config[str_replace('-', '_', $key)] = $value;
Expand Down Expand Up @@ -71,4 +86,21 @@ public function getAlias()
{
return 'twig';
}

protected function fixConfig($config, $key)
{
$values = array();
if (isset($config[$key.'s'])) {
$values = $config[$key.'s'];
} elseif (isset($config[$key])) {
if (is_string($config[$key]) || !is_int(key($config[$key]))) {
// only one
$values = array($config[$key]);
} else {
$values = $config[$key];
}
}

return $values;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +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:sequence>

<xsd:attribute name="charset" type="xsd:string" />
Expand All @@ -26,4 +27,9 @@
<xsd:element name="resource" type="xsd:string" minOccurs="1" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="global">
<xsd:attribute name="key" type="xsd:string" />
<xsd:attribute name="id" type="xsd:string" />
</xsd:complexType>
</xsd:schema>
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,23 @@ public function testConfigLoad()
$this->assertEquals('ISO-8859-1', $options['charset'], '->configLoad() overrides existing configuration options');
$this->assertEquals('%kernel.debug%', $options['debug'], '->configLoad() merges the new values with the old ones');
}

public function testConfigGlobals()
{
// XML
$container = new ContainerBuilder();
$loader = new TwigExtension();
$loader->configLoad(array('global' => array(array('key' => 'foo', 'id' => 'bar'))), $container);
$config = $container->getDefinition('twig')->getMethodCalls();
$this->assertEquals('foo', $config[0][1][0]);
$this->assertEquals('bar', (string) $config[0][1][1]);

// YAML, PHP
$container = new ContainerBuilder();
$loader = new TwigExtension();
$loader->configLoad(array('globals' => array('foo' => 'bar')), $container);
$config = $container->getDefinition('twig')->getMethodCalls();
$this->assertEquals('foo', $config[0][1][0]);
$this->assertEquals('bar', (string) $config[0][1][1]);
}
}

0 comments on commit 7fdc61f

Please sign in to comment.