<commit_before><?php

namespace Ola\RabbitMqAdminToolkitBundle\DependencyInjection;

use Ola\RabbitMqAdminToolkitBundle\Manager\QueueManager;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritdoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('ola_rabbit_mq_admin_toolkit');

        $rootNode
            ->addDefaultsIfNotSet()
            ->children()
                ->scalarNode('default_vhost')->defaultValue('default')->end()
                ->scalarNode('delete_allowed')->defaultFalse()->end()
                ->scalarNode('silent_failure')->defaultFalse()->end()
                ->arrayNode('connections')
                    ->prototype('scalar')
                    ->end()
                ->end()
                ->arrayNode('vhosts')
                    ->prototype('array')
                        ->children()
                            ->scalarNode('name')->end()
                            ->scalarNode('connection')->defaultValue('default')->end()
                            ->scalarNode('delete_allowed')->end()
                            ->append($this->getPermissionsConfiguration())
                            ->append($this->getExchangesConfiguration())
                            ->append($this->getQueuesConfiguration())
                        ->end()
                    ->end()
                ->end()
            ->end()
        ;
        return $treeBuilder;
    }

    /**
     * @return NodeDefinition
     */
    private function getPermissionsConfiguration()
    {
        $node = new ArrayNodeDefinition('permissions');

        return $node
            ->prototype('array')
                ->children()
                    ->scalarNode('configure')->defaultValue('.*')->end()
                    ->scalarNode('read')->defaultValue('.*')->end()
                    ->scalarNode('write')->defaultValue('.*')->end()
                ->end()
            ->end();
    }

    /**
     * @return NodeDefinition
     */
    private function getExchangesConfiguration()
    {
        $node = new ArrayNodeDefinition('exchanges');

        $this->appendNameNormalization($node);

        return $node
            ->prototype('array')
                ->children()
                    ->scalarNode('name')->end()
                    ->scalarNode('type')->defaultValue('topic')->end()
                    ->scalarNode('durable')->defaultTrue()->end()
                ->end()
            ->end();
    }

    /**
     * @return NodeDefinition
     */
    private function getQueuesConfiguration()
    {
        $modulusValidation = function($queues) {

            $hasModulus = function($string) {
                return strpos($string, QueueManager::MODULUS_PLACEHOLDER) !== false;
            };

            foreach ($queues as $name => $queue) {
                if (isset($queue['modulus']) && is_int($queue['modulus'])) {

                    if (!$hasModulus($queue['name'])) {
                        return true;
                    }

                    $bindingsHaveModulus = false;
                    foreach ($queue['bindings'] as $binding) {
                        if ($hasModulus($binding['routing_key'])) {
                            $bindingsHaveModulus = true;
                        }
                    }

                    if (!$bindingsHaveModulus) {
                        return true;
                    }
                }
            }
        };

        $node = new ArrayNodeDefinition('queues');

        $this->appendNameNormalization($node);

        return $node
            ->validate()
                ->ifTrue($modulusValidation)
                ->thenInvalid('If queue.modulus is defined queue.name & at least one associated routing_key should contain a {modulus} part')
            ->end()
            ->prototype('array')
                ->children()
                    ->scalarNode('name')->end()
                    ->scalarNode('durable')->defaultTrue()->end()
                    ->scalarNode('modulus')->defaultNull()->end()
                    ->arrayNode('bindings')
                        ->prototype('array')
                            ->children()
                                ->scalarNode('exchange')->end()
                                ->scalarNode('routing_key')->end()
                            ->end()
                        ->end()
                    ->end()
                ->end()
            ->end();
    }

    /**
     * @param NodeDefinition $node
     *
     * @return NodeDefinition
     */
    private function appendNameNormalization(NodeDefinition $node)
    {
        return $node->beforeNormalization()
            ->ifTrue(function($array) {
                foreach ($array as $item) {
                    if (!isset($item['name'])) {
                        return true;
                    }
                }
            })
            ->then(function($array) {
                foreach ($array as $name => $item) {
                    if (!isset($item['name'])) {
                        $array[$name]['name'] = $name;
                    }
                }

                return $array;
            })
            ->end();

    }
}<commit_msg>Enable configuration overriding<commit_after><?php

namespace Ola\RabbitMqAdminToolkitBundle\DependencyInjection;

