1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nofw\Emperror; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Error handler. |
7
|
|
|
* |
8
|
|
|
* @author Márk Sági-Kazár <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
final class ErrorHandler implements \Nofw\Error\ErrorHandler |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Default context values. |
14
|
|
|
*/ |
15
|
|
|
private $context = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var ErrorHandler[] |
19
|
|
|
*/ |
20
|
|
|
private $handlers = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Processor[] |
24
|
|
|
*/ |
25
|
|
|
private $processors = []; |
26
|
|
|
|
27
|
|
|
public function __construct(array $context = []) |
28
|
|
|
{ |
29
|
|
|
$this->context = $context; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function handle(\Throwable $t, array $context = []): void |
36
|
|
|
{ |
37
|
|
|
$context = array_replace_recursive($this->context, $context); |
38
|
|
|
|
39
|
|
|
// Process the context |
40
|
|
|
foreach ($this->processors as $processor) { |
41
|
|
|
$context = $processor->process($t, $context); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Handle the error |
45
|
|
|
foreach ($this->handlers as $handler) { |
46
|
|
|
$handler->handle($t, $context); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Pushes a handler on to the stack. |
52
|
|
|
* |
53
|
|
|
* @param \Nofw\Error\ErrorHandler $handler |
54
|
|
|
* |
55
|
|
|
* @return ErrorHandler |
56
|
|
|
*/ |
57
|
|
|
public function pushHandler(\Nofw\Error\ErrorHandler $handler): self |
58
|
|
|
{ |
59
|
|
|
array_unshift($this->handlers, $handler); |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Pops a handler from the stack. |
66
|
|
|
* |
67
|
|
|
* @return \Nofw\Error\ErrorHandler |
68
|
|
|
* |
69
|
|
|
* @throws \LogicException If the handler stack is empty |
70
|
|
|
*/ |
71
|
|
|
public function popHandler(): \Nofw\Error\ErrorHandler |
72
|
|
|
{ |
73
|
|
|
if (empty($this->handlers)) { |
74
|
|
|
throw new \LogicException('Tried to pop from an empty handler stack.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return array_shift($this->handlers); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return ErrorHandler[] |
82
|
|
|
*/ |
83
|
|
|
public function getHandlers(): array |
84
|
|
|
{ |
85
|
|
|
return $this->handlers; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param ErrorHandler[] $handlers |
90
|
|
|
* |
91
|
|
|
* @return ErrorHandler |
92
|
|
|
*/ |
93
|
|
|
public function setHandlers(array $handlers): self |
94
|
|
|
{ |
95
|
|
|
$this->handlers = []; |
96
|
|
|
|
97
|
|
|
foreach ($handlers as $handler) { |
98
|
|
|
$this->pushHandler($handler); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Pushes a processor on to the stack. |
106
|
|
|
* |
107
|
|
|
* @param Processor $processor |
108
|
|
|
* |
109
|
|
|
* @return ErrorHandler |
110
|
|
|
*/ |
111
|
|
|
public function pushProcessor(Processor $processor): self |
112
|
|
|
{ |
113
|
|
|
array_unshift($this->processors, $processor); |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Pops a processor from the stack. |
120
|
|
|
* |
121
|
|
|
* @return Processor |
122
|
|
|
* |
123
|
|
|
* @throws \LogicException If the processor stack is empty |
124
|
|
|
*/ |
125
|
|
|
public function popProcessor(): Processor |
126
|
|
|
{ |
127
|
|
|
if (empty($this->processors)) { |
128
|
|
|
throw new \LogicException('Tried to pop from an empty processor stack.'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return array_shift($this->processors); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return Processor[] |
136
|
|
|
*/ |
137
|
|
|
public function getProcessors(): array |
138
|
|
|
{ |
139
|
|
|
return $this->processors; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param Processor[] $processors |
144
|
|
|
* |
145
|
|
|
* @return ErrorHandler |
146
|
|
|
*/ |
147
|
|
|
public function setProcessors(array $processors): self |
148
|
|
|
{ |
149
|
|
|
$this->processors = []; |
150
|
|
|
|
151
|
|
|
foreach ($processors as $processor) { |
152
|
|
|
$this->pushProcessor($processor); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|