Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
ExceptionsServiceProvider
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
5 / 5
8
100.00% covered (success)
100.00%
1 / 1
 register
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 boot
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 addProductionHandler
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 addDevelopmentHandler
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 addLogger
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Exceptions;
6
7use Engelsystem\Container\ServiceProvider;
8use Engelsystem\Environment;
9use Engelsystem\Exceptions\Handlers\HandlerInterface;
10use Engelsystem\Exceptions\Handlers\Legacy;
11use Engelsystem\Exceptions\Handlers\LegacyDevelopment;
12use Engelsystem\Exceptions\Handlers\Whoops;
13use Psr\Log\LoggerInterface;
14use Whoops\Run as WhoopsRunner;
15
16class ExceptionsServiceProvider extends ServiceProvider
17{
18    public function register(): void
19    {
20        $errorHandler = $this->app->make(Handler::class);
21        $this->addProductionHandler($errorHandler);
22        $this->addDevelopmentHandler($errorHandler);
23        $this->app->instance('error.handler', $errorHandler);
24        $this->app->bind(Handler::class, 'error.handler');
25        $errorHandler->register();
26    }
27
28    public function boot(): void
29    {
30        /** @var Handler $handler */
31        $handler = $this->app->get('error.handler');
32        $request = $this->app->get('request');
33
34        $handler->setRequest($request);
35        $this->addLogger($handler);
36    }
37
38    protected function addProductionHandler(Handler $errorHandler): void
39    {
40        $handler = $this->app->make(Legacy::class);
41        $this->app->instance('error.handler.production', $handler);
42        $errorHandler->setHandler(Environment::PRODUCTION, $handler);
43        $this->app->bind(HandlerInterface::class, 'error.handler.production');
44    }
45
46    protected function addDevelopmentHandler(Handler $errorHandler): void
47    {
48        $handler = $this->app->make(LegacyDevelopment::class);
49
50        if (class_exists(WhoopsRunner::class)) {
51            $handler = $this->app->make(Whoops::class);
52        }
53
54        $this->app->instance('error.handler.development', $handler);
55        $errorHandler->setHandler(Environment::DEVELOPMENT, $handler);
56    }
57
58    protected function addLogger(Handler $handler): void
59    {
60        foreach ($handler->getHandler() as $h) {
61            if (!method_exists($h, 'setLogger')) {
62                continue;
63            }
64
65            $log = $this->app->get(LoggerInterface::class);
66            $h->setLogger($log);
67        }
68    }
69}