use Ola\RabbitMqAdminToolkitBundle\Manager\QueueManager;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritdoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('ola_rabbit_mq_admin_toolkit');

        $rootNode
            ->addDefaultsIfNotSet()
            ->children()
                ->scalarNode('default_vhost')->defaultValue('default')->end()
                ->scalarNode('delete_allowed')->defaultFalse()->end()
                ->scalarNode('silent_failure')->defaultFalse()->end()
                ->arrayNode('connections')
                    ->prototype('scalar')
                    ->end()
                ->end()
                ->arrayNode('vhosts')
                    ->useAttributeAsKey('identifier')
                    ->canBeUnset(true)
                    ->prototype('array')
                        ->children()
                            ->scalarNode('name')->end()
                            ->scalarNode('connection')->defaultValue('default')->end()
                            ->scalarNode('delete_allowed')->end()
                            ->append($this->getPermissionsConfiguration())
                            ->append($this->getExchangesConfiguration())
                            ->append($this->getQueuesConfiguration())
                        ->end()
                    ->end()
                ->end()
            ->end()
        ;
        return $treeBuilder;
    }

    /**
     * @return NodeDefinition
     */
    private function getPermissionsConfiguration()
    {
        $node = new ArrayNodeDefinition('permissions');

        return $node
            ->useAttributeAsKey('identifier')
            ->canBeUnset(true)
            ->prototype('array')
                ->children()
                    ->scalarNode('configure')->defaultValue('.*')->end()
                    ->scalarNode('read')->defaultValue('.*')->end()
                    ->scalarNode('write')->defaultValue('.*')->end()
                ->end()
            ->end();
    }

    /**
     * @return NodeDefinition
     */
    private function getExchangesConfiguration()
    {
        $node = new ArrayNodeDefinition('exchanges');

        $this->appendNameNormalization($node);

        return $node
            ->useAttributeAsKey('identifier')
            ->canBeUnset(true)
            ->prototype('array')
                ->children()
                    ->scalarNode('name')->end()
                    ->scalarNode('type')->defaultValue('topic')->end()
                    ->scalarNode('durable')->defaultTrue()->end()
                ->end()
            ->end();
    }

    /**
     * @return NodeDefinition
     */
    private function getQueuesConfiguration()
    {
        $modulusValidation = function($queues) {

            $hasModulus = function($string) {
                return strpos($string, QueueManager::MODULUS_PLACEHOLDER) !== false;
            };

            foreach ($queues as $name => $queue) {
                if (isset($queue['modulus']) && is_int($queue['modulus'])) {

                    if (!$hasModulus($queue['name'])) {
                        return true;
                    }

                    $bindingsHaveModulus = false;
                    foreach ($queue['bindings'] as $binding) {
                        if ($hasModulus($binding['routing_key'])) {
                            $bindingsHaveModulus = true;
                        }
                    }

                    if (!$bindingsHaveModulus) {
                        return true;
                    }
                }
            }
        };

        $node = new ArrayNodeDefinition('queues');

        $this->appendNameNormalization($node);

        return $node
            ->useAttributeAsKey('identifier')
            ->validate()
                ->ifTrue($modulusValidation)
                ->thenInvalid('If queue.modulus is defined queue.name & at least one associated routing_key should contain a {modulus} part')
            ->end()
            ->prototype('array')
                ->canBeUnset(true)
                ->children()
                    ->scalarNode('name')->end()
                    ->scalarNode('durable')->defaultTrue()->end()
                    ->scalarNode('modulus')->defaultNull()->end()
                    ->arrayNode('bindings')
                        ->performNoDeepMerging()
                        ->prototype('array')
                            ->children()
                                ->scalarNode('exchange')->end()
                                ->scalarNode('routing_key')->end()
                            ->end()
                        ->end()
                    ->end()
                ->end()
            ->end();
    }

    /**
     * @param NodeDefinition $node
     *
     * @return NodeDefinition
     */
    private function appendNameNormalization(NodeDefinition $node)
    {
        return $node
            ->beforeNormalization()
                ->ifTrue(function($array) {
                    foreach ($array as $item) {
                        if (false !== $item && !isset($item['name'])) {
                            return true;
                        }
                    }
                })
                ->then(function($array) {
                    foreach ($array as $name => $item) {
                        if (!isset($item['name'])) {
                            $array[$name]['name'] = $name;
                        }
                    }

                    return $array;
                })
            ->end();
    }
}
{'fraction_non_alphanumeric': 0.13708563535911603, 'fraction_numerical': 0.0, 'mean_word_length': 1.3239719157472418, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 202, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5280737', 'n_tokens_mistral': 2749, 'n_tokens_neox': 2581, 'n_words': 408}