|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nofw\Emperror; |
|
4
|
|
|
|
|
5
|
|
|
use Nofw\Emperror\Context\Processor; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Error handler. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Márk Sági-Kazár <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
final class ErrorHandler implements \Nofw\Error\ErrorHandler |
|
13
|
|
|
{ |
|
14
|
|
|
private $context = []; |
|
15
|
|
|
private $processors = []; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(array $context = []) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->context = $context; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* {@inheritdoc} |
|
24
|
|
|
*/ |
|
25
|
|
|
public function handle(\Throwable $t, array $context = []): void |
|
26
|
|
|
{ |
|
27
|
|
|
$context = array_replace_recursive($this->context, $context); |
|
28
|
|
|
|
|
29
|
|
|
// Process the context |
|
30
|
|
|
foreach ($this->processors as $processor) { |
|
31
|
|
|
$context = $processor->process($t, $context); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Pushes a processor on to the stack. |
|
37
|
|
|
* |
|
38
|
|
|
* @param Processor $processor |
|
39
|
|
|
* |
|
40
|
|
|
* @return ErrorHandler |
|
41
|
|
|
*/ |
|
42
|
|
|
public function pushProcessor(Processor $processor): self |
|
43
|
|
|
{ |
|
44
|
|
|
array_unshift($this->processors, $processor); |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Pops a processor from the stack. |
|
51
|
|
|
* |
|
52
|
|
|
* @return Processor |
|
53
|
|
|
* |
|
54
|
|
|
* @throws \LogicException If the processor stack is empty |
|
55
|
|
|
*/ |
|
56
|
|
|
public function popProcessor(): Processor |
|
57
|
|
|
{ |
|
58
|
|
|
if (empty($this->processors)) { |
|
59
|
|
|
throw new \LogicException('Tried to pop from an empty processor stack.'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return array_shift($this->processors); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getProcessors(): array |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->processors; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param array $processors |
|
75
|
|
|
* |
|
76
|
|
|
* @return ErrorHandler |
|
77
|
|
|
*/ |
|
78
|
|
|
public function setProcessors(array $processors): self |
|
79
|
|
|
{ |
|
80
|
|
|
$this->processors = []; |
|
81
|
|
|
|
|
82
|
|
|
foreach ($processors as $processor) { |
|
83
|
|
|
$this->pushProcessor($processor); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|