Completed
Pull Request — master (#35)
by recca
02:20
created

LaravelTracyServiceProvider::handleRoutes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

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