/** * {@inheritDoc} */ public function generateI18nPatterns($routeName, Route $route) { $patterns = array(); foreach ($route->getOption('i18n_locales') ?: $this->locales as $locale) { // Check if translation exists in the translation catalogue to avoid errors being logged by // the new LoggingTranslator of Symfony 2.6. However, the LoggingTranslator did not implement // the interface until Symfony 2.6.5, so an extra check is needed. if ($this->translator instanceof TranslatorBagInterface || $this->translator instanceof LoggingTranslator) { // Check if route is translated. if (!$this->translator->getCatalogue($locale)->has($routeName, $this->translationDomain)) { // No translation found. $i18nPattern = $route->getPattern(); } else { // Get translation. $i18nPattern = $this->translator->trans($routeName, array(), $this->translationDomain, $locale); } } else { // if no translation exists, we use the current pattern if ($routeName === ($i18nPattern = $this->translator->trans($routeName, array(), $this->translationDomain, $locale))) { $i18nPattern = $route->getPattern(); } } // prefix with locale if requested if (self::STRATEGY_PREFIX === $this->strategy || self::STRATEGY_PREFIX_EXCEPT_DEFAULT === $this->strategy && $this->defaultLocale !== $locale) { $i18nPattern = '/' . $locale . $i18nPattern; if (null !== $route->getOption('i18n_prefix')) { $i18nPattern = $route->getOption('i18n_prefix') . $i18nPattern; } } $patterns[$i18nPattern][] = $locale; } return $patterns; }
public function testPattern() { $route = new Route('/{foo}'); $route->setPattern('/{bar}'); $this->assertEquals('/{bar}', $route->getPattern(), '->setPattern() sets the pattern'); $route->setPattern(''); $this->assertEquals('/', $route->getPattern(), '->setPattern() adds a / at the beginning of the pattern if needed'); $route->setPattern('bar'); $this->assertEquals('/bar', $route->getPattern(), '->setPattern() adds a / at the beginning of the pattern if needed'); $this->assertEquals($route, $route->setPattern(''), '->setPattern() implements a fluent interface'); }
public function testPattern() { $route = new Route('/{foo}'); $route->setPattern('/{bar}'); $this->assertEquals('/{bar}', $route->getPattern(), '->setPattern() sets the pattern'); $route->setPattern(''); $this->assertEquals('/', $route->getPattern(), '->setPattern() adds a / at the beginning of the pattern if needed'); $route->setPattern('bar'); $this->assertEquals('/bar', $route->getPattern(), '->setPattern() adds a / at the beginning of the pattern if needed'); $this->assertEquals($route, $route->setPattern(''), '->setPattern() implements a fluent interface'); $route->setPattern('//path'); $this->assertEquals('/path', $route->getPattern(), '->setPattern() does not allow two slahes "//" at the beginning of the pattern as it would be confused with a network path when generating the path from the route'); }
/** * {@inheritDoc} */ public function generateI18nPatterns($routeName, Route $route) { $patterns = array(); foreach ($route->getOption('i18n_locales') ?: $this->locales as $locale) { // if no translation exists, we use the current pattern if ($routeName === ($i18nPattern = $this->translator->trans($routeName, array(), $this->translationDomain, $locale))) { $i18nPattern = $route->getPattern(); } // prefix with locale if requested if (self::STRATEGY_PREFIX === $this->strategy || self::STRATEGY_PREFIX_EXCEPT_DEFAULT === $this->strategy && $this->defaultLocale !== $locale) { $i18nPattern = '/' . $locale . $i18nPattern; } $patterns[$i18nPattern][] = $locale; } return $patterns; }
/** * {@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[$this->defaultLocale])) { throw new \InvalidArgumentException(sprintf('The "path" option for the route "%s" must have at least the %s translation.', $routeName, $this->defaultLocale)); } 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[$this->defaultLocale]; } if ($routeName === $i18nPattern) { $i18nPattern = $route->getPattern(); } $patterns[$i18nPattern][] = $locale; } return $patterns; }
public function testPattern() { $route = new Route('/{foo}'); $this->assertEquals('/{foo}', $route->getPattern()); $route->setPattern('/bar'); $this->assertEquals('/bar', $route->getPattern()); }
public function testLegacyPattern() { $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED); $route = new Route('/{foo}'); $this->assertEquals('/{foo}', $route->getPattern()); $route->setPattern('/bar'); $this->assertEquals('/bar', $route->getPattern()); }
/** * Compiles the current route instance. * * @param Route $route A Route instance * * @return CompiledRoute A CompiledRoute instance */ public function compile(Route $route) { $pattern = $route->getPattern(); $len = strlen($pattern); $tokens = array(); $variables = array(); $pos = 0; preg_match_all('#.\{([\w\d_]+)\}#', $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); foreach ($matches as $match) { if ($text = substr($pattern, $pos, $match[0][1] - $pos)) { $tokens[] = array('text', $text); } $seps = array($pattern[$pos]); $pos = $match[0][1] + strlen($match[0][0]); $var = $match[1][0]; if ($req = $route->getRequirement($var)) { $regexp = $req; } else { if ($pos !== $len) { $seps[] = $pattern[$pos]; } $regexp = sprintf('[^%s]+?', preg_quote(implode('', array_unique($seps)), '#')); } $tokens[] = array('variable', $match[0][0][0], $regexp, $var); $variables[] = $var; } if ($pos < $len) { $tokens[] = array('text', substr($pattern, $pos)); } // find the first optional token $firstOptional = INF; for ($i = count($tokens) - 1; $i >= 0; $i--) { if ('variable' === $tokens[$i][0] && $route->hasDefault($tokens[$i][3])) { $firstOptional = $i; } else { break; } } // compute the matching regexp $regex = ''; $indent = 1; if (1 === count($tokens) && 0 === $firstOptional) { $token = $tokens[0]; ++$indent; $regex .= str_repeat(' ', $indent * 4).sprintf("%s(?:\n", preg_quote($token[1], '#')); $regex .= str_repeat(' ', $indent * 4).sprintf("(?P<%s>%s)\n", $token[3], $token[2]); } else { foreach ($tokens as $i => $token) { if ('text' === $token[0]) { $regex .= str_repeat(' ', $indent * 4).preg_quote($token[1], '#')."\n"; } else { if ($i >= $firstOptional) { $regex .= str_repeat(' ', $indent * 4)."(?:\n"; ++$indent; } $regex .= str_repeat(' ', $indent * 4).sprintf("%s(?P<%s>%s)\n", preg_quote($token[1], '#'), $token[3], $token[2]); } } } while (--$indent) { $regex .= str_repeat(' ', $indent * 4).")?\n"; } return new CompiledRoute( 'text' === $tokens[0][0] ? $tokens[0][1] : '', sprintf("#^\n%s#x", $regex), array_reverse($tokens), $variables ); }
/** * @param Route $route */ public function setRoute(Route $route) { $this->route = $route; $this->uri = $route->getPattern(); $this->method = $route->getRequirement('_method') ?: 'ANY'; }