From 034809cd0b6bc4719fa50434c04ddd68247fbb6e Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Thu, 24 Sep 2015 15:41:05 +0100 Subject: [PATCH] Refactoring Localization --- src/Bases/Middleware.php | 29 ++++-------------------- src/Contracts/LocalizationInterface.php | 8 ++++--- src/Localization.php | 10 +++++--- src/Middleware/LocaleSessionRedirect.php | 2 +- src/Middleware/LocalizationRoutes.php | 5 ++-- 5 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/Bases/Middleware.php b/src/Bases/Middleware.php index 31be1c2..34cb809 100644 --- a/src/Bases/Middleware.php +++ b/src/Bases/Middleware.php @@ -2,7 +2,6 @@ use Arcanedev\Localization\Entities\LocaleCollection; use Arcanedev\Localization\Exceptions\UndefinedSupportedLocalesException; -use Arcanedev\Localization\Localization; use Arcanedev\Support\Bases\Middleware as BaseMiddleware; use Illuminate\Http\RedirectResponse; @@ -14,26 +13,6 @@ */ abstract class Middleware extends BaseMiddleware { - /* ------------------------------------------------------------------------------------------------ - | Properties - | ------------------------------------------------------------------------------------------------ - */ - /** - * The localization instance. - * - * @var Localization - */ - protected $localization; - - /* ------------------------------------------------------------------------------------------------ - | Constructor - | ------------------------------------------------------------------------------------------------ - */ - public function __construct() - { - $this->localization = localization(); - } - /* ------------------------------------------------------------------------------------------------ | Getters & Setters | ------------------------------------------------------------------------------------------------ @@ -45,7 +24,7 @@ public function __construct() */ public function getDefaultLocale() { - return $this->localization->getDefaultLocale(); + return localization()->getDefaultLocale(); } /** @@ -55,7 +34,7 @@ public function getDefaultLocale() */ public function getCurrentLocale() { - return $this->localization->getCurrentLocale(); + return localization()->getCurrentLocale(); } /** @@ -67,7 +46,7 @@ public function getCurrentLocale() */ public function getSupportedLocales() { - return $this->localization->getSupportedLocales(); + return localization()->getSupportedLocales(); } /** @@ -77,7 +56,7 @@ public function getSupportedLocales() */ protected function hideDefaultLocaleInURL() { - return $this->localization->isDefaultLocaleHiddenInUrl(); + return localization()->isDefaultLocaleHiddenInUrl(); } /* ------------------------------------------------------------------------------------------------ diff --git a/src/Contracts/LocalizationInterface.php b/src/Contracts/LocalizationInterface.php index 1d4421a..f20c1cf 100644 --- a/src/Contracts/LocalizationInterface.php +++ b/src/Contracts/LocalizationInterface.php @@ -109,11 +109,13 @@ public function setLocale($locale = null); public function setBaseUrl($url); /** - * Set current route name. + * Set route name from path. * - * @param false|string $routeName + * @param string $uri + * + * @return self */ - public function setRouteName($routeName); + public function setRouteNameFromCurrentUri($uri); /* ------------------------------------------------------------------------------------------------ | Main Functions diff --git a/src/Localization.php b/src/Localization.php index 54735a6..b684995 100644 --- a/src/Localization.php +++ b/src/Localization.php @@ -239,12 +239,16 @@ public function setBaseUrl($url) } /** - * Set current route name. + * Set route name from path. * - * @param false|string $routeName + * @param string $uri + * + * @return self */ - public function setRouteName($routeName) + public function setRouteNameFromCurrentUri($uri) { + $routeName = $this->getRouteNameFromPath($uri); + $this->routeTranslator->setCurrentRoute($routeName); } diff --git a/src/Middleware/LocaleSessionRedirect.php b/src/Middleware/LocaleSessionRedirect.php index 7e2f781..3e8f617 100644 --- a/src/Middleware/LocaleSessionRedirect.php +++ b/src/Middleware/LocaleSessionRedirect.php @@ -30,7 +30,7 @@ public function handle(Request $request, Closure $next) { $locale = $request->segment(1, null); - if ($this->localization->isLocaleSupported($locale)) { + if (localization()->isLocaleSupported($locale)) { session()->put(compact('locale')); return $next($request); diff --git a/src/Middleware/LocalizationRoutes.php b/src/Middleware/LocalizationRoutes.php index 4edc571..203875e 100644 --- a/src/Middleware/LocalizationRoutes.php +++ b/src/Middleware/LocalizationRoutes.php @@ -26,10 +26,9 @@ class LocalizationRoutes extends Middleware */ public function handle(Request $request, Closure $next) { - $localization = localization(); - $routeName = $localization->getRouteNameFromPath($request->getUri()); + $currentUri = $request->getUri(); - $localization->setRouteName($routeName); + localization()->setRouteNameFromCurrentUri($currentUri); return $next($request); }