<commit_before><?php

namespace Honeybee\FrameworkBinding\Silex\Config\Handler;

use Honeybee\Common\Error\ConfigError;

class SettingConfigHandler extends YamlConfigHandler
{
    public function handle(array $configFiles)
    {
        return $this->interpolateConfigValues(
            array_merge_recursive(
                array_reduce(
                    array_map([ $this, 'handleConfigFile' ], $configFiles),
                    [ $this, 'mergeConfigs' ],
                    []
                ),
                (array)$this->configProvider->getSettings()->toArray()
            )
        );
    }

    protected function handleConfigFile($configFile)
    {
        $settings = [];

        $settingsConfig = $this->yamlParser->parse(file_get_contents($configFile));

        $localConfigs = isset($settingsConfig['local_configs']) ? $settingsConfig['local_configs'] : [];
        unset($settingsConfig['local_configs']);

        foreach ($localConfigs as $prefix => $localConfig) {
            $settings[$prefix] = $this->loadLocalSettings($localConfig);
        }
        foreach ($settingsConfig as $prefix => $rawSettings) {
            $settings[$prefix] = $rawSettings;
        }

        return $settings;
    }

    protected function mergeConfigs(array $out, array $in)
    {
        return array_merge_recursive($out, $in);
    }

    protected function loadLocalSettings(array $localConfig)
    {
        $configFile = $this->configProvider->getLocalConfigDir().'/'.$localConfig['name'];
        if ($localConfig['load'] === 'from_file') {
            $settings = $this->handleFileBasedLocalConfig($configFile, $localConfig['type']);
        }
        // @todo load from env

        return $settings;
    }

    protected function handleFileBasedLocalConfig($configFile, $type)
    {
        if ($type === 'yaml') {
            $yamlString = file_get_contents($configFile);
            if (!$yamlString) {
                throw new ConfigError('Failed to read local configuration at: '.$configFile);
            };
            try {
                $settings = $yaml_parser->parse($yamlString);
            } catch (\Exception $parseError) {
                throw new ConfigError(
                    'Failed to parse yaml for local config file: '.$configFile.PHP_EOL .
                    'Error: '.$parseError->getMessage()
                );
            }
        } elseif ($type === 'json') {
            $jsonString = file_get_contents($configFile);
            if (!$jsonString) {
                throw new Exception('Failed to read local configuration at: '.$configFile);
            }
            $settings = json_decode($jsonString, true);

            if (json_last_error() !== JSON_ERROR_NONE) {
                throw new ConfigError('Failed to parse json from file "'.$confgFile.'": '.json_last_error_msg());
            }
        } else {
            throw new ConfigError('Only "yaml" or "json" are supported for "type" setting of local-configs.');
        }

        return $settings;
    }

    protected function interpolateConfigValues(array $config, array $globalConf = null)
    {
        $globalConf = $globalConf ?: $config;
        foreach ($config as $key => $value) {
            if (is_array($value)) {
                $config[$key] = $this->interpolateConfigValues($value, $globalConf);
            } elseif (is_string($value)) {
                if (preg_match_all('/(\$\{(.*?)\})/', $value, $matches)) {
                    $replacements = [];
                    foreach ($matches[2] as $configKey) {
                        $replacements[] = $this->interpolateConfigValue($configKey, $globalConf);
                    }
                    $config[$key] = str_replace($matches[0], $replacements, $value);
                }
            }
        }
<commit_msg>use ConfigError instead of non-used Exception<commit_after><?php

namespace Honeybee\FrameworkBinding\Silex\Config\Handler;

use Honeybee\Common\Error\ConfigError;

class SettingConfigHandler extends YamlConfigHandler
{
    public function handle(array $configFiles)
    {
        return $this->interpolateConfigValues(
            array_merge_recursive(
                array_reduce(
                    array_map([ $this, 'handleConfigFile' ], $configFiles),
                    [ $this, 'mergeConfigs' ],
                    []
                ),
                (array)$this->configProvider->getSettings()->toArray()
            )
        );
    }

    protected function handleConfigFile($configFile)
    {
        $settings = [];

        $settingsConfig = $this->yamlParser->parse(file_get_contents($configFile));

        $localConfigs = isset($settingsConfig['local_configs']) ? $settingsConfig['local_configs'] : [];
        unset($settingsConfig['local_configs']);

        foreach ($localConfigs as $prefix => $localConfig) {
            $settings[$prefix] = $this->loadLocalSettings($localConfig);
        }
        foreach ($settingsConfig as $prefix => $rawSettings) {
            $settings[$prefix] = $rawSettings;
        }

        return $settings;
    }

    protected function mergeConfigs(array $out, array $in)
    {
        return array_merge_recursive($out, $in);
    }

    protected function loadLocalSettings(array $localConfig)
    {
        $configFile = $this->configProvider->getLocalConfigDir().'/'.$localConfig['name'];
        if ($localConfig['load'] === 'from_file') {
            $settings = $this->handleFileBasedLocalConfig($configFile, $localConfig['type']);
        }
        // @todo load from env

        return $settings;
    }

    protected function handleFileBasedLocalConfig($configFile, $type)
    {
        if ($type === 'yaml') {
            $yamlString = file_get_contents($configFile);
            if (!$yamlString) {
                throw new ConfigError('Failed to read local configuration at: '.$configFile);
            };
            try {
                $settings = $yaml_parser->parse($yamlString);
            } catch (\Exception $parseError) {
                throw new ConfigError(
                    'Failed to parse yaml for local config file: '.$configFile.PHP_EOL .
                    'Error: '.$parseError->getMessage()
                );
            }
        } elseif ($type === 'json') {
            $jsonString = file_get_contents($configFile);
            if (!$jsonString) {
                throw new ConfigError('Failed to read local configuration at: '.$configFile);
            }
            $settings = json_decode($jsonString, true);

            if (json_last_error() !== JSON_ERROR_NONE) {
                throw new ConfigError('Failed to parse json from file "'.$confgFile.'": '.json_last_error_msg());
            }
        } else {
            throw new ConfigError('Only "yaml" or "json" are supported for "type" setting of local-configs.');
        }

        return $settings;
    }

    protected function interpolateConfigValues(array $config, array $globalConf = null)
    {
        $globalConf = $globalConf ?: $config;
        foreach ($config as $key => $value) {
            if (is_array($value)) {
                $config[$key] = $this->interpolateConfigValues($value, $globalConf);
            } elseif (is_string($value)) {
                if (preg_match_all('/(\$\{(.*?)\})/', $value, $matches)) {
                    $replacements = [];
                    foreach ($matches[2] as $configKey) {
                        $replacements[] = $this->interpolateConfigValue($configKey, $globalConf);
                    }
                    $config[$key] = str_replace($matches[0], $replacements, $value);
                }
            }
        }

{'fraction_non_alphanumeric': 0.12992948550535388, 'fraction_numerical': 0.0005223295899712719, 'mean_word_length': 2.026076649545634, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 37, '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': '6978955', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1977, 'n_words': 411}