<commit_before><?php
namespace Rocketeer\Services\Config;

use Illuminate\Support\Arr;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

class ConfigurationLoader
{
    /**
     * The various found configurations
     *
     * @type array
     */
    protected $configurations = [];

    /**
     * @type FileResource[]
     */
    protected $resources = [];

    /**
     * @type string[]
     */
    protected $folders;

    /**
     * @type LoaderInterface
     */
    protected $loader;

    /**
     * @type ConfigurationDefinition
     */
    protected $definition;

    /**
     * @type Processor
     */
    protected $processor;

    /**
     * @type ConfigurationCache
     */
    protected $cache;

    /**
     * ConfigurationLoader constructor.
     *
     * @param LoaderInterface         $loader
     * @param ConfigurationDefinition $definition
     * @param Processor               $processor
     * @param ConfigurationCache      $cache
     */
    public function __construct(
        LoaderInterface $loader,
        ConfigurationDefinition $definition,
        Processor $processor,
        ConfigurationCache $cache
    ) {
        $this->loader     = $loader;
        $this->definition = $definition;
        $this->processor  = $processor;
        $this->cache      = $cache;
    }

    //////////////////////////////////////////////////////////////////////
    //////////////////////// CONFIGURATION FOLDERS ///////////////////////
    //////////////////////////////////////////////////////////////////////

    /**
     * @return string[]
     */
    public function getFolders()
    {
        return $this->folders;
    }

    /**
     * @param string $folder
     */
    public function addFolder($folder)
    {
        $this->folders[] = $folder;
    }

    /**
     * @param string[] $folders
     */
    public function setFolders($folders)
    {
        $this->folders = $folders;
    }

    //////////////////////////////////////////////////////////////////////
    /////////////////////////// CONFIGURATION ////////////////////////////
    //////////////////////////////////////////////////////////////////////

    /**
     * Get a final merged version of the configuration,
     * taking into account defaults, user and contextual
     * configurations
     *
     * @param array $configurations Additional configurations to merge
     *
     * @return array
     */
    public function getConfiguration(array $configurations = [])
    {
        $this->configurations = [];
        $this->resources      = [];

        // Get which files are present in the configurations
        $folders = array_filter($this->folders, 'is_dir');
        if (!$folders) {
            return [];
        }

        $files = $this->getFinder($folders);
        $files = array_keys(iterator_to_array($files));

        // Return cached version if available
        if ($this->cache->isFresh()) {
            $cache = $this->cache->getContents();

            // If the files the configuration was cached from
            // match the ones we have, return the cache
            if (Arr::get($cache, 'meta') === $files) {
                return $cache;
            }
        }

        // Load in memory the files from all configurations
        foreach ($folders as $folder) {
            $this->configurations[$folder] = [];
            $this->loadConfigurationFor($folder);
            $this->loadContextualConfigurationsFor($folder);
        }

        // Merge additional configurations
        if ($configurations) {
            $this->configurations = array_merge($this->configurations, $configurations);
        }

        // Merge, process and validate configuration
        $processed = $this->processor->processConfiguration(
            $this->definition,
            $this->configurations
        );

        $processed['meta'] = $files;

        // Cache configuration
        $this->cache->write($processed, $this->resources);

        return $processed;
    }

    /**
     * @param string $folder
     */
    protected function loadConfigurationFor($folder)
    {
        /** @type SplFileInfo[] $files */
        $files = $this->getFinder($folder)
                      ->exclude(['connections', 'stages', 'tasks', 'events', 'strategies']);

        // Load base files
        foreach ($files as $file) {
            // Load file
            $contents = $this->loader->load($file->getPathname());
            $contents = $this->autoWrap($file, $contents);
            if (!is_array($contents)) {
                continue;
            }

            // Add to cache
            $this->resources[] = new FileResource($file->getPathname());

            $this->configurations[$folder] = array_merge($this->configurations[$folder], $contents);
        }
    }

