<commit_before><?php declare(strict_types=1);
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015-2019, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\UtilsBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
 * 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
{
    /**
     * @var array
     */
    private $bundles;

    /**
     * @param array $bundles Bundles
     */
    public function __construct(array $bundles)
    {
        $this->bundles = $bundles;
    }

    /**
     * {@inheritDoc}
     */
    public function getConfigTreeBuilder(): TreeBuilder
    {
        $builder = new TreeBuilder('darvin_utils');

        /** @var \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition $root */
        $root = $builder->getRootNode();

        // Here you should define the parameters that are allowed to
        // configure your bundle. See the documentation linked above for
        // more information on that topic.
        $root
            ->children()
                ->append($this->buildOverrideNode())
                ->scalarNode('yandex_translate_api_key')->defaultNull()->end()
                ->arrayNode('response')->addDefaultsIfNotSet()
                    ->children()
                        ->booleanNode('compress')->defaultFalse();

        return $builder;
    }

    /**
     * @return \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition
     */
    private function buildOverrideNode(): ArrayNodeDefinition
    {
        $existingBundles = $this->bundles;

        $root = (new TreeBuilder('override'))->getRootNode();
        $root->useAttributeAsKey('bundle')
            ->validate()
                ->ifTrue(function (array $bundles) use ($existingBundles): bool {
                    foreach ($bundles as $bundle => $subjects) {
                        if (!isset($existingBundles[$bundle])) {
                            throw new \InvalidArgumentException(sprintf('Bundle "%s" does not exist.', $bundle));
                        }

                        $basePath = dirname((new \ReflectionClass($existingBundles[$bundle]))->getFileName());

                        foreach ($subjects as $subject) {
                            foreach ($subject['templates'] as $relativePath) {
                                $absolutePath = implode(DIRECTORY_SEPARATOR, [$basePath, $relativePath]);
<commit_msg>Simplify override configuration.<commit_after><?php declare(strict_types=1);
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015-2019, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\UtilsBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
 * 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
{
    /**
     * @var array
     */
    private $bundles;

    /**
     * @param array $bundles Bundles
     */
    public function __construct(array $bundles)
    {
        $this->bundles = $bundles;
    }

    /**
     * {@inheritDoc}
     */
    public function getConfigTreeBuilder(): TreeBuilder
    {
        $builder = new TreeBuilder('darvin_utils');

        /** @var \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition $root */
        $root = $builder->getRootNode();

        // Here you should define the parameters that are allowed to
        // configure your bundle. See the documentation linked above for
        // more information on that topic.
        $root
            ->children()
                ->append($this->buildOverrideNode())
                ->scalarNode('yandex_translate_api_key')->defaultNull()->end()
                ->arrayNode('response')->addDefaultsIfNotSet()
                    ->children()
                        ->booleanNode('compress')->defaultFalse();

        return $builder;
    }

    /**
     * @return \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition
     */
    private function buildOverrideNode(): ArrayNodeDefinition
    {
        $existingBundles = $this->bundles;

        $root = (new TreeBuilder('override'))->getRootNode();
        $root->useAttributeAsKey('bundle')
            ->validate()
                ->ifTrue(function (array $bundles) use ($existingBundles): bool {
                    foreach ($bundles as $bundle => $subjects) {
                        if (!isset($existingBundles[$bundle])) {
                            throw new \InvalidArgumentException(sprintf('Bundle "%s" does not exist.', $bundle));
                        }

                        $basePath = sprintf('%s/Resources/views', dirname((new \ReflectionClass($existingBundles[$bundle]))->getFileName()));

                        foreach ($subjects as $subject) {
                            foreach ($subject['templates'] as $relativePath) {
                                $absolutePath = implode(DIRECTORY_SEPARATOR, [$basePath, $relativePath]);

{'fraction_non_alphanumeric': 0.12133961629081118, 'fraction_numerical': 0.0030292830696735107, 'mean_word_length': 2.4018317115054377, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '393745', 'n_tokens_mistral': 1526, 'n_tokens_neox': 1466, 'n_words': 347}