Skip to content

Inject Request instead of using the facade #91

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 1 commit into from
Apr 8, 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
19 changes: 14 additions & 5 deletions src/LocalizedUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@

use CodeZero\LocalizedRoutes\Facades\LocaleConfig;
use CodeZero\UrlBuilder\UrlBuilder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Request;
use Illuminate\Contracts\Routing\UrlRoutable;
use Symfony\Component\Routing\Exception\RouteNotFoundException;

class LocalizedUrlGenerator
{
/**
* The current Request.
*
* @var \Illuminate\Http\Request
*/
protected $request;

/**
* The current Route.
*
Expand All @@ -23,10 +29,13 @@ class LocalizedUrlGenerator

/**
* Create a new LocalizedUrlGenerator instance.
*
* @param \Illuminate\Http\Request $request
*/
public function __construct()
public function __construct(Request $request)
{
$this->route = Route::current();
$this->request = $request;
$this->route = $request->route();
}

/**
Expand All @@ -41,7 +50,7 @@ public function __construct()
*/
public function generateFromRequest(string $locale = null, $parameters = null, bool $absolute = true, bool $keepQuery = true): string
{
$urlBuilder = UrlBuilder::make(Request::fullUrl());
$urlBuilder = UrlBuilder::make($this->request->fullUrl());
$requestQueryString = $urlBuilder->getQuery();

$currentDomain = $urlBuilder->getHost();
Expand Down
7 changes: 3 additions & 4 deletions src/Macros/Route/HasLocalizedMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CodeZero\LocalizedRoutes\Macros\Route;

use CodeZero\LocalizedRoutes\RouteHelper;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Route;

Expand All @@ -14,10 +15,8 @@ class HasLocalizedMacro
*/
public static function register()
{
Route::macro('hasLocalized', function ($name, $locale = null) {
$locale = $locale ?: App::getLocale();

return $this->routes->hasNamedRoute("{$locale}.{$name}");
Route::macro('hasLocalized', function (string $name, ?string $locale = null) {
return App::make(RouteHelper::class)->hasLocalized($name, $locale);
});
}
}
4 changes: 3 additions & 1 deletion src/Macros/Route/IsFallbackMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CodeZero\LocalizedRoutes\Macros\Route;

use CodeZero\LocalizedRoutes\RouteHelper;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Route;

class IsFallbackMacro
Expand All @@ -14,7 +16,7 @@ class IsFallbackMacro
public static function register()
{
Route::macro('isFallback', function () {
return Route::current() && Route::current()->isFallback;
return App::make(RouteHelper::class)->isFallback();
});
}
}
22 changes: 3 additions & 19 deletions src/Macros/Route/IsLocalizedMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace CodeZero\LocalizedRoutes\Macros\Route;

use CodeZero\LocalizedRoutes\Facades\LocaleConfig;
use Illuminate\Support\Collection;
use CodeZero\LocalizedRoutes\RouteHelper;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Route;

class IsLocalizedMacro
Expand All @@ -16,23 +16,7 @@ class IsLocalizedMacro
public static function register()
{
Route::macro('isLocalized', function ($patterns = null, $locales = '*') {
if ($patterns === null) {
$routeAction = LocaleConfig::getRouteAction();
$route = Route::current();

return $route && $route->getAction($routeAction) !== null;
}

$locales = Collection::make($locales);
$names = Collection::make();

Collection::make($patterns)->each(function ($name) use ($locales, $names) {
$locales->each(function ($locale) use ($name, $names) {
$names->push($locale . '.' . $name);
});
});

return Route::is($names->all());
return App::make(RouteHelper::class)->isLocalized($patterns, $locales);
});
}
}
21 changes: 19 additions & 2 deletions src/Middleware/Detectors/RouteActionDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,28 @@

namespace CodeZero\LocalizedRoutes\Middleware\Detectors;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Request;

class RouteActionDetector implements Detector
{
/**
* The current Route.
*
* @var \Illuminate\Routing\Route
*/
protected $route;

/**
* Create a new RouteActionDetector instance.
*
* @param \Illuminate\Http\Request $request
*/
public function __construct(Request $request)
{
$this->route = $request->route();
}

/**
* Detect the locale.
*
Expand All @@ -16,6 +33,6 @@ public function detect()
{
$action = Config::get('localized-routes.route_action');

return Request::route()->getAction($action);
return $this->route->getAction($action);
}
}
23 changes: 20 additions & 3 deletions src/Middleware/Detectors/UrlDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,35 @@
namespace CodeZero\LocalizedRoutes\Middleware\Detectors;

use CodeZero\LocalizedRoutes\Facades\LocaleConfig;
use Illuminate\Support\Facades\Request;
use Illuminate\Http\Request;

class UrlDetector implements Detector
{
/**
* The current Request.
*
* @var \Illuminate\Http\Request
*/
protected $request;

/**
* Create a new UrlDetector instance.
*
* @param \Illuminate\Http\Request $request
*/
public function __construct(Request $request)
{
$this->request = $request;
}

/**
* Detect the locale.
*
* @return string|array|null
*/
public function detect()
{
$slug = Request::segment(1);
$slug = $this->request->segment(1);

// If supported locales is a simple array like ['en', 'nl']
// just return the slug and let the calling code check if it is supported.
Expand All @@ -25,7 +42,7 @@ public function detect()
// Find the locale that belongs to the custom domain or slug.
// Return the original slug as fallback.
// The calling code should validate and handle it.
$domain = Request::getHttpHost();
$domain = $this->request->getHttpHost();
$locale = LocaleConfig::findLocaleByDomain($domain) ?? LocaleConfig::findLocaleBySlug($slug) ?? $slug;

return $locale;
Expand Down
103 changes: 103 additions & 0 deletions src/RouteHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace CodeZero\LocalizedRoutes;

use CodeZero\LocalizedRoutes\Facades\LocaleConfig;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Route;

class RouteHelper
{
/**
* The current Route.
*
* @var \Illuminate\Routing\Route
*/
protected $route;

/**
* Create a new RouteHelper instance.
*
* @param \Illuminate\Http\Request $request
*/
public function __construct(Request $request)
{
$this->route = $request->route();
}

/**
* Check if the current route is a fallback route.
*
* @return bool
*/
public function isFallback(): bool
{
return $this->route && $this->route->isFallback;
}

/**
* Check if the current route is localized.
*
* @param string|array $patterns
* @param string|array $locales
*
* @return bool
*/
public function isLocalized($patterns = null, $locales = '*'): bool
{
return $patterns === null
? $this->isCurrentRouteLocalized()
: $this->isCurrentRouteLocalizedWithNamePattern($patterns, $locales);
}

/**
* Check if a localized route exists.
*
* @param string $name
* @param string|null $locale
*
* @return bool
*/
public function hasLocalized(string $name, ?string $locale = null): bool
{
$locale = $locale ?: App::getLocale();

return Route::has("{$locale}.{$name}");
}

/**
* Check if the current route is localized.
*
* @return bool
*/
protected function isCurrentRouteLocalized(): bool
{
$routeAction = LocaleConfig::getRouteAction();

return $this->route && $this->route->getAction($routeAction) !== null;
}

/**
* Check if the current route is localized and has a specific name.
*
* @param string|array $patterns
* @param string|array $locales
*
* @return bool
*/
protected function isCurrentRouteLocalizedWithNamePattern($patterns = null, $locales = '*'): bool
{
$locales = Collection::make($locales);
$names = Collection::make();

Collection::make($patterns)->each(function ($name) use ($locales, $names) {
$locales->each(function ($locale) use ($name, $names) {
$names->push($locale . '.' . $name);
});
});

return Route::is($names->all());
}
}