Completed
Push — master ( 2c8513...a0ff8a )
by Mike
05:16 queued 01:45
created

Service::shouldRegisterGlobalMiddleware()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 7
c 4
b 0
f 0
dl 0
loc 9
rs 8.8333
cc 7
nc 11
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(app("router")->hasMiddlewareGroup('web')
14
            ? ['middleware' => 'web']
15
            : [], function () {
16
                require __DIR__ . '/../../routes/web.php';
17
18
                if (config("app.env") === 'internaltesting') {
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 (config("app.env") === 'internaltesting') {
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 shouldRegisterGlobalMiddleware() : bool
57
    {
58
        return (! request()->ajax()
59
            && ! $this->shouldRegisterRouteMiddleware()
60
            && (php_sapi_name() === 'fpm-fcgi'
61
                || php_sapi_name() === 'cgi-fcgi'
62
                || php_sapi_name() === 'apache2handler'
63
                || php_sapi_name() === 'cli-server'
64
                || config("app.env") === 'internaltesting'));
65
    }
66
67
    protected function shouldRegisterRouteMiddleware() : bool
68
    {
69
        return (bool) config('genealabs-laravel-caffeine.use-route-middleware');
70
    }
71
}
72