Test Failed
Pull Request — master (#80)
by Mike
05:21
created

Service::provides()   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
    public function boot()
12
    {
13
        app('router')->group($this->middlewareGroupExists('web')
14
            ? ['middleware' => 'web']
15
            : [], function () {
16
                require __DIR__ . '/../../routes/web.php';
17
18
                if (app('env') === 'testing') {
19
                    require __DIR__ . '/../../tests/routes/web.php';
20
                }
21
            });
22
23
        $configPath = __DIR__ . '/../../config/genealabs-laravel-caffeine.php';
24
        $this->mergeConfigFrom($configPath, 'genealabs-laravel-caffeine');
25
        $this->loadViewsFrom(
26
            __DIR__ . '/../../resources/views',
27
            'genealabs-laravel-caffeine'
28
        );
29
30
        if (app('env') === 'testing') {
31
            $this->loadViewsFrom(
32
                __DIR__ . '/../../tests/resources/views',
33
                'genealabs-laravel-caffeine'
34
            );
35
        }
36
37
        $this->publishes([
38
            $configPath => config_path('genealabs-laravel-caffeine.php')
39
        ], 'config');
40
41
        $this->commands(Publish::class);
42
        $this->mergeConfigFrom(__DIR__ . '/../../config/genealabs-laravel-caffeine.php', 'genealabs-laravel-caffeine');
43
44
        if ($this->shouldRegisterGlobalMiddleware()) {
45
            app(Kernel::class)->pushMiddleware('\\' . LaravelCaffeineDripMiddleware::class);
46
        }
47
48
        if ($this->shouldRegisterRouteMiddleware()) {
49
            app('router')->aliasMiddleware(
50
                'caffeinated',
51
                '\\' . LaravelCaffeineDripMiddleware::class
52
            );
53
        }
54
    }
55
56
    protected function middlewareGroupExists(string $group) : bool
57
    {
58
        $routes = collect(app('router')->getRoutes()->getRoutes());
59
60
        return $routes->reduce(function ($carry, Route $route) use ($group) {
61
            $carry = ($carry ?? false) ?: false;
62
            $actions = (array) $route->getAction();
63
64
            if (array_key_exists('middleware', $actions)
65
                && in_array($group, (array) $actions['middleware'])
66
            ) {
67
                return true;
68
            }
69
70
            return $carry;
71
        }) ?? false;
72
    }
73
74
    protected function shouldRegisterGlobalMiddleware() : bool
75
    {
76
        return (! request()->ajax()
77
            && ! $this->shouldRegisterRouteMiddleware()
78
            && (php_sapi_name() === 'fpm-fcgi'
79
                || php_sapi_name() === 'apache2handler'
80
                || app('env') === 'testing'));
81
    }
82
83
    protected function shouldRegisterRouteMiddleware() : bool
84
    {
85
        return (bool) config('genealabs-laravel-caffeine.use-route-middleware');
86
    }
87
}
88