-
-
Save Mparaiso/3748767 to your computer and use it in GitHub Desktop.
"Recursive" configuration in Symfony2 Config Component
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
fashion_web_dynamic_routing: | |
paths: | |
family: | |
action: FashionWeb\ProductBundle\Controller\FamilyController::showFamilyAction | |
children: | |
subfamily: | |
action: FashionWeb\ProductBundle\Controller\ProductController::showByFamilyAndSubfamilyAction | |
campaign: | |
identifier: true | |
action: FashionWeb\CampaignBundle\Controller\CampaignController::showCampaignByFamilyAction | |
children: | |
subfamily: | |
action: FashionWeb\CampaignBundle\Controller\CampaignController::showCampaignByFamilyAndSubfamilyAction | |
campaign: | |
identifier: true | |
action: FashionWeb\CampaignBundle\Controller\CampaignController::showCampaignAction | |
children: | |
product: | |
identifier: true | |
action: FashionWeb\ProductBundle\Controller\ProductController::showProductAction |
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
<?php | |
namespace FashionWeb\DynamicRoutingBundle\DependencyInjection; | |
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | |
use Symfony\Component\Config\Definition\ConfigurationInterface; | |
use Symfony\Component\Config\Definition\Builder\NodeDefinition; | |
/** | |
* This is the class that validates and merges configuration from your app/config files | |
* | |
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class} | |
*/ | |
class Configuration implements ConfigurationInterface | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public function getConfigTreeBuilder() | |
{ | |
$treeBuilder = new TreeBuilder(); | |
$rootNode = $treeBuilder->root('fashion_web_dynamic_routing'); | |
$this->buildPathNode( | |
$rootNode | |
->children() | |
->arrayNode('paths') | |
->validate()->ifTrue(function($element) {return count(array_filter($element, function($element) {return $element['identifier'];})) > 1;})->thenInvalid('There should be only one element with identifier => true.')->end() | |
->useAttributeAsKey('id') | |
->defaultValue(array()) | |
->prototype('array') | |
) | |
->end() | |
->end() | |
->end() | |
; | |
return $treeBuilder; | |
} | |
protected function evaluateChildren(&$child, $name) | |
{ | |
$child = $this->getPathNode($name)->finalize($child); | |
} | |
protected function buildPathNode(NodeDefinition $node) | |
{ | |
return $node | |
->addDefaultsIfNotSet() | |
->validate()->ifTrue(function($element) {return count(array_filter($element['children'], function($element) {return $element['identifier'];})) > 1;})->thenInvalid('There should be only one element with identifier => true.')->end() | |
->children() | |
->scalarNode('action')->defaultNull()->end() | |
->booleanNode('identifier')->defaultFalse()->end() | |
->variableNode('children') | |
->defaultValue(array()) | |
->validate()->ifTrue(function($element) { return !is_array($element); })->thenInvalid('The children element must be an array.')->end() | |
->validate()->always(function($children) {array_walk($children, array($this, 'evaluateChildren'));return $children;})->end() | |
->end() | |
->end() | |
; | |
} | |
protected function getPathNode($name = '') | |
{ | |
$treeBuilder = new TreeBuilder(); | |
$definition = $treeBuilder->root($name); | |
$this->buildPathNode($definition); | |
return $definition->getNode(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment