<commit_before><?php

namespace VGMdb\Component\Silex\Loader;

use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Yaml\Yaml;

/**
 * YamlFileLoader loads YAML files service definitions.
 *
 * @author <NAME> <<EMAIL>>
 */
class YamlFileLoader extends FileLoader
{
    protected $replacements = array();

    /**
     * Loads a Yaml file.
     *
     * @param mixed  $file The resource
     * @param string $type The resource type
     */
    public function load($file, $type = null)
    {
        $path = $this->locator->locate($file);

        $content = $this->loadFile($path);

        //$this->container->addResource(new FileResource($path));

        // empty file
        if (null === $content) {
            return;
        }

        // imports
        $this->parseImports($content, $file);

        // parameters
        $this->parseParameters($content);

        // services
        $this->parseDefinitions($content, $file);

        // extensions
        $this->loadFromExtensions($content);
    }

    /**
     * Returns true if this class supports the given resource.
     *
     * @param mixed  $resource A resource
     * @param string $type     The resource type
     *
     * @return Boolean true if this class supports the given resource, false otherwise
     */
    public function supports($resource, $type = null)
    {
        return true;
    }

    /**
     * Parses all imports
     *
     * @param array  $content
     * @param string $file
     */
    private function parseImports($content, $file)
    {
        if (!isset($content['imports'])) {
            return;
        }

        foreach ($content['imports'] as $import) {
            $resource = $this->doReplacements($import['resource'], $this->replacements);
            $this->setCurrentDir(dirname($file));
            $this->import($resource, null, isset($import['ignore_errors']) ? (Boolean) $import['ignore_errors'] : false, $file);
        }
    }

    /**
     * Parses parameters
     *
     * @param array $content
     */
    private function parseParameters($content)
    {
        if (!isset($content['parameters'])) {
            return;
        }

        foreach ($content['parameters'] as $key => $value) {
            $this->options['parameters'][$key] = $value;
        }

        foreach ($this->options['parameters'] as $key => $value) {
            $this->replacements['%' . $key . '%'] = $value;
        }
    }

    /**
     * Parses definitions
     *
     * @param array  $content
     * @param string $file
     */
    private function parseDefinitions($content, $file)
    {
        if (!isset($content['services'])) {
            return;
        }

        foreach ($content['services'] as $id => $service) {
            $this->parseDefinition($id, $service, $file);
        }
    }

    /**
     * Parses a definition.
     *
     * @param string $id
     * @param array  $service
     * @param string $file
     *
     * @throws InvalidArgumentException When tags are invalid
     */
    private function parseDefinition($id, $service, $file)
    {
        $class = null;

        if (isset($service['provider'])) {
            $class = $service['provider'];
            unset($service['provider']);
        }

        $parameters = array();
        foreach ($service as $key => $value) {
            $parameters[$id . '.' . $key] = $this->doReplacements($value, $this->replacements);
        }

        if (null !== $class) {
            $this->app->register(new $class(), $parameters);
        } else {
            $this->replaceConfig($this->app, $parameters, array());
        }
    }

    /**
     * Loads from Extensions
     *
     * @param array $content
     */
    private function loadFromExtensions($content)
    {
        unset($content['imports']);
        unset($content['parameters']);
        unset($content['services']);

        $this->replaceConfig($this->app, $content, $this->replacements);
    }

    /**
     * Loads a YAML file.
     *
     * @param string $file
     *
     * @return array The file content
     */
    protected function loadFile($file)
    {
        return $this->validate(Yaml::parse($file), $file);
    }

    /**
     * Validates a YAML file.
     *
     * @param mixed  $content
     * @param string $file
     *
     * @return array
     *
     * @throws InvalidArgumentException When service file is not valid
     */
    private function validate($content, $file)
    {
        if (null === $content) {
            return $content;
        }

        if (!is_array($content)) {
            throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
        }

        return $content;
    }
}
<commit_msg>Fix for proper loading precedence<commit_after><?php

namespace VGMdb\Component\Silex\Loader;

use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Yaml\Yaml;

