68 lines
2.5 KiB
PHP
68 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use Illuminate\Support\Facades\View;
|
|
use Closure;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Spatie\Translatable\HasTranslations;
|
|
|
|
class FrontEndGlobals
|
|
{
|
|
/**
|
|
* Handle an incoming request.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param \Closure(\Illuminate\Http\Request):(\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
|
|
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function handle(Request $request, Closure $next)
|
|
{
|
|
// Header menu.
|
|
$currentRoute = Route::current()->getName();
|
|
$routeParamsFR = Route::current()->parameters();
|
|
$routeParamsEN = $routeParamsFR;
|
|
$routeParamsES = $routeParamsFR;
|
|
array_walk($routeParamsFR, array($this, 'unbind'), '');
|
|
array_walk($routeParamsEN, array($this, 'unbind'), 'en');
|
|
array_walk($routeParamsES, array($this, 'unbind'), 'es');
|
|
if (preg_match('/^(en|es)/', $currentRoute)) {
|
|
$currentRouteFR = preg_replace('/^(en|es)\./', '', $currentRoute, 1);
|
|
$currentRouteEN = preg_replace('/^(en|es)/', 'en', $currentRoute, 1);
|
|
$currentRouteES = preg_replace('/^(en|es)/', 'es', $currentRoute, 1);
|
|
} else {
|
|
$currentRouteFR = $currentRoute;
|
|
$currentRouteEN = 'en.' . $currentRoute;
|
|
$currentRouteES = 'es.' . $currentRoute;
|
|
}
|
|
|
|
View::share('currentRouteFR', $currentRouteFR);
|
|
View::share('currentRouteEN', $currentRouteEN);
|
|
View::share('currentRouteES', $currentRouteES);
|
|
View::share('routeParamsFR', $routeParamsFR);
|
|
View::share('routeParamsEN', $routeParamsEN);
|
|
View::share('routeParamsES', $routeParamsES);
|
|
|
|
return $next($request);
|
|
}
|
|
|
|
/**
|
|
* Switch locale for translatable models
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Model $item
|
|
* @param string $key
|
|
* @param string $locale
|
|
* @return void
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
|
|
*/
|
|
private function unbind(Model &$item, string $key, string $locale)
|
|
{
|
|
// phpcs:enable
|
|
$classUses = class_uses($item);
|
|
if ($classUses and (\in_array(HasTranslations::class, $classUses))) {
|
|
$item = (clone $item)->setLocale($locale);
|
|
}
|
|
}
|
|
}
|