Skip to content

Commit

Permalink
refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
eborges78 committed Apr 19, 2018
1 parent 3400195 commit 0af9fe2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
34 changes: 17 additions & 17 deletions Services/CustomRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ public function generate($route, $params = array(), $referenceType = UrlGenerato
$this->request = $this->container->get('request_stack')->getMasterRequest();
}
if ($this->request !== null) {
$market = $this->request->attributes->get('market');
$locale = $this->request->attributes->get('market');
if (\in_array($route, $this->routes, true)) {
return parent::generate($route, $params, $referenceType);
}
if (\in_array($route.'.'.$market, $this->routes, true)) {
return parent::generate($route.'.'.$market, $params, $referenceType);
if (\in_array($route.'.'.$locale, $this->routes, true)) {
return parent::generate($route.'.'.$locale, $params, $referenceType);
}

$end = substr($route, \strlen($route) - \strlen($market), \strlen($market));
if ($end === $market) {
$route = substr($route, 0, \strlen($route)-\strlen($market) - 1);
$end = substr($route, \strlen($route) - \strlen($locale), \strlen($locale));
if ($end === $locale) {
$route = substr($route, 0, \strlen($route)-\strlen($locale) - 1);
}
}

Expand All @@ -95,22 +95,22 @@ public function getRouteCollection()
);

// Find config for available locales
$availableMarkets = [];
$availableLocales = [];
$list = $this->container->getParameter('available_locales');
foreach ($list as $market) {
if ($this->container->hasParameter('i18n_' . $market)) {
$config = $this->container->getParameter('i18n_'.$market);
$availableMarkets[$market] = $config;
foreach ($list as $locale) {
if ($this->container->hasParameter('i18n_' . $locale)) {
$config = $this->container->getParameter('i18n_'.$locale);
$availableLocales[$locale] = $config;
}
}

// filter routes ( _ and exceptions )
$this->collection->addCollection($this->filterRoutes($availableMarkets));
$this->collection->addCollection($this->filterRoutes($availableLocales));

$defaultLocale = $this->container->getParameter('default_locale');
// Duplicate other routes for alternative markets
// Duplicate other routes for alternative locales
foreach ($this->defaultCollection as $key => $route) {
foreach ($availableMarkets as $market => $config) {
foreach ($availableLocales as $locale => $config) {
// find route into config
$prefix = $config['prefix'];
$configuredLocale = $config['locale'];
Expand Down Expand Up @@ -169,15 +169,15 @@ private function removeRoutes()
}

/**
* @param array $availableMarkets
* @param array $availableLocales
* @return RouteCollection
*/
private function filterRoutes(array $availableMarkets = []): RouteCollection
private function filterRoutes(array $availableLocales = []): RouteCollection
{
$tmpCollection = new RouteCollection();
foreach ($this->defaultCollection as $key => $route) {
if ($key[0] === '_' || stripos($key, 'pages_exceptions') === 0) {
foreach ($availableMarkets as $config) {
foreach ($availableLocales as $config) {
// find route into config
$configuredLocale = $config['locale'];
$parts = explode('_', $configuredLocale);
Expand Down
10 changes: 5 additions & 5 deletions Subscriber/RouterSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public function beforeRouter(GetResponseEvent $event)
{
$request = $event->getRequest();
if ($event->isMasterRequest()) {
$currentMarket = explode('.', $request->attributes->get('_route'));
$market = @end($currentMarket);
if ($this->container->hasParameter('i18n_'.$market)) {
return $this->persistLocaleParameters($request, 'i18n_'.$market);
$currentLocale = explode('.', $request->attributes->get('_route'));
$locale = @end($currentLocale);
if ($this->container->hasParameter('i18n_'.$locale)) {
return $this->persistLocaleParameters($request, 'i18n_'.$locale);
}
if ($this->container->hasParameter('default_locale')) {
$name = 'i18n_'.$this->container->getParameter('default_locale');
Expand Down Expand Up @@ -95,7 +95,7 @@ protected function persistLocaleParameters(Request $request, string $name): arra
$prefix = (string)$configuration['prefix'];
list($locale, $country) = explode('_', $configuredLocale);

// Market is a LCID string ( couple of locale and country like en-gb )
// market is a couple of locale and country like en-gb )
$request->attributes->set('market', str_replace('_', '-', strtolower($configuredLocale)));
$request->attributes->set('locale', $locale);
$request->attributes->set('hasPrefix', !empty($prefix));
Expand Down
12 changes: 6 additions & 6 deletions Twig/CustomRoutingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class CustomRoutingExtension extends RoutingExtension
/** @var ContainerInterface */
private $container;

/** Default market ( market is a couple of locale and country ) */
const DEFAULT_MARKET = 'fr-fr';
/** Default locale */
const DEFAULT_LOCALE = 'fr-fr';

/**
* CustomRoutingExtension constructor.
Expand All @@ -41,17 +41,17 @@ public function getPath($name, $parameters = array(), $relative = false)
{
try {
if (false === strpos($name, '_')) {
$market = self::DEFAULT_MARKET;
$locale = self::DEFAULT_LOCALE;
if ($this->container !== null && $this->container->hasParameter('default_locale')) {
$market = $this->container->getParameter('default_locale');
$locale = $this->container->getParameter('default_locale');
}
if ($this->masterRequest !== null &&
$this->masterRequest->attributes !== null &&
$this->masterRequest->attributes->has('market')
) {
$market = $this->masterRequest->attributes->get('market');
$locale = $this->masterRequest->attributes->get('market');
}
return parent::getPath($name.'.'.$market, $parameters, $relative);
return parent::getPath($name.'.'.$locale, $parameters, $relative);
}
} catch (\Exception $e) {
}
Expand Down

0 comments on commit 0af9fe2

Please sign in to comment.