Skip to content

Commit

Permalink
Prevent unwanted path generation with trailing slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérôme Poskin committed Jun 2, 2015
1 parent 9021aa8 commit f51da89
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
7 changes: 4 additions & 3 deletions Routing/I18nAnnotatedRouteControllerLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ protected function addRoute(
if($i18n) {
$i18nAnnot->setName($this->helper->alterName($i18nAnnot->getName(), $locale));
$i18nAnnot->setPath($this->helper->alterPath($i18nAnnot->getPath(), $locale));
$i18nAnnot->setDefaults($this->helper->alterdefaults($i18nAnnot->getDefaults(), $locale));
$i18nAnnot->setDefaults($this->helper->alterDefaults($i18nAnnot->getDefaults(), $locale));

if (isset($globals['path']) && !empty($globals['path'])) {
$globals['path'] = $this->helper->alterPath($globals['path'], $locale);
$globals['path'] = rtrim('/' . $locale . '/' . ltrim($globals['path'], '/'), '/');
$globals['path'] = '/' . $locale . '/' . ltrim($this->helper->alterPath($globals['path'], $locale), '/');
} else {
$globals['path'] = '/' . $locale;
}
}

Expand Down
6 changes: 2 additions & 4 deletions Routing/I18nLoaderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,15 @@ public function alterName($name, $locale)
*/
public function alterPath($path, $locale)
{
$translatedPath = $this->translator->trans($path, array(), $this->translationDomain, $locale);

return '/' . trim( $translatedPath, '/');
return $this->translator->trans($path, array(), $this->translationDomain, $locale);
}

/**
* @param array $defaults
* @param string $locale
* @return array
*/
public function alterdefaults(array $defaults, $locale)
public function alterDefaults(array $defaults, $locale)
{
return array_merge($defaults, array('_locale' => $locale));
}
Expand Down
16 changes: 11 additions & 5 deletions Routing/I18nYamlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class I18nYamlFileLoader extends YamlFileLoader {

/**
* @param FileLocatorInterface $locator
* @param array $locales
* @param string $translationDomain
* @param I18nLoaderHelper $helper
* @param Registry $registry
*/
public function __construct(FileLocatorInterface $locator, I18nLoaderHelper $helper, Registry $registry)
{
Expand All @@ -34,14 +34,20 @@ public function __construct(FileLocatorInterface $locator, I18nLoaderHelper $hel

/**
* @param mixed $resource
* @param null $type
* @param string|null $type
* @return bool
*/
public function supports($resource, $type = null)
{
return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'yaml_i18n' === $type);
}

/**
* @param RouteCollection $collection
* @param string $name
* @param array $config
* @param string $path
*/
protected function parseRoute(RouteCollection $collection, $name, array $config, $path)
{
$defaults = isset($config['defaults']) ? $config['defaults'] : array();
Expand All @@ -56,8 +62,8 @@ protected function parseRoute(RouteCollection $collection, $name, array $config,
$route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods);

if($i18n) {
$route->setPath('/' . $locale . $this->helper->alterPath($config['path'], $locale));
$route->setDefaults($this->helper->alterdefaults($defaults, $locale));
$route->setPath('/' . $locale . '/' . ltrim($this->helper->alterPath($config['path'], $locale), '/'));
$route->setDefaults($this->helper->alterDefaults($defaults, $locale));
}

$i18nName = $i18n ? $this->helper->alterName($name, $locale) : $name;
Expand Down

0 comments on commit f51da89

Please sign in to comment.