Skip to content

Commit

Permalink
Add a Twig extension to expose the RuntimeConfig.
Browse files Browse the repository at this point in the history
  • Loading branch information
bobthecow committed Nov 8, 2011
1 parent d63e0ac commit d544ed9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Resources/config/service.xml
Expand Up @@ -6,6 +6,7 @@
<parameters>
<parameter key="opensky.runtime_config.class">OpenSky\Bundle\RuntimeConfigBundle\Service\RuntimeParameterBag</parameter>
<parameter key="opensky.runtime_config.logger.class">OpenSky\Bundle\RuntimeConfigBundle\Service\RuntimeParameterBagLogger</parameter>
<parameter key="opensky.runtime_config.twig.class">OpenSky\Bundle\RuntimeConfigBundle\Twig\Extension\RuntimeConfigExtension</parameter>
<parameter key="opensky.runtime_config.logger.level">debug</parameter>
</parameters>

Expand All @@ -20,6 +21,11 @@
<argument type="service" id="logger" on-invalid="null" />
<tag name="monolog.logger" channel="opensky.runtime_config" />
</service>

<service id="opensky.runtime_config.twig" class="%opensky.runtime_config.twig.class%">
<argument type="service" id="opensky.runtime_config" />
<tag name="twig.extension" />
</service>
</services>

</container>
47 changes: 47 additions & 0 deletions Twig/Extension/RuntimeConfigExtension.php
@@ -0,0 +1,47 @@
<?php

namespace OpenSky\Bundle\RuntimeConfigBundle\Twig\Extension;

use OpenSky\Bundle\RuntimeConfigBundle\Service\RuntimeParameterBag;

class RuntimeConfigExtension extends \Twig_Extension
{
protected $runtimeConfig;

/**
* Constructor.
*
* @param ContainerInterface $container
*/
public function __construct(RuntimeParameterBag $runtimeConfig)
{
$this->runtimeConfig = $runtimeConfig;
}

/**
* Returns a list of global functions to add to the existing list.
*
* @return array An array of global functions
*/
public function getFunctions()
{
return array(
'runtime_config' => new \Twig_Function_Method($this, 'getRuntimeConfig'),
);
}

/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'runtime_config';
}

public function getRuntimeConfig($name)
{
return $this->runtimeConfig->get($name);
}
}

2 comments on commit d544ed9

@jmikola
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this was a long time in the making.

@bobthecow
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmikola apparently nobody but me needed it?

Please sign in to comment.