Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TwigBundle] Refactored TwigExtension class and implemented configura…
…tion tree Added config fixtures in each format to demonstrate the possible styles of all of the extension options. These should all be covered by the updated tests. Made XSD slightly more restrictive, with regards to the "type" attribute on globals. This is coupled with validation in the configuration class.
- Loading branch information
Showing
8 changed files
with
344 additions
and
141 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\TwigBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\NodeBuilder; | ||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
|
||
/** | ||
* TwigExtension configuration structure. | ||
* | ||
* @author Jeremy Mikola <jmikola@gmail.com> | ||
*/ | ||
class Configuration | ||
{ | ||
/** | ||
* Generates the configuration tree. | ||
* | ||
* @return \Symfony\Component\Config\Definition\NodeInterface | ||
*/ | ||
public function getConfigTree() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$rootNode = $treeBuilder->root('twig', 'array'); | ||
|
||
$rootNode | ||
->scalarNode('cache_warmer')->end() | ||
; | ||
|
||
$this->addExtensionsSection($rootNode); | ||
$this->addFormSection($rootNode); | ||
$this->addGlobalsSection($rootNode); | ||
$this->addTwigOptions($rootNode); | ||
|
||
return $treeBuilder->buildTree(); | ||
} | ||
|
||
private function addExtensionsSection(NodeBuilder $rootNode) | ||
{ | ||
$rootNode | ||
->fixXmlConfig('extension') | ||
->arrayNode('extensions') | ||
->prototype('scalar') | ||
->beforeNormalization() | ||
->ifTrue(function($v) { return is_array($v) && isset($v['id']); }) | ||
->then(function($v){ return $v['id']; }) | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
} | ||
|
||
private function addFormSection(NodeBuilder $rootNode) | ||
{ | ||
$rootNode | ||
->arrayNode('form') | ||
->addDefaultsIfNotSet() | ||
->fixXmlConfig('resource') | ||
->arrayNode('resources') | ||
->addDefaultsIfNotSet() | ||
->defaultValue(array('TwigBundle::form.html.twig')) | ||
->validate() | ||
->always() | ||
->then(function($v){ | ||
return array_merge(array('TwigBundle::form.html.twig'), $v); | ||
}) | ||
->end() | ||
->prototype('scalar')->end() | ||
->end() | ||
->end() | ||
; | ||
} | ||
|
||
private function addGlobalsSection(NodeBuilder $rootNode) | ||
{ | ||
$rootNode | ||
->fixXmlConfig('global') | ||
->arrayNode('globals') | ||
->useAttributeAsKey('key') | ||
->prototype('array') | ||
->beforeNormalization() | ||
->ifTrue(function($v){ return is_scalar($v); }) | ||
->then(function($v){ | ||
return ('@' === substr($v, 0, 1)) | ||
? array('id' => substr($v, 1), 'type' => 'service') | ||
: array('value' => $v); | ||
}) | ||
->end() | ||
->scalarNode('id')->end() | ||
->scalarNode('type') | ||
->validate() | ||
->ifNotInArray(array('service')) | ||
->thenInvalid('The %s type is not supported') | ||
->end() | ||
->end() | ||
->scalarNode('value')->end() | ||
->end() | ||
->end() | ||
; | ||
} | ||
|
||
private function addTwigOptions(NodeBuilder $rootNode) | ||
{ | ||
$rootNode | ||
->scalarNode('autoescape')->end() | ||
->scalarNode('base_template_class')->end() | ||
->scalarNode('cache') | ||
->addDefaultsIfNotSet() | ||
->defaultValue('%kernel.cache_dir%/twig') | ||
->end() | ||
->scalarNode('charset') | ||
->addDefaultsIfNotSet() | ||
->defaultValue('%kernel.charset%') | ||
->end() | ||
->scalarNode('debug') | ||
->addDefaultsIfNotSet() | ||
->defaultValue('%kernel.debug%') | ||
->end() | ||
->scalarNode('strict_variables')->end() | ||
->scalarNode('auto_reload')->end() | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/php/full.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
$container->loadFromExtension('twig', array( | ||
'form' => array( | ||
'resources' => array( | ||
'MyBundle::form.html.twig', | ||
) | ||
), | ||
'extensions' => array( | ||
'twig.extension.debug', | ||
'twig.extension.text', | ||
), | ||
'globals' => array( | ||
'foo' => '@bar', | ||
'pi' => 3.14, | ||
), | ||
'auto_reload' => true, | ||
'autoescape' => true, | ||
'base_template_class' => 'stdClass', | ||
'cache' => '/tmp', | ||
'cache_warmer' => true, | ||
'charset' => 'ISO-8859-1', | ||
'debug' => true, | ||
'strict_variables' => true, | ||
)); |
18 changes: 18 additions & 0 deletions
18
src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/xml/full.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://www.symfony-project.org/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:twig="http://www.symfony-project.org/schema/dic/twig" | ||
xsi:schemaLocation="http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd | ||
http://www.symfony-project.org/schema/dic/twig http://www.symfony-project.org/schema/dic/twig/twig-1.0.xsd"> | ||
|
||
<twig:config auto-reload="true" autoescape="true" base-template-class="stdClass" cache="/tmp" cache-warmer="true" charset="ISO-8859-1" debug="true" strict-variables="true"> | ||
<twig:form> | ||
<twig:resource>MyBundle::form.html.twig</twig:resource> | ||
</twig:form> | ||
<twig:global key="foo" id="bar" type="service" /> | ||
<twig:global key="pi">3.14</twig:global> | ||
<twig:extension id="twig.extension.debug" /> | ||
<twig:extension id="twig.extension.text" /> | ||
</twig:config> | ||
</container> |
Oops, something went wrong.