ConditionalDebugBarServiceProvider   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 18
dl 0
loc 52
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 3 1
A overrideDebugBarMiddleware() 0 19 4
A boot() 0 10 3
1
<?php
2
3
namespace MaksimM\ConditionalDebugBar;
4
5
use Barryvdh\Debugbar\LaravelDebugbar;
6
use Barryvdh\Debugbar\Middleware\DebugbarEnabled;
7
use Illuminate\Routing\Route;
8
use Illuminate\Routing\Router;
9
use Illuminate\Support\Collection;
10
use Illuminate\Support\ServiceProvider;
11
12
class ConditionalDebugBarServiceProvider extends ServiceProvider
13
{
14
    /**
15
     * Bootstrap the application services.
16
     *
17
     * @throws \Exception
18
     */
19
    public function boot()
20
    {
21
        if ($this->app->runningInConsole()) {
22
            if (!str_contains($this->app->version(), 'Lumen')) {
23
                $this->publishes([
24
                    __DIR__.'/../config/conditional-debugbar.php' => config_path('conditional-debugbar.php'),
25
                ], 'config');
26
            }
27
        }
28
        $this->overrideDebugBarMiddleware($this->app);
29
    }
30
31
    /**
32
     * @throws \Exception
33
     */
34
    public function overrideDebugBarMiddleware($app)
35
    {
36
        if (class_exists(LaravelDebugbar::class)) {
37
            /**
38
             * @var Router
39
             */
40
            $router = $app['router'];
41
            /**
42
             * @var Collection|Route[]
43
             */
44
            $debugbarRoutes = collect($router->getRoutes()->getRoutesByName())
45
                ->filter(function ($value, $key) {
46
                    return strpos($key, 'debugbar') !== false;
47
                });
48
            if ($debugbarRoutes->count() > 0) {
49
                foreach ($debugbarRoutes as $route) {
50
                    $action = $route->getAction();
51
                    $action['middleware'] = array_merge(array_diff($action['middleware'], ['web', DebugbarEnabled::class]), ['web', Http\Middleware\DebugBarEnabled::class]);
52
                    $route->setAction($action);
53
                }
54
            }
55
        }
56
    }
57
58
    /**
59
     * Register the application services.
60
     */
61
    public function register()
62
    {
63
        $this->mergeConfigFrom(__DIR__.'/../config/conditional-debugbar.php', 'conditional-debugbar');
64
    }
65
}
66