<commit_before><?php

namespace ShopwareCli;

use ShopwareCli\Services\PathProvider\PathProvider;
use Symfony\Component\Yaml\Yaml;

/**
 * Simple config object for the config.yaml file
 *
 * Class Config
 * @package ShopwareCli
 */
class Config implements \ArrayAccess
{
    /**
     * @var array
     */
    protected $configArray;

    /**
     * @var PathProvider
     */
    private $pathProvider;

    private $mainConfig;

    /**
     * @param PathProvider $pathProvider
     */
    public function __construct(PathProvider $pathProvider)
    {
        $this->pathProvider = $pathProvider;

        $this->mainConfig = $this->getMainConfigFile();

        $config = $this->getMergedConfigs($this->collectConfigFiles());
        $this->configArray = Yaml::parse($config, true);
    }

    /**
     * Iterate the extension directories and return config.yaml files
     *
     * @return string[]
     */
    private function collectConfigFiles()
    {
        $files = array(
            $this->mainConfig
        );

        $vendorIterator = new \DirectoryIterator($this->pathProvider->getExtensionPath());

        /** @var $vendorFileInfo \DirectoryIterator */
        foreach ($vendorIterator as $vendorFileInfo) {
            if (!$this->isValidDir($vendorFileInfo)) {
                continue;
            }
            $extensionIterator = new \DirectoryIterator($vendorFileInfo->getPathname());

            /** @var $extensionFileInfo \DirectoryIterator */
            foreach ($extensionIterator as $extensionFileInfo) {
                if (!$this->isValidDir($extensionFileInfo)) {
                    continue;
                }
                $file = $extensionFileInfo->getPathname() . '/config.yaml';

                if (file_exists($file)) {
                    $files[] = $file;
                }
            }
        }

        return $files;
    }

    /**
     * @param \DirectoryIterator $fileInfo
     *
     * @return bool
     */
    private function isValidDir(\DirectoryIterator $fileInfo)
    {
        return $fileInfo->isDir() && !$fileInfo->isDot() && stripos(
            $fileInfo->getBasename(),
            '.'
        ) !== 0; // skip dot directories e.g. .git
    }

    /**
     * Merge all given config.yaml files
     *
     * @param  string[] $paths
     * @return string
     */
    private function getMergedConfigs($paths)
    {
        $content = array();

        foreach ($paths as $path) {
            $content[] = file_get_contents($path);
        }

        return implode("\n", $content);
    }

    /**
     * @return mixed
     */
    public function getRepositories()
    {
        return $this->configArray['repositories'];
    }

    /**
     * @param mixed $offset
     *
     * @return bool
     */
    public function offsetExists($offset)
    {
        return isset($this->configArray[$offset]);
    }

    /**
     * @param mixed $offset
     *
     * @return mixed
     */
    public function offsetGet($offset)
    {
        return $this->configArray[$offset];
    }

    /**
     * @param mixed $offset
     * @param mixed $value
     */
    public function offsetSet($offset, $value)
    {
        $this->configArray[$offset] = $value;
    }

    /**
     * @param mixed $offset
     */
    public function offsetUnset($offset)
    {
        unset($this->configArray[$offset]);
    }

    /**
     * Copy config.yaml.dist to the config directory, if the main config file does not exist, yet.
     *
     * @throws \RuntimeException
     */
    private function getMainConfigFile()
    {
        $configFile = $this->pathProvider->getConfigPath() . '/config.yaml';

        // If the main config file exists, use that
        if (file_exists($configFile)) {
            return $configFile;
        }

        // Else try the config.yaml (not .dist) file inside the cli dir
        // This will allow you to release own box releases with bundled config
        $bundledConfig = $this->pathProvider->getCliToolPath() . '/assets/config.yaml';
        if (file_exists($bundledConfig)) {
            return $bundledConfig;
        }

        // Else copy over config.yaml.dist to the user's config dir and use that
        copy($this->pathProvider->getCliToolPath() . '/config.yaml.dist', $configFile);
        if (!file_exists($configFile)) {
            throw new \RuntimeException("Could not find '{$configFile}'");
        }

        return $configFile;
    }
}
<commit_msg>Don't copy fallback config - use shipped config directly<commit_after><?php