    /**
     * @param string $folder
     */
    protected function loadContextualConfigurationsFor($folder)
    {
        $contextual = (new Finder())->in($folder)->name('/(stages|connections)/')->directories();
        foreach ($contextual as $type) {
            /** @type SplFileInfo[] $files */
            $files = (new Finder())->in($type->getPathname())->files();
<commit_msg>Fix plugins folder being parsed for config<commit_after><?php
namespace Rocketeer\Services\Config;

use Illuminate\Support\Arr;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

class ConfigurationLoader
{
    /**
     * The various found configurations
     *
     * @type array
     */
    protected $configurations = [];

    /**
     * @type FileResource[]
     */
    protected $resources = [];

    /**
     * @type string[]
     */
    protected $folders;

    /**
     * @type LoaderInterface
     */
    protected $loader;

    /**
     * @type ConfigurationDefinition
     */
    protected $definition;

    /**
     * @type Processor
     */
    protected $processor;

    /**
     * @type ConfigurationCache
     */
    protected $cache;

    /**
     * ConfigurationLoader constructor.
     *
     * @param LoaderInterface         $loader
     * @param ConfigurationDefinition $definition
     * @param Processor               $processor
     * @param ConfigurationCache      $cache
     */
    public function __construct(
        LoaderInterface $loader,
        ConfigurationDefinition $definition,
        Processor $processor,
        ConfigurationCache $cache
    ) {
        $this->loader     = $loader;
        $this->definition = $definition;
        $this->processor  = $processor;
        $this->cache      = $cache;
    }

    //////////////////////////////////////////////////////////////////////
    //////////////////////// CONFIGURATION FOLDERS ///////////////////////
    //////////////////////////////////////////////////////////////////////

    /**
     * @return string[]
     */
    public function getFolders()
    {
        return $this->folders;
    }

    /**
     * @param string $folder
     */
    public function addFolder($folder)
    {
        $this->folders[] = $folder;
    }

    /**
     * @param string[] $folders
     */
    public function setFolders($folders)
    {
        $this->folders = $folders;
    }

    //////////////////////////////////////////////////////////////////////
    /////////////////////////// CONFIGURATION ////////////////////////////
    //////////////////////////////////////////////////////////////////////

    /**
     * Get a final merged version of the configuration,
     * taking into account defaults, user and contextual
     * configurations
     *
     * @param array $configurations Additional configurations to merge
     *
     * @return array
     */
    public function getConfiguration(array $configurations = [])
    {
        $this->configurations = [];
        $this->resources      = [];

        // Get which files are present in the configurations
        $folders = array_filter($this->folders, 'is_dir');
        if (!$folders) {
            return [];
        }

        $files = $this->getFinder($folders);
        $files = array_keys(iterator_to_array($files));

        // Return cached version if available
        if ($this->cache->isFresh()) {
            $cache = $this->cache->getContents();

            // If the files the configuration was cached from
            // match the ones we have, return the cache
            if (Arr::get($cache, 'meta') === $files) {
                return $cache;
            }
        }

        // Load in memory the files from all configurations
        foreach ($folders as $folder) {
            $this->configurations[$folder] = [];
            $this->loadConfigurationFor($folder);
            $this->loadContextualConfigurationsFor($folder);
        }

        // Merge additional configurations
        if ($configurations) {
            $this->configurations = array_merge($this->configurations, $configurations);
        }

        // Merge, process and validate configuration
        $processed = $this->processor->processConfiguration(
            $this->definition,
            $this->configurations
        );

        $processed['meta'] = $files;

        // Cache configuration
        $this->cache->write($processed, $this->resources);

        return $processed;
    }

    /**
     * @param string $folder
     */
    protected function loadConfigurationFor($folder)
    {
        /** @type SplFileInfo[] $files */
        $files = $this->getFinder($folder)
                      ->exclude(['connections', 'stages', 'plugins', 'tasks', 'events', 'strategies']);

        // Load base files
        foreach ($files as $file) {
            // Load file
            $contents = $this->loader->load($file->getPathname());
            $contents = $this->autoWrap($file, $contents);
            if (!is_array($contents)) {
                continue;
            }

            // Add to cache
            $this->resources[] = new FileResource($file->getPathname());

            $this->configurations[$folder] = array_merge($this->configurations[$folder], $contents);
        }
    }

    /**
     * @param string $folder
     */
    protected function loadContextualConfigurationsFor($folder)
    {
        $contextual = (new Finder())->in($folder)->name('/(stages|connections)/')->directories();
        foreach ($contextual as $type) {
            /** @type SplFileInfo[] $files */
            $files = (new Finder())->in($type->getPathname())->files();

{'fraction_non_alphanumeric': 0.19154275092936804, 'fraction_numerical': 0.0, 'mean_word_length': 1.6131617289946576, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 89, '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': '2364218', 'n_tokens_mistral': 2668, 'n_tokens_neox': 2595, 'n_words': 585}