ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PlainTextHandler.php
Go to the documentation of this file.
1 <?php
9 namespace Whoops\Handler;
10 
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 }
Handler outputing plaintext error messages.
$template
Whoops - php errors for cool kids.
setDumper(callable $dumper)
Set var dumper callback function.
getTraceFunctionArgsOutputLimit()
Get the size limit in bytes of frame arguments var_dump output.
addTraceFunctionArgsToOutput($addTraceFunctionArgsToOutput=null)
Add error trace function arguments to output.
const QUIT
The Handler has handled the Throwable in some way, and wishes to quit/stop execution.
Definition: Handler.php:31
setTraceFunctionArgsOutputLimit($traceFunctionArgsOutputLimit)
Abstract implementation of a Handler.
Definition: Handler.php:15
Describes a logger instance.
setLogger($logger=null)
Set the output logger interface.
canOutput()
Test if handler can output to stdout.
loggerOnly($loggerOnly=null)
Only output to logger.
$response
addTraceToOutput($addTraceToOutput=null)
Add error trace to output.
getFrameArgsOutput(Frame $frame, $line)
Get the frame args var_dump.
generateResponse()
Create plain text response and return it as a string.
__construct($logger=null)
Constructor.