LaravelTracyServiceProvider::boot()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5.0035

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 3
dl 0
loc 32
ccs 18
cts 19
cp 0.9474
crap 5.0035
rs 9.0968
c 0
b 0
f 0
1
<?php
2
3
namespace Recca0120\LaravelTracy;
4
5
use Illuminate\Contracts\Debug\ExceptionHandler;
6
use Illuminate\Contracts\Http\Kernel;
7
use Illuminate\Contracts\View\Factory as View;
8
use Illuminate\Routing\Router;
9
use Illuminate\Support\Arr;
10
use Illuminate\Support\ServiceProvider;
11
use Recca0120\LaravelTracy\Exceptions\Handler;
12
use Recca0120\LaravelTracy\Middleware\RenderBar;
13
use Recca0120\Terminal\TerminalServiceProvider;
14
use Tracy\Bar;
15
use Tracy\BlueScreen;
16
use Tracy\Debugger;
17
18
class LaravelTracyServiceProvider extends ServiceProvider
19
{
20
    /**
21
     * Indicates if loading of the provider is deferred.
22
     *
23
     * @var bool
24
     */
25
    protected $defer = false;
26
27
    /**
28
     * namespace.
29
     *
30
     * @var string
31
     */
32
    protected $namespace = 'Recca0120\LaravelTracy\Http\Controllers';
33
34
    /**
35
     * boot.
36
     *
37
     * @param \Illuminate\Contracts\Http\Kernel $kernel
38
     * @param \Illuminate\Contracts\View\Factory $view
39
     * @param \Illuminate\Routing\Router $router
40
     */
41 2
    public function boot(Kernel $kernel, View $view, Router $router)
42
    {
43 2
        $config = $this->app['config']['tracy'];
44 2
        $this->handleRoutes($router, Arr::get($config, 'route', []));
45
46 2
        if ($this->app->runningInConsole() === true) {
47 1
            $this->publishes([__DIR__.'/../config/tracy.php' => config_path('tracy.php')], 'config');
48
49 1
            return;
50
        }
51
52
        $view->getEngineResolver()->resolve('blade')->getCompiler()->directive('bdump', function ($expression) {
53 1
            return "<?php \Tracy\Debugger::barDump({$expression}); ?>";
54 1
        });
55
56 1
        $enabled = Arr::get($config, 'enabled', true) === true;
57 1
        if ($enabled === false) {
58
            return;
59
        }
60
61 1
        $showException = Arr::get($config, 'showException', true);
62 1
        if ($showException === true) {
63
            $this->app->extend(ExceptionHandler::class, function ($exceptionHandler, $app) {
64 1
                return new Handler($exceptionHandler, $app[DebuggerManager::class]);
65 1
            });
66
        }
67
68 1
        $showBar = Arr::get($config, 'showBar', true);
69 1
        if ($showBar === true) {
70 1
            $kernel->prependMiddleware(RenderBar::class);
71
        }
72 1
    }
73
74
    /**
75
     * Register the service provider.
76
     */
77
    public function register()
78
    {
79
        $this->mergeConfigFrom(__DIR__.'/../config/tracy.php', 'tracy');
80
81
        $config = Arr::get($this->app['config'], 'tracy');
82
83
        if (Arr::get($config, 'panels.terminal') === true) {
84
            $this->app->register(TerminalServiceProvider::class);
85
        }
86
87
        $this->app->bind(BlueScreen::class, function () {
88
            return Debugger::getBlueScreen();
89
        });
90
91
        $this->app->bind(Bar::class, function ($app) use ($config) {
92
            return (new BarManager(Debugger::getBar(), $app['request'], $app))
93
                ->loadPanels(Arr::get($config, 'panels', []))
94
                ->getBar();
95
        });
96
97
        $this->app->bind(DebuggerManager::class, function ($app) use ($config) {
98
            return (new DebuggerManager(
99
                DebuggerManager::init($config), $app[Bar::class], $app[BlueScreen::class], new Session
100
            ))->setUrlGenerator($app['url']);
101
        });
102
    }
103
104
    /**
105
     * Get the services provided by the provider.
106
     *
107
     * @return array
108
     */
109 1
    public function provides()
110
    {
111 1
        return [ExceptionHandler::class];
112
    }
113
114
    /**
115
     * register routes.
116
     *
117
     * @param \Illuminate\Routing\Router $router
118
     * @param array $config
119
     */
120 2
    protected function handleRoutes(Router $router, $config = [])
121
    {
122 2
        if ($this->app->routesAreCached() === false) {
123 1
            $router->group(array_merge([
124 1
                'namespace' => $this->namespace,
125
            ], $config), function (Router $router) {
126
                require __DIR__.'/../routes/web.php';
127 1
            });
128
        }
129 2
    }
130
}
131