summaryrefslogtreecommitdiff
path: root/src/Exceptions/Handler.php
blob: 95bcd132c9913c6ccd9c3cb86baad3dffed2d6eb (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php

namespace Engelsystem\Exceptions;

use Throwable;

class Handler
{
    /** @var string */
    protected $environment;

    const ENV_PRODUCTION = 'prod';
    const ENV_DEVELOPMENT = 'dev';

    /**
     * Handler constructor.
     *
     * @param string $environment production|development
     */
    public function __construct($environment = self::ENV_PRODUCTION)
    {
        $this->environment = $environment;

        set_error_handler([$this, 'errorHandler']);
        set_exception_handler([$this, 'exceptionHandler']);
    }

    /**
     * @param int    $number
     * @param string $string
     * @param string $file
     * @param int    $line
     * @param array  $context
     */
    public function errorHandler($number, $string, $file, $line, $context)
    {
        $trace = array_reverse(debug_backtrace());

        $this->handle('error', $number, $string, $file, $line, $context, $trace);
    }

    /**
     * @param Throwable $e
     */
    public function exceptionHandler($e)
    {
        $this->handle(
            'exception',
            $e->getCode(),
            get_class($e) . ': ' . $e->getMessage(),
            $e->getFile(),
            $e->getLine(),
            ['exception' => $e]
        );
    }

    /**
     * @param string $type
     * @param int    $number
     * @param string $string
     * @param string $file
     * @param int    $line
     * @param array  $context
     * @param array  $trace
     */
    protected function handle($type, $number, $string, $file, $line, $context = [], $trace = [])
    {
        error_log(sprintf('%s: Number: %s, String: %s, File: %s:%u, Context: %s',
            $type,
            $number,
            $string,
            $file,
            $line,
            json_encode($context)
        ));

        $file = $this->stripBasePath($file);

        if ($this->environment == self::ENV_DEVELOPMENT) {
            echo '<pre style="background-color:#333;color:#ccc;z-index:1000;position:fixed;bottom:1em;padding:1em;width:97%;max-height: 90%;overflow-y:auto;">';
            echo sprintf('%s: (%s)' . PHP_EOL, ucfirst($type), $number);
            var_export([
                'string'     => $string,
                'file'       => $file . ':' . $line,
                'context'    => $context,
                'stacktrace' => $this->formatStackTrace($trace),
            ]);
            echo '</pre>';
            die();
        }

        echo 'An <del>un</del>expected error occurred, a team of untrained monkeys has been dispatched to deal with it.';
        die();
    }

    /**
     * @param array $stackTrace
     * @return array
     */
    protected function formatStackTrace($stackTrace)
    {
        $return = [];

        foreach ($stackTrace as $trace) {
            $path = '';
            $line = '';

            if (isset($trace['file']) && isset($trace['line'])) {
                $path = $this->stripBasePath($trace['file']);
                $line = $trace['line'];
            }

            $functionName = $trace['function'];

            $return[] = [
                'file'        => $path . ':' . $line,
                $functionName => $trace['args'],
            ];
        }

        return $return;
    }

    /**
     * @param string $path
     * @return string
     */
    protected function stripBasePath($path)
    {
        $basePath = realpath(__DIR__ . '/../..') . '/';
        return str_replace($basePath, '', $path);
    }

    /**
     * @param string $environment
     */
    public function setEnvironment($environment)
    {
        $this->environment = $environment;
    }
}