1 | <?php |
||
22 | class VndErrorHandler implements ErrorInterface |
||
23 | { |
||
24 | /** |
||
25 | * @var int |
||
26 | */ |
||
27 | private $code; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | private $body = ['message' => '', 'logref' => '']; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $logDir; |
||
38 | |||
39 | /** |
||
40 | * @var ErrorPage |
||
41 | */ |
||
42 | private $errorPage; |
||
43 | |||
44 | /** |
||
45 | * @var TransferInterface |
||
46 | */ |
||
47 | private $responder; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | private $lastErrorFile; |
||
53 | |||
54 | /** |
||
55 | * @var ExceptionAsString |
||
56 | */ |
||
57 | private $exceptionString; |
||
58 | |||
59 | 8 | public function __construct(AbstractAppMeta $appMeta, TransferInterface $responder) |
|
60 | { |
||
61 | 8 | $this->logDir = $appMeta->logDir; |
|
62 | 8 | $this->lastErrorFile = $this->logDir . '/last_error.log'; |
|
63 | 8 | $this->responder = $responder; |
|
64 | $this->exceptionString = new ExceptionAsString; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | 4 | public function handle(\Exception $e, Request $request) |
|
71 | { |
||
72 | $this->errorPage = $this->getErrorPage($e, $this->lastErrorFile); |
||
73 | |||
74 | $isCodeError = array_key_exists($e->getCode(), (new Code)->statusText); |
||
75 | 1 | $code = $isCodeError ? $e->getCode() : Code::ERROR; |
|
76 | $message = $code . ' ' . (new Code)->statusText[$code]; |
||
77 | // Client error |
||
78 | 4 | if (400 <= $code && $code < 500) { |
|
79 | $this->log($e, $request); |
||
80 | 2 | $this->code = $code; |
|
81 | 2 | $this->body = [ |
|
82 | 'message' => $message |
||
83 | 2 | ]; |
|
84 | |||
85 | 2 | return $this; |
|
86 | } |
||
87 | // Server error |
||
88 | $logRef = $this->log($e, $request); |
||
89 | 2 | $this->code = $code; |
|
90 | 2 | $this->body = [ |
|
91 | 'message' => $message, |
||
92 | 'logref' => $logRef |
||
93 | 2 | ]; |
|
94 | |||
95 | 2 | return $this; |
|
96 | 4 | } |
|
97 | |||
98 | /** |
||
99 | * @param \Exception $e |
||
100 | * @param string $lastErrorFile |
||
101 | * |
||
102 | * @return \BEAR\Package\Provide\Error\ErrorPage|ErrorPage |
||
103 | */ |
||
104 | 4 | private function getErrorPage(\Exception $e, $lastErrorFile) |
|
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | 4 | public function transfer() |
|
120 | |||
121 | /** |
||
122 | * @param \Exception $e |
||
123 | * @param Request $request |
||
124 | * |
||
125 | * @return int logRef |
||
126 | */ |
||
127 | 4 | private function log(\Exception $e, Request $request) |
|
137 | |||
138 | /** |
||
139 | * Return log ref id |
||
140 | * |
||
141 | * @param \Exception $e |
||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | 4 | private function getLogRefId(\Exception $e) |
|
149 | } |
||
150 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: