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