HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/beat-the-barn.komma.nl/app/Http/Middleware/LocalizedRouteResolver.php
<?php


namespace App\Http\Middleware;

use Illuminate\Support\Facades\App;
use Komma\KMS\Globalization\Languages\Models\Language;
use App\Routes\Models\Route;
use App\Routes\RouteService;
use Closure;
use Illuminate\Http\Request;

/**
 * Class LocalizedRoute
 *
 * Does have a look at the first segment from the route and looks that up in the iso_2 column of the language table.
 * If it matches a language. It uses that language to set the app's language. It then strips the first segment from the url.
 * After that, all the values from the translations from site/routes.php will be compared against the url.
 * If it matches, the corresponding key is the named route where the user will be sent to.
 *
 * @package App\Http\Middleware
 */
final class LocalizedRouteResolver extends AbstractResolver
{
    /**
     * @param \Illuminate\Http\Request $request
     * @param Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // Check if route should be excluded from resolving
        if($this->isExcludedFromResolving($request)) return $next($request);

        // Check if route is already resolved
        if($this->isRouteResolved($request)) return $next($request);

        $this->log('Trying to resolve: '.$request->path());

        $segments = $request->segments();

        if(config('app.multipleLanguages')) {
            //TODO rewrite this so that only the current site available languages should be checked
            $possibleLanguage = Language::where('iso_2', '=', $request->segments()[0])->first();

            //Change the app language to the one from the url
            if ($possibleLanguage && \App::getLanguage() != $possibleLanguage) {
                $this->log('Changed app language to the one listed in the url: ' . $possibleLanguage->iso_2);
                \App::setLanguage($possibleLanguage);
            }

            //Remove the language iso 2 from the request
            array_shift($segments);
        }

        //Check if the remaining route uri matches named route in the site routes translation file. If so, Change the url to the original named url and return that.
        $newRoute = $this->getResolvedNamedRouteFromTranslatedRoute(implode('/',$segments));
        if($newRoute) {
            $translatedModifiedRequest = $request->duplicate();
            $translatedModifiedRequest->server->set('REQUEST_URI', $newRoute);

            $this->setRouteResolved($translatedModifiedRequest);
            return $next($translatedModifiedRequest);
        }

        //Just return the url without the iso_2 language prefix.
        return $next($request);
    }

    /**
     * @param string $translatedRouteCandidate
     * @return string
     */
    private function getResolvedNamedRouteFromTranslatedRoute(string $translatedRouteCandidate):string
    {
        //Example $translatedRouteCandidate = passwort/zurucksetzen/123As4$as4125sdasjfisghvcda$2512
        //Example            'site.password.reset       passwort/zurucksetzen

        $translationRoutes = __('routes');

        //Sort by length of translated routes routes
        uasort($translationRoutes, function($routeTranslationA, $routeTranslationB) {
            return strlen($routeTranslationB) - strlen($routeTranslationA);
        });

        foreach ($translationRoutes as $namedRoute => $translationRoute)
        {
            $length = mb_strlen($translationRoute);
            $translatedRouteCandidateTrimmed = substr($translatedRouteCandidate, 0, $length); //Example: passwort/zurucksetzen
            $parameters = explode('/', substr($translatedRouteCandidate, $length + 1)); //Example: 123As4$as4125sdasjfisghvcda$2512
            if($translationRoute == $translatedRouteCandidateTrimmed) {
                $url = route($namedRoute, $parameters, false);
                $this->log('Found a localized named route in the language file resources/lang/'.App::getLanguage()->iso_2.'/site/routes. Routename: '.$namedRoute);
                $this->log('That named route expands like this route("'.$namedRoute.'") to (including parameters): '.$url);
                return $url;
            }
        }

        return false;
    }
}