1 | <?php |
||
20 | class PlainTextHandler extends Handler |
||
21 | { |
||
22 | const VAR_DUMP_PREFIX = ' | '; |
||
23 | |||
24 | /** |
||
25 | * @var \Psr\Log\LoggerInterface |
||
26 | */ |
||
27 | protected $logger; |
||
28 | |||
29 | /** |
||
30 | * @var bool |
||
31 | */ |
||
32 | private $addTraceToOutput = true; |
||
33 | |||
34 | /** |
||
35 | * @var bool|integer |
||
36 | */ |
||
37 | private $addTraceFunctionArgsToOutput = false; |
||
38 | |||
39 | /** |
||
40 | * @var integer |
||
41 | */ |
||
42 | private $traceFunctionArgsOutputLimit = 1024; |
||
43 | |||
44 | /** |
||
45 | * @var bool |
||
46 | */ |
||
47 | private $onlyForCommandLine = false; |
||
48 | |||
49 | /** |
||
50 | * @var bool |
||
51 | */ |
||
52 | private $outputOnlyIfCommandLine = true; |
||
53 | |||
54 | /** |
||
55 | * @var bool |
||
56 | */ |
||
57 | private $loggerOnly = false; |
||
58 | |||
59 | /** |
||
60 | * Constructor. |
||
61 | * @throws InvalidArgumentException If argument is not null or a LoggerInterface |
||
62 | * @param \Psr\Log\LoggerInterface|null $logger |
||
63 | */ |
||
64 | 1 | public function __construct($logger = null) |
|
65 | { |
||
66 | 1 | $this->setLogger($logger); |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * Set the output logger interface. |
||
71 | * @throws InvalidArgumentException If argument is not null or a LoggerInterface |
||
72 | * @param \Psr\Log\LoggerInterface|null $logger |
||
73 | */ |
||
74 | 2 | public function setLogger($logger = null) |
|
75 | { |
||
76 | 2 | if (! (is_null($logger) |
|
77 | 2 | || $logger instanceof LoggerInterface)) { |
|
78 | 2 | throw new InvalidArgumentException( |
|
79 | 2 | 'Argument to ' . __METHOD__ . |
|
80 | 2 | " must be a valid Logger Interface (aka. Monolog), " . |
|
81 | 2 | get_class($logger) . ' given.' |
|
82 | 2 | ); |
|
83 | } |
||
84 | |||
85 | 1 | $this->logger = $logger; |
|
86 | 1 | } |
|
87 | |||
88 | /** |
||
89 | * @return \Psr\Log\LoggerInterface|null |
||
90 | */ |
||
91 | public function getLogger() |
||
95 | |||
96 | /** |
||
97 | * Add error trace to output. |
||
98 | * @param bool|null $addTraceToOutput |
||
99 | * @return bool|$this |
||
100 | */ |
||
101 | 4 | public function addTraceToOutput($addTraceToOutput = null) |
|
102 | { |
||
103 | 4 | if (func_num_args() == 0) { |
|
104 | 4 | return $this->addTraceToOutput; |
|
105 | } |
||
106 | |||
107 | 4 | $this->addTraceToOutput = (bool) $addTraceToOutput; |
|
108 | 4 | return $this; |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Add error trace function arguments to output. |
||
113 | * Set to True for all frame args, or integer for the n first frame args. |
||
114 | * @param bool|integer|null $addTraceFunctionArgsToOutput |
||
115 | * @return null|bool|integer |
||
116 | */ |
||
117 | 2 | public function addTraceFunctionArgsToOutput($addTraceFunctionArgsToOutput = null) |
|
118 | { |
||
119 | 2 | if (func_num_args() == 0) { |
|
120 | 2 | return $this->addTraceFunctionArgsToOutput; |
|
121 | } |
||
122 | |||
123 | 2 | if (! is_integer($addTraceFunctionArgsToOutput)) { |
|
124 | 1 | $this->addTraceFunctionArgsToOutput = (bool) $addTraceFunctionArgsToOutput; |
|
125 | 1 | } else { |
|
126 | 2 | $this->addTraceFunctionArgsToOutput = $addTraceFunctionArgsToOutput; |
|
127 | } |
||
128 | 2 | } |
|
129 | |||
130 | /** |
||
131 | * Set the size limit in bytes of frame arguments var_dump output. |
||
132 | * If the limit is reached, the var_dump output is discarded. |
||
133 | * Prevent memory limit errors. |
||
134 | * @var integer |
||
135 | */ |
||
136 | 1 | public function setTraceFunctionArgsOutputLimit($traceFunctionArgsOutputLimit) |
|
140 | |||
141 | /** |
||
142 | * Get the size limit in bytes of frame arguments var_dump output. |
||
143 | * If the limit is reached, the var_dump output is discarded. |
||
144 | * Prevent memory limit errors. |
||
145 | * @return integer |
||
146 | */ |
||
147 | 1 | public function getTraceFunctionArgsOutputLimit() |
|
151 | |||
152 | /** |
||
153 | * Restrict error handling to command line calls. |
||
154 | * @param bool|null $onlyForCommandLine |
||
155 | * @return null|bool |
||
156 | */ |
||
157 | 1 | public function onlyForCommandLine($onlyForCommandLine = null) |
|
158 | { |
||
159 | 1 | if (func_num_args() == 0) { |
|
160 | 1 | return $this->onlyForCommandLine; |
|
161 | } |
||
162 | 1 | $this->onlyForCommandLine = (bool) $onlyForCommandLine; |
|
163 | 1 | } |
|
164 | |||
165 | /** |
||
166 | * Output the error message only if using command line. |
||
167 | * else, output to logger if available. |
||
168 | * Allow to safely add this handler to web pages. |
||
169 | * @param bool|null $outputOnlyIfCommandLine |
||
170 | * @return null|bool |
||
171 | */ |
||
172 | 1 | public function outputOnlyIfCommandLine($outputOnlyIfCommandLine = null) |
|
173 | { |
||
174 | 1 | if (func_num_args() == 0) { |
|
175 | 1 | return $this->outputOnlyIfCommandLine; |
|
176 | } |
||
177 | 1 | $this->outputOnlyIfCommandLine = (bool) $outputOnlyIfCommandLine; |
|
178 | 1 | } |
|
179 | |||
180 | /** |
||
181 | * Only output to logger. |
||
182 | * @param bool|null $loggerOnly |
||
183 | * @return null|bool |
||
184 | */ |
||
185 | 1 | public function loggerOnly($loggerOnly = null) |
|
186 | { |
||
187 | 1 | if (func_num_args() == 0) { |
|
188 | 1 | return $this->loggerOnly; |
|
189 | } |
||
190 | |||
191 | 1 | $this->loggerOnly = (bool) $loggerOnly; |
|
192 | 1 | } |
|
193 | |||
194 | /** |
||
195 | * Check, if possible, that this execution was triggered by a command line. |
||
196 | * @return bool |
||
197 | */ |
||
198 | private function isCommandLine() |
||
202 | |||
203 | /** |
||
204 | * Test if handler can process the exception.. |
||
205 | * @return bool |
||
206 | */ |
||
207 | 2 | private function canProcess() |
|
211 | |||
212 | /** |
||
213 | * Test if handler can output to stdout. |
||
214 | * @return bool |
||
215 | */ |
||
216 | 2 | private function canOutput() |
|
217 | { |
||
218 | 2 | return ($this->isCommandLine() || ! $this->outputOnlyIfCommandLine()) |
|
221 | |||
222 | /** |
||
223 | * Get the frame args var_dump. |
||
224 | * @param \Whoops\Exception\Frame $frame [description] |
||
225 | * @param integer $line [description] |
||
226 | * @return string |
||
227 | */ |
||
228 | 1 | private function getFrameArgsOutput(Frame $frame, $line) |
|
253 | |||
254 | /** |
||
255 | * Get the exception trace as plain text. |
||
256 | * @return string |
||
257 | */ |
||
258 | 2 | private function getTraceOutput() |
|
294 | |||
295 | /** |
||
296 | * @return int |
||
297 | */ |
||
298 | 3 | public function handle() |
|
331 | } |
||
332 |
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.