namespace ShopwareCli;

use ShopwareCli\Services\PathProvider\PathProvider;
use Symfony\Component\Yaml\Yaml;

/**
 * Simple config object for the config.yaml file
 *
 * Class Config
 * @package ShopwareCli
 */
class Config implements \ArrayAccess
{
    /**
     * @var array
     */
    protected $configArray;

    /**
     * @var PathProvider
     */
    private $pathProvider;

    private $mainConfig;

    /**
     * @param PathProvider $pathProvider
     */
    public function __construct(PathProvider $pathProvider)
    {
        $this->pathProvider = $pathProvider;

        $this->mainConfig = $this->getMainConfigFile();

        $config = $this->getMergedConfigs($this->collectConfigFiles());
        $this->configArray = Yaml::parse($config, true);
    }

    /**
     * Iterate the extension directories and return config.yaml files
     *
     * @return string[]
     */
    private function collectConfigFiles()
    {
        $files = array(
            $this->mainConfig
        );

        $vendorIterator = new \DirectoryIterator($this->pathProvider->getExtensionPath());

        /** @var $vendorFileInfo \DirectoryIterator */
        foreach ($vendorIterator as $vendorFileInfo) {
            if (!$this->isValidDir($vendorFileInfo)) {
                continue;
            }
            $extensionIterator = new \DirectoryIterator($vendorFileInfo->getPathname());

            /** @var $extensionFileInfo \DirectoryIterator */
            foreach ($extensionIterator as $extensionFileInfo) {
                if (!$this->isValidDir($extensionFileInfo)) {
                    continue;
                }
                $file = $extensionFileInfo->getPathname() . '/config.yaml';

                if (file_exists($file)) {
                    $files[] = $file;
                }
            }
        }

        return $files;
    }

    /**
     * @param \DirectoryIterator $fileInfo
     *
     * @return bool
     */
    private function isValidDir(\DirectoryIterator $fileInfo)
    {
        return $fileInfo->isDir() && !$fileInfo->isDot() && stripos(
            $fileInfo->getBasename(),
            '.'
        ) !== 0; // skip dot directories e.g. .git
    }

    /**
     * Merge all given config.yaml files
     *
     * @param  string[] $paths
     * @return string
     */
    private function getMergedConfigs($paths)
    {
        $content = array();

        foreach ($paths as $path) {
            $content[] = file_get_contents($path);
        }

        return implode("\n", $content);
    }

    /**
     * @return mixed
     */
    public function getRepositories()
    {
        return $this->configArray['repositories'];
    }

    /**
     * @param mixed $offset
     *
     * @return bool
     */
    public function offsetExists($offset)
    {
        return isset($this->configArray[$offset]);
    }

    /**
     * @param mixed $offset
     *
     * @return mixed
     */
    public function offsetGet($offset)
    {
        return $this->configArray[$offset];
    }

    /**
     * @param mixed $offset
     * @param mixed $value
     */
    public function offsetSet($offset, $value)
    {
        $this->configArray[$offset] = $value;
    }

    /**
     * @param mixed $offset
     */
    public function offsetUnset($offset)
    {
        unset($this->configArray[$offset]);
    }

    /**
     * Copy config.yaml.dist to the config directory, if the main config file does not exist, yet.
     *
     * @throws \RuntimeException
     */
    private function getMainConfigFile()
    {
        $configFile = $this->pathProvider->getConfigPath() . '/config.yaml';

        // If the main config file exists, use that
        if (file_exists($configFile)) {
            return $configFile;
        }

        // Else use config.yaml.dist
        return $this->pathProvider->getCliToolPath() . '/config.yaml.dist';
    }
}

{'fraction_non_alphanumeric': 0.12857483317445187, 'fraction_numerical': 0.00023832221163012392, 'mean_word_length': 1.9387254901960784, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, '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': '2076185', 'n_tokens_mistral': 2345, 'n_tokens_neox': 2278, 'n_words': 516}