/**
 * YamlFileLoader loads YAML files service definitions.
 *
 * @author <NAME> <<EMAIL>>
 */
class YamlFileLoader extends FileLoader
{
    protected $replacements = array();

    /**
     * Loads a Yaml file.
     *
     * @param mixed  $file The resource
     * @param string $type The resource type
     */
    public function load($file, $type = null)
    {
        $path = $this->locator->locate($file);

        $content = $this->loadFile($path);

        //$this->container->addResource(new FileResource($path));

        // empty file
        if (null === $content) {
            return;
        }

        // imports
        $this->parseImports($content, $file);

        // parameters
        $this->parseParameters($content);

        // services
        $this->parseDefinitions($content, $file);

        // extensions
        $this->loadFromExtensions($content);
    }

    /**
     * Returns true if this class supports the given resource.
     *
     * @param mixed  $resource A resource
     * @param string $type     The resource type
     *
     * @return Boolean true if this class supports the given resource, false otherwise
     */
    public function supports($resource, $type = null)
    {
        return true;
    }

    /**
     * Parses all imports
     *
     * @param array  $content
     * @param string $file
     */
    private function parseImports($content, $file)
    {
        if (!isset($content['imports'])) {
            return;
        }

        foreach ($content['imports'] as $import) {
            $resource = $this->doReplacements($import['resource'], $this->replacements);
            //$this->setCurrentDir(dirname($file));
            $this->import($resource, null, isset($import['ignore_errors']) ? (Boolean) $import['ignore_errors'] : false, $file);
        }
    }

    /**
     * Parses parameters
     *
     * @param array $content
     */
    private function parseParameters($content)
    {
        if (!isset($content['parameters'])) {
            return;
        }

        foreach ($content['parameters'] as $key => $value) {
            $this->options['parameters'][$key] = $value;
        }

        foreach ($this->options['parameters'] as $key => $value) {
            $this->replacements['%' . $key . '%'] = $value;
        }
    }

    /**
     * Parses definitions
     *
     * @param array  $content
     * @param string $file
     */
    private function parseDefinitions($content, $file)
    {
        if (!isset($content['services'])) {
            return;
        }

        foreach ($content['services'] as $id => $service) {
            $this->parseDefinition($id, $service, $file);
        }
    }

    /**
     * Parses a definition.
     *
     * @param string $id
     * @param array  $service
     * @param string $file
     *
     * @throws InvalidArgumentException When tags are invalid
     */
    private function parseDefinition($id, $service, $file)
    {
        $class = null;

        if (isset($service['provider'])) {
            $class = $service['provider'];
            unset($service['provider']);
        }

        $parameters = array();
        foreach ($service as $key => $value) {
            $parameters[$id . '.' . $key] = $this->doReplacements($value, $this->replacements);
        }

        if (null !== $class) {
            $this->app->register(new $class(), $parameters);
        } else {
            $this->replaceConfig($this->app, $parameters, array());
        }
    }

    /**
     * Loads from Extensions
     *
     * @param array $content
     */
    private function loadFromExtensions($content)
    {
        unset($content['imports']);
        unset($content['parameters']);
        unset($content['services']);

        $this->replaceConfig($this->app, $content, $this->replacements);
    }

    /**
     * Loads a YAML file.
     *
     * @param string $file
     *
     * @return array The file content
     */
    protected function loadFile($file)
    {
        return $this->validate(Yaml::parse($file), $file);
    }

    /**
     * Validates a YAML file.
     *
     * @param mixed  $content
     * @param string $file
     *
     * @return array
     *
     * @throws InvalidArgumentException When service file is not valid
     */
    private function validate($content, $file)
    {
        if (null === $content) {
            return $content;
        }

        if (!is_array($content)) {
            throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
        }

        return $content;
    }
}

{'fraction_non_alphanumeric': 0.14602236758809875, 'fraction_numerical': 0.0, 'mean_word_length': 1.9873936337850615, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 71, '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': '7798', 'n_tokens_mistral': 2685, 'n_tokens_neox': 2629, 'n_words': 626}