Test Setup Failed
Push — master ( 40b2f2...aac3f3 )
by Ben
09:14 queued 11s
created

RoutesServiceProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A autoloadAdminMiddleware() 0 18 1
A boot() 0 8 2
A loadAdminRoutes() 0 10 3
A loadOpenAdminRoutes() 0 4 1
A autoloadFrontendRoute() 0 14 2
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Providers;
4
5
use Illuminate\Routing\Router;
6
use Illuminate\Support\Facades\Route;
7
use Illuminate\Support\ServiceProvider;
8
use Thinktomorrow\Chief\Admin\HealthMonitor\Middleware\MonitorMiddleware;
9
use Thinktomorrow\Chief\App\Http\Middleware\AuthenticateChiefSession;
10
use Thinktomorrow\Chief\App\Http\Middleware\ChiefNavigation;
11
use Thinktomorrow\Chief\App\Http\Middleware\ChiefRedirectIfAuthenticated;
12
use Thinktomorrow\Chief\App\Http\Middleware\ChiefValidateInvite;
13
use Thinktomorrow\Chief\App\Http\Middleware\EncryptCookies;
14
use Thinktomorrow\Chief\Shared\AdminEnvironment;
15
use Thinktomorrow\Chief\Site\Urls\ChiefResponse;
16
17
class RoutesServiceProvider extends ServiceProvider
18
{
19
    public function boot(): void
20
    {
21
        $this->autoloadFrontendRoute();
22
        $this->loadOpenAdminRoutes();
23
24
        if (app(AdminEnvironment::class)->check()) {
25
            $this->loadAdminRoutes();
26
            $this->autoloadAdminMiddleware();
27
        }
28
    }
29
30
    private function loadOpenAdminRoutes(): void
31
    {
32
        Route::group(['prefix' => config('chief.route.prefix', 'admin'), 'middleware' => ['web']], function () {
33
            $this->loadRoutesFrom(__DIR__ . '/../../routes/chief-open-routes.php');
34
        });
35
    }
36
37
    private function loadAdminRoutes(): void
38
    {
39
        Route::group(['prefix' => config('chief.route.prefix', 'admin'), 'middleware' => ['web-chief', 'auth:chief']], function () {
40
            $this->loadRoutesFrom(__DIR__ . '/../../routes/chief-admin-routes.php');
41
42
            // Add project specific chief routing...
43
            $projectChiefRoutePath = config('chief.route.admin-filepath', null);
44
45
            if ($projectChiefRoutePath && file_exists($projectChiefRoutePath)) {
46
                $this->loadRoutesFrom($projectChiefRoutePath);
47
            }
48
        });
49
    }
50
51
    private function autoloadFrontendRoute()
52
    {
53
        if (true !== config('chief.route.autoload')) {
54
            return;
55
        }
56
57
        app()->booted(function () {
0 ignored issues
show
introduced by
The method booted() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        app()->/** @scrutinizer ignore-call */ booted(function () {
Loading history...
58
            $routeName = config('chief.route.name');
59
60
            Route::get('{slug?}', function ($slug = '/') {
61
                return ChiefResponse::fromSlug($slug);
62
            })->name($routeName)
63
                ->where('slug', '(.*)?')
64
                ->middleware('web');
65
        });
66
    }
67
68
    private function autoloadAdminMiddleware(): void
69
    {
70
        app(Router::class)->middlewareGroup('web-chief', [
71
            // The default laravel web middleware - except for the csrf token verification.
72
            EncryptCookies::class,
73
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
74
            \Illuminate\Session\Middleware\StartSession::class,
75
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
76
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
77
78
            // Chief admin specific middleware
79
            AuthenticateChiefSession::class,
80
            MonitorMiddleware::class,
81
            ChiefNavigation::class,
82
        ]);
83
84
        app(Router::class)->aliasMiddleware('chief-guest', ChiefRedirectIfAuthenticated::class);
85
        app(Router::class)->aliasMiddleware('chief-validate-invite', ChiefValidateInvite::class);
86
    }
87
}
88