Skip to content

Commit 61b7540

Browse files
authored
Refactor (#87)
1 parent ad4b49e commit 61b7540

File tree

8 files changed

+229
-121
lines changed

8 files changed

+229
-121
lines changed

src/Controllers/FallbackController.php

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,29 @@ class FallbackController extends Controller
2020
*/
2121
public function __invoke()
2222
{
23-
$shouldRedirect = Config::get('localized-routes.redirect_to_localized_urls', false);
23+
return $this->redirectResponse() ?: $this->notFoundResponse();
24+
}
25+
26+
/**
27+
* Return a redirect response if needed.
28+
*
29+
* @return \Illuminate\Http\RedirectResponse|false
30+
*/
31+
protected function redirectResponse()
32+
{
33+
if ( ! $this->shouldRedirect()) {
34+
return false;
35+
}
2436

25-
if ($shouldRedirect) {
26-
$localizedUrl = Route::localizedUrl();
27-
$route = $this->findRouteByUrl($localizedUrl);
37+
$localizedUrl = Route::localizedUrl();
38+
$route = $this->findRouteByUrl($localizedUrl);
2839

29-
if ( ! $route->isFallback) {
30-
return Redirect::to($localizedUrl, Config::get('localized-routes.redirect_status_code', 301))
31-
->header('Cache-Control', 'no-store, no-cache, must-revalidate');
32-
}
40+
if ($route->isFallback) {
41+
return false;
3342
}
3443

35-
return $this->notFoundResponse();
44+
return Redirect::to($localizedUrl, $this->getRedirectStatusCode())
45+
->header('Cache-Control', 'no-store, no-cache, must-revalidate');
3646
}
3747

3848
/**
@@ -42,7 +52,7 @@ public function __invoke()
4252
*
4353
* @return \Illuminate\Routing\Route
4454
*/
45-
protected function findRouteByUrl($url)
55+
protected function findRouteByUrl(string $url)
4656
{
4757
return Collection::make(Route::getRoutes())->first(function ($route) use ($url) {
4858
return $route->matches(Request::create($url));
@@ -64,4 +74,24 @@ protected function notFoundResponse()
6474

6575
abort(404);
6676
}
77+
78+
/**
79+
* Determine if we need to redirect to a localized version of this route.
80+
*
81+
* @return bool
82+
*/
83+
protected function shouldRedirect()
84+
{
85+
return Config::get('localized-routes.redirect_to_localized_urls');
86+
}
87+
88+
/**
89+
* Get the redirect status code from config.
90+
*
91+
* @return int
92+
*/
93+
protected function getRedirectStatusCode()
94+
{
95+
return Config::get('localized-routes.redirect_status_code', 301);
96+
}
6797
}

src/LocaleConfig.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class LocaleConfig
2828
/**
2929
* The configured route action that holds a route's locale.
3030
*
31-
* @var string
31+
* @var string|null
3232
*/
3333
protected $routeAction;
3434

@@ -37,7 +37,7 @@ class LocaleConfig
3737
*
3838
* @param array $config
3939
*/
40-
public function __construct($config = [])
40+
public function __construct(array $config = [])
4141
{
4242
$this->supportedLocales = $config['supported_locales'] ?? [];
4343
$this->omittedLocale = $config['omitted_locale'] ?? null;
@@ -50,7 +50,7 @@ public function __construct($config = [])
5050
*
5151
* @return array
5252
*/
53-
public function getSupportedLocales()
53+
public function getSupportedLocales(): array
5454
{
5555
return $this->supportedLocales;
5656
}
@@ -62,7 +62,7 @@ public function getSupportedLocales()
6262
*
6363
* @return void
6464
*/
65-
public function setSupportedLocales($locales)
65+
public function setSupportedLocales(array $locales): void
6666
{
6767
$this->supportedLocales = $locales;
6868
}
@@ -72,7 +72,7 @@ public function setSupportedLocales($locales)
7272
*
7373
* @return string|null
7474
*/
75-
public function getOmittedLocale()
75+
public function getOmittedLocale(): ?string
7676
{
7777
return $this->omittedLocale;
7878
}
@@ -84,7 +84,7 @@ public function getOmittedLocale()
8484
*
8585
* @return void
8686
*/
87-
public function setOmittedLocale($locale)
87+
public function setOmittedLocale(?string $locale): void
8888
{
8989
$this->omittedLocale = $locale;
9090
}
@@ -94,7 +94,7 @@ public function setOmittedLocale($locale)
9494
*
9595
* @return string|null
9696
*/
97-
public function getFallbackLocale()
97+
public function getFallbackLocale(): ?string
9898
{
9999
return $this->fallbackLocale;
100100
}
@@ -106,39 +106,39 @@ public function getFallbackLocale()
106106
*
107107
* @return void
108108
*/
109-
public function setFallbackLocale($locale)
109+
public function setFallbackLocale(?string $locale): void
110110
{
111111
$this->fallbackLocale = $locale;
112112
}
113113

114114
/**
115115
* Get the route action that holds a route's locale.
116116
*
117-
* @return string
117+
* @return string|null
118118
*/
119-
public function getRouteAction()
119+
public function getRouteAction(): ?string
120120
{
121121
return $this->routeAction;
122122
}
123123

124124
/**
125125
* Set the route action that holds a route's locale.
126126
*
127-
* @param string $locale
127+
* @param string $action
128128
*
129129
* @return string
130130
*/
131-
public function setRouteAction($locale)
131+
public function setRouteAction(string $action): string
132132
{
133-
return $this->routeAction = $locale;
133+
return $this->routeAction = $action;
134134
}
135135

136136
/**
137137
* Get the locales (not the slugs or domains).
138138
*
139139
* @return array
140140
*/
141-
public function getLocales()
141+
public function getLocales(): array
142142
{
143143
$locales = $this->getSupportedLocales();
144144

@@ -156,7 +156,7 @@ public function getLocales()
156156
*
157157
* @return string|null
158158
*/
159-
public function findSlugByLocale($locale)
159+
public function findSlugByLocale(string $locale): ?string
160160
{
161161
if ( ! $this->isSupportedLocale($locale) || $this->hasCustomDomains()) {
162162
return null;
@@ -172,7 +172,7 @@ public function findSlugByLocale($locale)
172172
*
173173
* @return string|null
174174
*/
175-
public function findDomainByLocale($locale)
175+
public function findDomainByLocale(string $locale): ?string
176176
{
177177
if ( ! $this->isSupportedLocale($locale) || ! $this->hasCustomDomains()) {
178178
return null;
@@ -188,7 +188,7 @@ public function findDomainByLocale($locale)
188188
*
189189
* @return string|null
190190
*/
191-
public function findLocaleBySlug($slug)
191+
public function findLocaleBySlug(string $slug): ?string
192192
{
193193
if ($this->hasCustomDomains()) {
194194
return null;
@@ -208,7 +208,7 @@ public function findLocaleBySlug($slug)
208208
*
209209
* @return string|null
210210
*/
211-
public function findLocaleByDomain($domain)
211+
public function findLocaleByDomain(string $domain): ?string
212212
{
213213
if ( ! $this->hasCustomDomains()) {
214214
return null;
@@ -222,7 +222,7 @@ public function findLocaleByDomain($domain)
222222
*
223223
* @return bool
224224
*/
225-
public function hasLocales()
225+
public function hasLocales(): bool
226226
{
227227
return count($this->getSupportedLocales()) > 0;
228228
}
@@ -233,7 +233,7 @@ public function hasLocales()
233233
*
234234
* @return bool
235235
*/
236-
public function hasSimpleLocales()
236+
public function hasSimpleLocales(): bool
237237
{
238238
return is_numeric(key($this->getSupportedLocales()));
239239
}
@@ -243,7 +243,7 @@ public function hasSimpleLocales()
243243
*
244244
* @return bool
245245
*/
246-
public function hasCustomSlugs()
246+
public function hasCustomSlugs(): bool
247247
{
248248
return $this->hasLocales() && ! $this->hasSimpleLocales() && ! $this->hasCustomDomains();
249249
}
@@ -253,7 +253,7 @@ public function hasCustomSlugs()
253253
*
254254
* @return bool
255255
*/
256-
public function hasCustomDomains()
256+
public function hasCustomDomains(): bool
257257
{
258258
$firstValue = array_values($this->getSupportedLocales())[0] ?? '';
259259
$containsDot = strpos($firstValue, '.') !== false;
@@ -264,11 +264,11 @@ public function hasCustomDomains()
264264
/**
265265
* Check if the given locale is supported.
266266
*
267-
* @param string $locale
267+
* @param string|null $locale
268268
*
269269
* @return bool
270270
*/
271-
public function isSupportedLocale($locale)
271+
public function isSupportedLocale(?string $locale): bool
272272
{
273273
return in_array($locale, $this->getLocales());
274274
}

src/LocalizedRoutesRegistrar.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CodeZero\LocalizedRoutes;
44

5+
use Closure;
56
use CodeZero\LocalizedRoutes\Facades\LocaleConfig;
67
use Illuminate\Support\Facades\App;
78
use Illuminate\Support\Facades\Route;
@@ -16,7 +17,7 @@ class LocalizedRoutesRegistrar
1617
*
1718
* @return void
1819
*/
19-
public function register($closure, $options = [])
20+
public function register(Closure $closure, array $options = []): void
2021
{
2122
$locales = $options['supported_locales'] ?? LocaleConfig::getSupportedLocales();
2223
$omittedLocale = $options['omitted_locale'] ?? LocaleConfig::getOmittedLocale();
@@ -90,7 +91,7 @@ public function register($closure, $options = [])
9091
*
9192
* @return array
9293
*/
93-
protected function moveOmittedLocaleToEnd($locales, $omittedLocale, $usingDomains, $usingCustomSlugs)
94+
protected function moveOmittedLocaleToEnd(array $locales, ?string $omittedLocale, bool $usingDomains, bool $usingCustomSlugs): array
9495
{
9596
if ( ! $omittedLocale || $usingDomains) {
9697
return $locales;

0 commit comments

Comments
 (0)