ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
PlainTextHandler.php
Go to the documentation of this file.
1<?php
9namespace Whoops\Handler;
10
11use InvalidArgumentException;
14
21{
22 const VAR_DUMP_PREFIX = ' | ';
23
27 protected $logger;
28
32 private $addTraceToOutput = true;
33
38
43
47 private $onlyForCommandLine = false;
48
53
57 private $loggerOnly = false;
58
64 public function __construct($logger = null)
65 {
66 $this->setLogger($logger);
67 }
68
74 public function setLogger($logger = null)
75 {
76 if (! (is_null($logger)
77 || $logger instanceof LoggerInterface)) {
78 throw new InvalidArgumentException(
79 'Argument to ' . __METHOD__ .
80 " must be a valid Logger Interface (aka. Monolog), " .
81 get_class($logger) . ' given.'
82 );
83 }
84
85 $this->logger = $logger;
86 }
87
91 public function getLogger()
92 {
93 return $this->logger;
94 }
95
101 public function addTraceToOutput($addTraceToOutput = null)
102 {
103 if (func_num_args() == 0) {
105 }
106
107 $this->addTraceToOutput = (bool) $addTraceToOutput;
108 return $this;
109 }
110
118 {
119 if (func_num_args() == 0) {
121 }
122
123 if (! is_integer($addTraceFunctionArgsToOutput)) {
125 } else {
127 }
128 }
129
137 {
138 $this->traceFunctionArgsOutputLimit = (integer) $traceFunctionArgsOutputLimit;
139 }
140
148 {
150 }
151
158 {
159 if (func_num_args() == 0) {
161 }
163 }
164
173 {
174 if (func_num_args() == 0) {
176 }
178 }
179
185 public function loggerOnly($loggerOnly = null)
186 {
187 if (func_num_args() == 0) {
188 return $this->loggerOnly;
189 }
190
191 $this->loggerOnly = (bool) $loggerOnly;
192 }
193
198 private function isCommandLine()
199 {
200 return PHP_SAPI == 'cli';
201 }
202
207 private function canProcess()
208 {
209 return $this->isCommandLine() || !$this->onlyForCommandLine();
210 }
211
216 private function canOutput()
217 {
218 return ($this->isCommandLine() || ! $this->outputOnlyIfCommandLine())
219 && ! $this->loggerOnly();
220 }
221
228 private function getFrameArgsOutput(Frame $frame, $line)
229 {
230 if ($this->addTraceFunctionArgsToOutput() === false
231 || $this->addTraceFunctionArgsToOutput() < $line) {
232 return '';
233 }
234
235 // Dump the arguments:
236 ob_start();
237 var_dump($frame->getArgs());
238 if (ob_get_length() > $this->getTraceFunctionArgsOutputLimit()) {
239 // The argument var_dump is to big.
240 // Discarded to limit memory usage.
241 ob_clean();
242 return sprintf(
243 "\n%sArguments dump length greater than %d Bytes. Discarded.",
244 self::VAR_DUMP_PREFIX,
246 );
247 }
248
249 return sprintf("\n%s",
250 preg_replace('/^/m', self::VAR_DUMP_PREFIX, ob_get_clean())
251 );
252 }
253
258 private function getTraceOutput()
259 {
260 if (! $this->addTraceToOutput()) {
261 return '';
262 }
263 $inspector = $this->getInspector();
264 $frames = $inspector->getFrames();
265
266 $response = "\nStack trace:";
267
268 $line = 1;
269 foreach ($frames as $frame) {
271 $class = $frame->getClass();
272
273 $template = "\n%3d. %s->%s() %s:%d%s";
274 if (! $class) {
275 // Remove method arrow (->) from output.
276 $template = "\n%3d. %s%s() %s:%d%s";
277 }
278
279 $response .= sprintf(
280 $template,
281 $line,
282 $class,
283 $frame->getFunction(),
284 $frame->getFile(),
285 $frame->getLine(),
286 $this->getFrameArgsOutput($frame, $line)
287 );
288
289 $line++;
290 }
291
292 return $response;
293 }
294
298 public function handle()
299 {
300 if (! $this->canProcess()) {
301 return Handler::DONE;
302 }
303
304 $exception = $this->getException();
305
306 $response = sprintf("%s: %s in file %s on line %d%s\n",
307 get_class($exception),
308 $exception->getMessage(),
309 $exception->getFile(),
310 $exception->getLine(),
311 $this->getTraceOutput()
312 );
313
314 if ($this->getLogger()) {
315 $this->getLogger()->error($response);
316 }
317
318 if (! $this->canOutput()) {
319 return Handler::DONE;
320 }
321
322 if (class_exists('\Whoops\Util\Misc')
323 && \Whoops\Util\Misc::canSendHeaders()) {
324 header('Content-Type: text/plain');
325 }
326
327 echo $response;
328
329 return Handler::QUIT;
330 }
331}
Abstract implementation of a Handler.
Definition: Handler.php:17
const DONE
Return constants that can be returned from Handler::handle to message the handler walker.
Definition: Handler.php:22
Handler outputing plaintext error messages.
getFrameArgsOutput(Frame $frame, $line)
Get the frame args var_dump.
addTraceToOutput($addTraceToOutput=null)
Add error trace to output.
loggerOnly($loggerOnly=null)
Only output to logger.
__construct($logger=null)
Constructor.
setLogger($logger=null)
Set the output logger interface.
addTraceFunctionArgsToOutput($addTraceFunctionArgsToOutput=null)
Add error trace function arguments to output.
outputOnlyIfCommandLine($outputOnlyIfCommandLine=null)
Output the error message only if using command line.
onlyForCommandLine($onlyForCommandLine=null)
Restrict error handling to command line calls.
canOutput()
Test if handler can output to stdout.
isCommandLine()
Check, if possible, that this execution was triggered by a command line.
getTraceFunctionArgsOutputLimit()
Get the size limit in bytes of frame arguments var_dump output.
setTraceFunctionArgsOutputLimit($traceFunctionArgsOutputLimit)
canProcess()
Test if handler can process the exception.
static canSendHeaders()
Can we at this point in time send HTTP headers?
Definition: Misc.php:21
Describes a logger instance.
Whoops - php errors for cool kids.
Whoops - php errors for cool kids.