<commit_before><?php

namespace Blablacar\I18nRoutingBundle\Routing\Loader;

use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Routing\Route;

class DefaultPatternGenerationStrategy implements PatternGenerationStrategyInterface
{
    private $translator;
    private $translationDomain;
    private $locales;
    private $cacheDir;

    public function __construct(TranslatorInterface $translator, array $locales, $cacheDir, $translationDomain = 'routes')
    {
        $this->translator        = $translator;
        $this->locales           = $locales;
        $this->cacheDir          = $cacheDir;
        $this->translationDomain = $translationDomain;
    }

    /**
     * {@inheritDoc}
     */
    public function generateI18nPatterns($routeName, Route $route)
    {
        $locales = $route->getOption('i18n_locales') ?: $this->locales;

        $patterns = array();
        foreach ($locales as $locale) {
            // if no translation exists, we use the current pattern
            $i18nPattern = $this->translator->trans($routeName, array(), $this->translationDomain, $locale);
            if ($routeName === $i18nPattern) {
                $i18nPattern = $route->getPattern();
            }

            $patterns[$i18nPattern][] = $locale;
        }

        return $patterns;
    }

    /**
     * {@inheritDoc}
     */
    public function addResources(RouteCollection $i18nCollection)
    {
        foreach ($this->locales as $locale) {
            if (file_exists($metadata = $this->cacheDir.'/translations/catalogue.'.$locale.'.php.meta')) {
                foreach (unserialize(file_get_contents($metadata)) as $resource) {
                    $i18nCollection->addResource($resource);
                }
            }
        }
    }
}<commit_msg>Take into consideration the new routes option.<commit_after><?php

namespace Blablacar\I18nRoutingBundle\Routing\Loader;

use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Routing\Route;

class DefaultPatternGenerationStrategy implements PatternGenerationStrategyInterface
{
    private $translator;
    private $translationDomain;
    private $locales;
    private $cacheDir;

    public function __construct(TranslatorInterface $translator, array $locales, $cacheDir, $translationDomain = 'routes')
    {
        $this->translator        = $translator;
        $this->locales           = $locales;
        $this->cacheDir          = $cacheDir;
        $this->translationDomain = $translationDomain;
    }

    /**
     * {@inheritDoc}
     */
    public function generateI18nPatterns($routeName, Route $route)
    {
        $locales  = $route->getOption('i18n_locales') ?: $this->locales;
        $patterns = array();

        // "routes" option which store all translations for a given route (TODO: required after refacto)
        if (null !== ($routes = $route->getOption('routes')) && !isset($routes['en_GB'])) {
            throw new \InvalidArgumentException(sprintf('The "path" option for the route "%s" must have at least the en_GB translation.', $routeName));
        }

        foreach ($locales as $locale) {
            // if no translation exists, we use the current pattern
            $i18nPattern = $this->translator->trans($routeName, array(), $this->translationDomain, $locale);

            // overload the routes' translations from translations' files by the routes' translations from route' files
            if (null !== $routes) {
                $i18nPattern = (isset($routes[$locale]) ? $routes[$locale] : $routes['en_GB']);
            }

            if ($routeName === $i18nPattern) {
                $i18nPattern = $route->getPattern();
            }

            $patterns[$i18nPattern][] = $locale;
        }

        return $patterns;
    }

    /**
     * {@inheritDoc}
     */
    public function addResources(RouteCollection $i18nCollection)
    {
        foreach ($this->locales as $locale) {
            if (file_exists($metadata = $this->cacheDir.'/translations/catalogue.'.$locale.'.php.meta')) {
                foreach (unserialize(file_get_contents($metadata)) as $resource) {
                    $i18nCollection->addResource($resource);
                }
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.12384898710865562, 'fraction_numerical': 0.008747697974217311, 'mean_word_length': 2.546938775510204, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, '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': '7144669', 'n_tokens_mistral': 1213, 'n_tokens_neox': 1183, 'n_words': 249}