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/ste.komma.pro/app/Routes/BreadcrumbComposer.php
<?php


namespace App\Routes;


use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Lang;
use Illuminate\View\View;
use Komma\KMS\Sites\SiteServiceInterface;

final class BreadcrumbComposer
{

    private SiteServiceInterface $siteService;

    public function __construct()
    {
        $this->siteService = app(SiteServiceInterface::class);
    }

    /**
     * Bind data to the view.
     *
     * @param  View  $view
     * @return void
     */
    public function compose(View $view)
    {

        $breadcrumbList = [ $this->makeRootBread() ];

        // Use the server request URI, because we have changed the laravel request into our RESTfull way
        $requestPath = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/';

        // If root then we can already return
        // Or root with language for when multi language
        if($requestPath == "/" || config('app.multipleLanguages') && $requestPath == '/' . app()->getLocale() ) {
            $view->with('breadcrumbList', $breadcrumbList);
            return;
        }

        $this->handleSegments($breadcrumbList, $requestPath);

        $view->with('breadcrumbList', $breadcrumbList);
    }

    /**
     * Make root bread object
     *
     * @return object
     */
    private function makeRootBread()
    {
        $rootBread = (object)[
            'route' => request()->root(),
            'name'  => __('site/breadcrumb.rootName'),
        ];

        // If multi language we also add the current language to the root route.
        if(config('app.multipleLanguages') && App::getLanguage()->id !== $this->siteService->getCurrentSiteDefaultLanguage()) {
            $rootBread->route .= '/' . app()->getLocale();
        }

        return $rootBread;
    }

    /**
     * Handle the other segments of the requested path
     *
     * @param  array  $breadcrumbList
     * @param  string  $requestPath
     */
    private function handleSegments(array &$breadcrumbList, string $requestPath)
    {
        // Remove the root slash from the request path, and explode into segments
        $requestPath = substr($requestPath, 1);
        $segments = explode('/', $requestPath);

        // If multi language is enabled then the first segment can be skipped
        if(config('app.multipleLanguages')) array_shift($segments);

        // Loop through the segments
        foreach ($segments as $key => $segment) {

            // Check if the segment has a translation defined.
            $translationKey = 'site/breadcrumb.' . $segment;
            $translation = __($translationKey);

            // If the key and translation are the same, we haven't defined a manual translation
            // Therefor we do the default conversion
            if($translation === $translationKey) {

                $translation = ucfirst($segment);
                $translation = str_replace('-', ' ', $translation);
            }

            $lastCrumbPath = end($breadcrumbList)->route;

            // Append the language segment when multi language is enabled and we are on the default language
            if(config('app.multipleLanguages') && App::getLanguage()->id === $this->siteService->getCurrentSiteDefaultLanguage() && $key == 0) {
                $lastCrumbPath .= '/' . app()->getLocale();
            }

            // Append to the breadcrumb list
            $breadcrumbList[] = (object)[
                'route' => $lastCrumbPath . '/' . $segment,
                'name' => $translation
            ];
        }
    }
}