Skip to content

Refactor #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions src/Controllers/FallbackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,29 @@ class FallbackController extends Controller
*/
public function __invoke()
{
$shouldRedirect = Config::get('localized-routes.redirect_to_localized_urls', false);
return $this->redirectResponse() ?: $this->notFoundResponse();
}

/**
* Return a redirect response if needed.
*
* @return \Illuminate\Http\RedirectResponse|false
*/
protected function redirectResponse()
{
if ( ! $this->shouldRedirect()) {
return false;
}

if ($shouldRedirect) {
$localizedUrl = Route::localizedUrl();
$route = $this->findRouteByUrl($localizedUrl);
$localizedUrl = Route::localizedUrl();
$route = $this->findRouteByUrl($localizedUrl);

if ( ! $route->isFallback) {
return Redirect::to($localizedUrl, Config::get('localized-routes.redirect_status_code', 301))
->header('Cache-Control', 'no-store, no-cache, must-revalidate');
}
if ($route->isFallback) {
return false;
}

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

/**
Expand All @@ -42,7 +52,7 @@ public function __invoke()
*
* @return \Illuminate\Routing\Route
*/
protected function findRouteByUrl($url)
protected function findRouteByUrl(string $url)
{
return Collection::make(Route::getRoutes())->first(function ($route) use ($url) {
return $route->matches(Request::create($url));
Expand All @@ -64,4 +74,24 @@ protected function notFoundResponse()

abort(404);
}

/**
* Determine if we need to redirect to a localized version of this route.
*
* @return bool
*/
protected function shouldRedirect()
{
return Config::get('localized-routes.redirect_to_localized_urls');
}

/**
* Get the redirect status code from config.
*
* @return int
*/
protected function getRedirectStatusCode()
{
return Config::get('localized-routes.redirect_status_code', 301);
}
}
48 changes: 24 additions & 24 deletions src/LocaleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class LocaleConfig
/**
* The configured route action that holds a route's locale.
*
* @var string
* @var string|null
*/
protected $routeAction;

Expand All @@ -37,7 +37,7 @@ class LocaleConfig
*
* @param array $config
*/
public function __construct($config = [])
public function __construct(array $config = [])
{
$this->supportedLocales = $config['supported_locales'] ?? [];
$this->omittedLocale = $config['omitted_locale'] ?? null;
Expand All @@ -50,7 +50,7 @@ public function __construct($config = [])
*
* @return array
*/
public function getSupportedLocales()
public function getSupportedLocales(): array
{
return $this->supportedLocales;
}
Expand All @@ -62,7 +62,7 @@ public function getSupportedLocales()
*
* @return void
*/
public function setSupportedLocales($locales)
public function setSupportedLocales(array $locales): void
{
$this->supportedLocales = $locales;
}
Expand All @@ -72,7 +72,7 @@ public function setSupportedLocales($locales)
*
* @return string|null
*/
public function getOmittedLocale()
public function getOmittedLocale(): ?string
{
return $this->omittedLocale;
}
Expand All @@ -84,7 +84,7 @@ public function getOmittedLocale()
*
* @return void
*/
public function setOmittedLocale($locale)
public function setOmittedLocale(?string $locale): void
{
$this->omittedLocale = $locale;
}
Expand All @@ -94,7 +94,7 @@ public function setOmittedLocale($locale)
*
* @return string|null
*/
public function getFallbackLocale()
public function getFallbackLocale(): ?string
{
return $this->fallbackLocale;
}
Expand All @@ -106,39 +106,39 @@ public function getFallbackLocale()
*
* @return void
*/
public function setFallbackLocale($locale)
public function setFallbackLocale(?string $locale): void
{
$this->fallbackLocale = $locale;
}

/**
* Get the route action that holds a route's locale.
*
* @return string
* @return string|null
*/
public function getRouteAction()
public function getRouteAction(): ?string
{
return $this->routeAction;
}

/**
* Set the route action that holds a route's locale.
*
* @param string $locale
* @param string $action
*
* @return string
*/
public function setRouteAction($locale)
public function setRouteAction(string $action): string
{
return $this->routeAction = $locale;
return $this->routeAction = $action;
}

/**
* Get the locales (not the slugs or domains).
*
* @return array
*/
public function getLocales()
public function getLocales(): array
{
$locales = $this->getSupportedLocales();

Expand All @@ -156,7 +156,7 @@ public function getLocales()
*
* @return string|null
*/
public function findSlugByLocale($locale)
public function findSlugByLocale(string $locale): ?string
{
if ( ! $this->isSupportedLocale($locale) || $this->hasCustomDomains()) {
return null;
Expand All @@ -172,7 +172,7 @@ public function findSlugByLocale($locale)
*
* @return string|null
*/
public function findDomainByLocale($locale)
public function findDomainByLocale(string $locale): ?string
{
if ( ! $this->isSupportedLocale($locale) || ! $this->hasCustomDomains()) {
return null;
Expand All @@ -188,7 +188,7 @@ public function findDomainByLocale($locale)
*
* @return string|null
*/
public function findLocaleBySlug($slug)
public function findLocaleBySlug(string $slug): ?string
{
if ($this->hasCustomDomains()) {
return null;
Expand All @@ -208,7 +208,7 @@ public function findLocaleBySlug($slug)
*
* @return string|null
*/
public function findLocaleByDomain($domain)
public function findLocaleByDomain(string $domain): ?string
{
if ( ! $this->hasCustomDomains()) {
return null;
Expand All @@ -222,7 +222,7 @@ public function findLocaleByDomain($domain)
*
* @return bool
*/
public function hasLocales()
public function hasLocales(): bool
{
return count($this->getSupportedLocales()) > 0;
}
Expand All @@ -233,7 +233,7 @@ public function hasLocales()
*
* @return bool
*/
public function hasSimpleLocales()
public function hasSimpleLocales(): bool
{
return is_numeric(key($this->getSupportedLocales()));
}
Expand All @@ -243,7 +243,7 @@ public function hasSimpleLocales()
*
* @return bool
*/
public function hasCustomSlugs()
public function hasCustomSlugs(): bool
{
return $this->hasLocales() && ! $this->hasSimpleLocales() && ! $this->hasCustomDomains();
}
Expand All @@ -253,7 +253,7 @@ public function hasCustomSlugs()
*
* @return bool
*/
public function hasCustomDomains()
public function hasCustomDomains(): bool
{
$firstValue = array_values($this->getSupportedLocales())[0] ?? '';
$containsDot = strpos($firstValue, '.') !== false;
Expand All @@ -264,11 +264,11 @@ public function hasCustomDomains()
/**
* Check if the given locale is supported.
*
* @param string $locale
* @param string|null $locale
*
* @return bool
*/
public function isSupportedLocale($locale)
public function isSupportedLocale(?string $locale): bool
{
return in_array($locale, $this->getLocales());
}
Expand Down
5 changes: 3 additions & 2 deletions src/LocalizedRoutesRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CodeZero\LocalizedRoutes;

use Closure;
use CodeZero\LocalizedRoutes\Facades\LocaleConfig;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Route;
Expand All @@ -16,7 +17,7 @@ class LocalizedRoutesRegistrar
*
* @return void
*/
public function register($closure, $options = [])
public function register(Closure $closure, array $options = []): void
{
$locales = $options['supported_locales'] ?? LocaleConfig::getSupportedLocales();
$omittedLocale = $options['omitted_locale'] ?? LocaleConfig::getOmittedLocale();
Expand Down Expand Up @@ -90,7 +91,7 @@ public function register($closure, $options = [])
*
* @return array
*/
protected function moveOmittedLocaleToEnd($locales, $omittedLocale, $usingDomains, $usingCustomSlugs)
protected function moveOmittedLocaleToEnd(array $locales, ?string $omittedLocale, bool $usingDomains, bool $usingCustomSlugs): array
{
if ( ! $omittedLocale || $usingDomains) {
return $locales;
Expand Down
Loading