Skip to content

Commit

Permalink
Merge branch 'master' into 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaxs committed Aug 26, 2016
2 parents 2c7ed96 + de082b8 commit 0ece399
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/EventListener/LocaleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function resolveLocale(GetResponseEvent $event)
}

/**
* Return host
* Return exclude routes reg
* @return string
*/
protected function getExcludeRoutesReg()
Expand Down
40 changes: 36 additions & 4 deletions src/Twig/LocaleExtension.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Pmaxs\Silex\Locale\Twig;

use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Pmaxs\Silex\Locale\Utils\UrlGenerator;

/**
Expand Down Expand Up @@ -36,6 +37,9 @@ public function __construct(UrlGenerator $generator, $locales)
public function getFunctions()
{
return [
new \Twig_SimpleFunction('generate', [$this, 'generate']),
new \Twig_SimpleFunction('locale_get_url', [$this, 'getUrl']),
new \Twig_SimpleFunction('locale_get_url_for_locale', [$this, 'getUrlForLocale']),
new \Twig_SimpleFunction('locale_get_index_url', [$this, 'getIndexUrl']),
new \Twig_SimpleFunction('locale_get_index_url_for_locale', [$this, 'getIndexUrlForLocale']),
];
Expand All @@ -51,23 +55,51 @@ public function getGlobals()
];
}

/**
* Returns url for route
* @return string url
*/
public function generate($locale, $name, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
{
return $this->generator->generate($locale, $name, $parameters, $referenceType);
}

/**
* Returns url for current locale
* @return string url
*/
public function getUrl($url, $absolute = false)
{
return $this->generator->getUrl($url, $absolute);
}

/**
* Returns url for locale
* @param string $locale locale
* @return string url
*/
public function getUrlForLocale($url, $locale, $absolute = false)
{
return $this->generator->getUrlForLocale($url, $locale, $absolute);
}

/**
* Returns index url for current locale
* @return string url
*/
public function getIndexUrl()
public function getIndexUrl($absolute = false)
{
return $this->generator->getIndexUrl();
return $this->generator->getIndexUrl($absolute);
}

/**
* Returns index url for locale
* @param string $locale locale
* @return string url
*/
public function getIndexUrlForLocale($locale)
public function getIndexUrlForLocale($locale, $absolute = false)
{
return $this->generator->getIndexUrlForLocale($locale);
return $this->generator->getIndexUrlForLocale($locale, $absolute);
}

/**
Expand Down
94 changes: 87 additions & 7 deletions src/Utils/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,71 @@ public function __construct(UrlGeneratorInterface $generator, $locales, $default
$this->fake_index_route = $fake_index_route;
}

/**
* Returns url for route
* @param $locale
* @param $name
* @param array $parameters
* @param int $referenceType
* @return string url
*/
public function generate($locale, $name, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
{
if ($locale != $this->default_locale) {
$parameters['locale'] = $locale . '/';
}

return $this->generator->generate($name, $parameters, $referenceType);
}

public function getUrl($url, $absolute = false)
{
return $this->getUrlForLocale($url, $this->getLocale(), $absolute);
}

public function getUrlForLocale($url, $locale, $absolute = false)
{
$url_parts = parse_url($url);

if (!empty($url_parts['scheme'])){
return $url;
}

if ($this->resolve_by_host) {
if (!$absolute && $locale == $this->getLocale()) {
return $url;
}

$url = ''
.$this->getScheme().'://'
.($locale != $this->default_locale ? $locale . '.' : '')
.$this->getHost() . '/'
.$url;

return $url;

} else {
if (!strlen($url_parts['path']) && !strlen($url_parts['query'])) {
return $url;
}

if (!preg_match('~^/?(' . $this->getLocalesReg() . ')(/|$)~', $url)) {
$url = '/'
.($locale != $this->default_locale ? $locale . '/' : '')
.ltrim($url, '/');
}

if ($absolute) {
$url = ''
.$this->getScheme().'://'
.$this->getHost() . '/'
.$url;
}

return $url;
}
}

/**
* Returns index url for current locale
* @param boolean $absolute absolute url
Expand All @@ -73,24 +138,25 @@ public function getIndexUrl($absolute = false)
*/
public function getIndexUrlForLocale($locale, $absolute = false)
{
if ($locale == $this->default_locale) $locale = '';

if ($this->resolve_by_host) {
$url = ''
.$this->getScheme().'://'
.($locale ? $locale . '.' : '')
.($locale != $this->default_locale ? $locale . '.' : '')
.$this->getHost() . '/';

return $url;

} else {
$url = $this->generator->generate(
$this->fake_index_route,
['locale0' => $locale],
['locale0' => $locale != $this->default_locale ? $locale : ''],
$absolute ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH
);

$url = rtrim($url, '/') . '/';
}

return $url;
return $url;
}
}

/**
Expand All @@ -113,7 +179,7 @@ protected function getHost()
if (isset($host)) return $host;

$host = $this->generator->getContext()->getHost();
$host = preg_replace('~(' . implode('|', $this->locales) . ')\\.~i', '', $host);
$host = preg_replace('~(' . $this->getLocalesReg() . ')\\.~i', '', $host);

return $host;
}
Expand All @@ -127,4 +193,18 @@ protected function getLocale()
return $this->generator->getContext()->getParameter('_locale');
}

/**
* Return locales reg
* @return string
*/
protected function getLocalesReg()
{
static $locales_reg;

if (isset($locales_reg)) return $locales_reg;

$locales_reg = implode('|', $this->locales);

return $locales_reg;
}
}

0 comments on commit 0ece399

Please sign in to comment.