Your IP : 216.73.216.79


Current Path : /var/www/html/redd/app/Services/
Upload File :
Current File : /var/www/html/redd/app/Services/RouteScanner.php

<?php

namespace App\Services;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Cache;

class RouteScanner
{
    protected array $exceptPatterns = [
        '_debugbar',
        'telescope',
        'horizon',
        'vendor',
        'api/',
        ' sanctum',
        'passport',
        'spark',
    ];

    protected array $exceptRoutes = [
        'up',
        'down',
        '404',
        '500',
        '503',
    ];

    public function scan(): array
    {
        $routes = Route::getRoutes();
        $scanned = [];

        foreach ($routes as $route) {
            if ($this->shouldSkip($route)) {
                continue;
            }

            $scanned[] = $this->extractRouteData($route);
        }

        return $scanned;
    }

    protected function shouldSkip($route): bool
    {
        if ($route->getActionName() === 'Closure') {
            return true;
        }

        $uri = $route->uri();

        foreach ($this->exceptRoutes as $except) {
            if ($uri === $except) {
                return true;
            }
        }

        foreach ($this->exceptPatterns as $pattern) {
            if (stripos($uri, $pattern) !== false) {
                return true;
            }
        }

        if (str_contains($uri, '_debugbar')) {
            return true;
        }

        return false;
    }

    protected function extractRouteData($route): array
    {
        $action = $route->getActionName();
        $controller = null;
        $actionName = null;

        if ($action !== 'Closure' && str_contains($action, '@')) {
            [$controller, $actionName] = explode('@', $action);
        }

        $middleware = $route->middleware();
        $middlewareStr = is_array($middleware) ? implode(',', $middleware) : '';

        $type = $this->determineType($route);

        return [
            'name' => $route->getName(),
            'uri' => '/' . ltrim($route->uri(), '/'),
            'method' => implode(',', $route->methods()),
            'controller' => $controller,
            'action' => $actionName,
            'middleware' => $middlewareStr,
            'type' => $type,
        ];
    }

    protected function determineType($route): string
    {
        $uri = $route->uri();
        $middleware = implode(' ', $route->middleware());

        if (str_starts_with($uri, 'api/') || str_contains($middleware, 'api')) {
            return 'api';
        }

        if (str_starts_with($uri, 'admin/') || str_contains($middleware, 'admin')) {
            return 'admin';
        }

        return 'web';
    }

    public function scanAndCache(): array
    {
        $routes = $this->scan();
        Cache::put('scanned_routes', $routes, now()->addDay());
        return $routes;
    }

    public function getCached(): ?array
    {
        return Cache::get('scanned_routes');
    }

    public function clearCache(): bool
    {
        return Cache::forget('scanned_routes');
    }

    public function refresh(): array
    {
        $this->clearCache();
        return $this->scanAndCache();
    }
}