summaryrefslogtreecommitdiff
path: root/src/Exceptions/ExceptionsServiceProvider.php
blob: a9bc2b17a7e70dd35202559ae54f41446f05ced7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php

namespace Engelsystem\Exceptions;

use Engelsystem\Container\ServiceProvider;
use Engelsystem\Exceptions\Handlers\HandlerInterface;
use Engelsystem\Exceptions\Handlers\Legacy;
use Engelsystem\Exceptions\Handlers\LegacyDevelopment;
use Engelsystem\Exceptions\Handlers\Whoops;
use Whoops\Run as WhoopsRunner;

class ExceptionsServiceProvider extends ServiceProvider
{
    public function register()
    {
        $errorHandler = $this->app->make(Handler::class);
        $this->addProductionHandler($errorHandler);
        $this->addDevelopmentHandler($errorHandler);
        $this->app->instance('error.handler', $errorHandler);
        $this->app->bind(Handler::class, 'error.handler');
        $errorHandler->register();
    }

    public function boot()
    {
        /** @var Handler $handler */
        $handler = $this->app->get('error.handler');
        $request = $this->app->get('request');

        $handler->setRequest($request);
    }

    /**
     * @param Handler $errorHandler
     */
    protected function addProductionHandler($errorHandler)
    {
        $handler = $this->app->make(Legacy::class);
        $this->app->instance('error.handler.production', $handler);
        $errorHandler->setHandler(Handler::ENV_PRODUCTION, $handler);
        $this->app->bind(HandlerInterface::class, 'error.handler.production');
    }

    /**
     * @param Handler $errorHandler
     */
    protected function addDevelopmentHandler($errorHandler)
    {
        $handler = $this->app->make(LegacyDevelopment::class);

        if (class_exists(WhoopsRunner::class)) {
            $handler = $this->app->make(Whoops::class);
        }

        $this->app->instance('error.handler.development', $handler);
        $errorHandler->setHandler(Handler::ENV_DEVELOPMENT, $handler);
    }
}