ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 protected $dumper;
33
37 private $addTraceToOutput = true;
38
43
48
52 private $loggerOnly = false;
53
59 public function __construct($logger = null)
60 {
61 $this->setLogger($logger);
62 }
63
69 public function setLogger($logger = null)
70 {
71 if (! (is_null($logger)
72 || $logger instanceof LoggerInterface)) {
73 throw new InvalidArgumentException(
74 'Argument to ' . __METHOD__ .
75 " must be a valid Logger Interface (aka. Monolog), " .
76 get_class($logger) . ' given.'
77 );
78 }
79
80 $this->logger = $logger;
81 }
82
86 public function getLogger()
87 {
88 return $this->logger;
89 }
90
97 public function setDumper(callable $dumper)
98 {
99 $this->dumper = $dumper;
100 }
101
107 public function addTraceToOutput($addTraceToOutput = null)
108 {
109 if (func_num_args() == 0) {
111 }
112
113 $this->addTraceToOutput = (bool) $addTraceToOutput;
114 return $this;
115 }
116
124 {
125 if (func_num_args() == 0) {
127 }
128
129 if (! is_integer($addTraceFunctionArgsToOutput)) {
131 } else {
133 }
134 }
135
143 {
144 $this->traceFunctionArgsOutputLimit = (integer) $traceFunctionArgsOutputLimit;
145 }
146
151 public function generateResponse()
152 {
153 $exception = $this->getException();
154 return sprintf(
155 "%s: %s in file %s on line %d%s\n",
156 get_class($exception),
157 $exception->getMessage(),
158 $exception->getFile(),
159 $exception->getLine(),
160 $this->getTraceOutput()
161 );
162 }
163
171 {
173 }
174
180 public function loggerOnly($loggerOnly = null)
181 {
182 if (func_num_args() == 0) {
183 return $this->loggerOnly;
184 }
185
186 $this->loggerOnly = (bool) $loggerOnly;
187 }
188
193 private function canOutput()
194 {
195 return !$this->loggerOnly();
196 }
197
204 private function getFrameArgsOutput(Frame $frame, $line)
205 {
206 if ($this->addTraceFunctionArgsToOutput() === false
207 || $this->addTraceFunctionArgsToOutput() < $line) {
208 return '';
209 }
210
211 // Dump the arguments:
212 ob_start();
213 $this->dump($frame->getArgs());
214 if (ob_get_length() > $this->getTraceFunctionArgsOutputLimit()) {
215 // The argument var_dump is to big.
216 // Discarded to limit memory usage.
217 ob_clean();
218 return sprintf(
219 "\n%sArguments dump length greater than %d Bytes. Discarded.",
220 self::VAR_DUMP_PREFIX,
222 );
223 }
224
225 return sprintf(
226 "\n%s",
227 preg_replace('/^/m', self::VAR_DUMP_PREFIX, ob_get_clean())
228 );
229 }
230
237 protected function dump($var)
238 {
239 if ($this->dumper) {
240 call_user_func($this->dumper, $var);
241 } else {
242 var_dump($var);
243 }
244 }
245
250 private function getTraceOutput()
251 {
252 if (! $this->addTraceToOutput()) {
253 return '';
254 }
255 $inspector = $this->getInspector();
256 $frames = $inspector->getFrames();
257
258 $response = "\nStack trace:";
259
260 $line = 1;
261 foreach ($frames as $frame) {
263 $class = $frame->getClass();
264
265 $template = "\n%3d. %s->%s() %s:%d%s";
266 if (! $class) {
267 // Remove method arrow (->) from output.
268 $template = "\n%3d. %s%s() %s:%d%s";
269 }
270
271 $response .= sprintf(
272 $template,
273 $line,
274 $class,
275 $frame->getFunction(),
276 $frame->getFile(),
277 $frame->getLine(),
278 $this->getFrameArgsOutput($frame, $line)
279 );
280
281 $line++;
282 }
283
284 return $response;
285 }
286
290 public function handle()
291 {
292 $response = $this->generateResponse();
293
294 if ($this->getLogger()) {
295 $this->getLogger()->error($response);
296 }
297
298 if (! $this->canOutput()) {
299 return Handler::DONE;
300 }
301
302 echo $response;
303
304 return Handler::QUIT;
305 }
306
310 public function contentType()
311 {
312 return 'text/plain';
313 }
314}
sprintf('%.4f', $callTime)
An exception for terminatinating execution or to throw for unit testing.
Abstract implementation of a Handler.
Definition: Handler.php:16
const QUIT
The Handler has handled the Throwable in some way, and wishes to quit/stop execution.
Definition: Handler.php:31
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.
setDumper(callable $dumper)
Set var dumper callback function.
generateResponse()
Create plain text response and return it as a string.
canOutput()
Test if handler can output to stdout.
getTraceFunctionArgsOutputLimit()
Get the size limit in bytes of frame arguments var_dump output.
setTraceFunctionArgsOutputLimit($traceFunctionArgsOutputLimit)
Describes a logger instance.
Whoops - php errors for cool kids.