New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symfony Config: two new methods for configuration definition tree manipulation to better reuse #27534

Open
cejen opened this Issue Jun 7, 2018 · 0 comments

Comments

Projects
None yet
2 participants
@cejen

cejen commented Jun 7, 2018

Description
I can't find any way to make some parts of configuration definition reuseable. The best approach by me is to add two methods which can provides configuration tree manipulation (similar to DOM manipulation). But any other approach is welcome.

  1. \\Symfony\\Component\\Config\\Definition\\TreeBuilder::findNode(string $path):NodeDefinition - this method find node on given path (e.g. $node = $treeBuilder->findNode('node_name\node_name\node_name')
  2. \\Symfony\\Component\\Config\\Definition\\Builder\\NodeDefinition::addNode(NodeDefinition $node): NodeDefinition - this method adds given node under.

Example

class EventDispatcherConfig implements ConfigurationInterface {

    private function addSerializerDefinition(NodeDefinition $node, $comment)
    {
        $node->addNode(
            (new ArrayNodeDefinition('serializer', $node))
                ->info($comment')
                ->children()
                    ->scalarNode('service')
                        ->isRequired()
                        ->cannotBeEmpty()
                    ->end()
                    ->enumNode('format')
                        ->isRequired()
                        ->cannotBeEmpty()
                        ->values(['json', 'xml'])
                    ->end()
                    ->arrayNode('groups')
                        ->scalarPrototype()
                        ->end()
                    ->end()
                    ->scalarNode('version')
                    ->end()
                ->end()
            ->end()
        );
    }

    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('A');
        ...
        // Now i can add repetitive definitions into
        $this->addSerializerDefinition($treeBuilder->findNode('A.B', 
            'Default serializer used to serialize enqueued events (used when route definition does not contain serializer definition'));
        $this->addSerializerDefinition($treeBuilder->findNode('A.C.D.E', 
            'Serializer used to serialize enqueued events'));
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment