Completed
Push — master ( 75c9f6...102369 )
by Mike
13s
created

Service::shouldRegisterRouteMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php namespace GeneaLabs\LaravelCaffeine\Providers;
2
3
use GeneaLabs\LaravelCaffeine\Console\Commands\Publish;
4
use GeneaLabs\LaravelCaffeine\Http\Middleware\LaravelCaffeineDripMiddleware;
5
use Illuminate\Contracts\Http\Kernel;
6
use Illuminate\Routing\Route;
7
use Illuminate\Support\ServiceProvider;
8
9
class Service extends ServiceProvider
10
{
11
    protected $defer = false;
12
13
    public function boot()
14
    {
15
        app('router')->group($this->middlewareGroupExists('web')
16
            ? ['middleware' => 'web']
17
            : [], function () {
18
                require __DIR__ . '/../../routes/web.php';
19
20
                if (app('env') === 'testing') {
21
                    require __DIR__ . '/../../tests/routes/web.php';
22
                }
23
            });
24
25
        $configPath = __DIR__ . '/../../config/genealabs-laravel-caffeine.php';
26
        $this->mergeConfigFrom($configPath, 'genealabs-laravel-caffeine');
27
        $this->loadViewsFrom(
28
            __DIR__ . '/../../resources/views',
29
            'genealabs-laravel-caffeine'
30
        );
31
32
        if (app('env') === 'testing') {
33
            $this->loadViewsFrom(
34
                __DIR__ . '/../../tests/resources/views',
35
                'genealabs-laravel-caffeine'
36
            );
37
        }
38
39
        $this->publishes([
40
            $configPath => config_path('genealabs-laravel-caffeine.php')
41
        ], 'config');
42
    }
43
44
    public function register()
45
    {
46
        $this->commands(Publish::class);
47
        $this->mergeConfigFrom(__DIR__ . '/../../config/genealabs-laravel-caffeine.php', 'genealabs-laravel-caffeine');
48
49
        if ($this->shouldRegisterGlobalMiddleware()) {
50
            app(Kernel::class)->pushMiddleware('\\' . LaravelCaffeineDripMiddleware::class);
51
        }
52
53
        if ($this->shouldRegisterRouteMiddleware()) {
54
            app('router')->aliasMiddleware(
55
                'caffeinated',
56
                '\\' . LaravelCaffeineDripMiddleware::class
57
            );
58
        }
59
    }
60
61
    public function provides() : array
62
    {
63
        return ['genealabs-laravel-caffeine'];
64
    }
65
66
    protected function middlewareGroupExists(string $group) : bool
67
    {
68
        $routes = collect(app('router')->getRoutes()->getRoutes());
69
70
        return $routes->reduce(function ($carry, Route $route) use ($group) {
71
            $carry = ($carry ?? false) ?: false;
72
            $actions = (array) $route->getAction();
73
74
            if (array_key_exists('middleware', $actions)
75
                && in_array($group, (array) $actions['middleware'])
76
            ) {
77
                return true;
78
            }
79
80
            return $carry;
81
        }) ?? false;
82
    }
83
84
    protected function shouldRegisterGlobalMiddleware() : bool
85
    {
86
        return (! request()->ajax()
87
            && ! $this->shouldRegisterRouteMiddleware()
88
            && (php_sapi_name() === 'fpm-fcgi' || app('env') === 'testing'));
89
    }
90
91
    protected function shouldRegisterRouteMiddleware() : bool
92
    {
93
        return (bool) config('genealabs-laravel-caffeine.use-route-middleware');
94
    }
95
}
96