1: <?php
2:
3: namespace PHPixie\Debug;
4:
5: class Messages
6: {
7: protected $builder;
8:
9: public function __construct($builder)
10: {
11: $this->builder = $builder;
12: }
13:
14: public function exception($exception, $backtraceLimit = null, $neighboringLines = 5)
15: {
16: $dumper = $this->builder->dumper();
17: $tracer = $this->builder->tracer();
18:
19: $string = $dumper->dump($exception);
20:
21: if($backtraceLimit !== 0) {
22: $trace = $tracer->exceptionTrace($exception, $backtraceLimit);
23:
24: if($neighboringLines > 0) {
25: $elements = $trace->elements();
26: $string.= "\n\n".$this->neighboringLines($elements[0], $neighboringLines);
27: }
28:
29: $string.= "\n\n".$trace->asString();
30: }
31:
32: return $string;
33: }
34:
35: public function log($withTitle = true, $withTraceArguments = true, $shortValueDump = null)
36: {
37: $string = '';
38: if($withTitle) {
39: $string.= "Logged items:\n\n";
40: }
41:
42: $logger = $this->builder->logger();
43: $string.= $logger->asString($withTraceArguments, $shortValueDump);
44:
45: return $string;
46: }
47:
48: public function neighboringLines($traceElement, $amount = 5)
49: {
50: $offsets = $traceElement->getNeighboringOffsets($amount);
51: if(count($offsets) === 0) {
52: return '';
53: }
54:
55: $string = '';
56: $pad = strlen($traceElement->line(end($offsets)));
57: foreach($offsets as $key => $offset) {
58: if($offset !== 0) {
59: $prefix = $traceElement->line($offset);
60: $prefix = str_pad($prefix, $pad);
61: }else{
62: $prefix = str_pad('', $pad, '>');
63: }
64:
65: if($key > 0) {
66: $string.= "\n";
67: }
68: $string.= $prefix.' '.$traceElement->lineContents($offset);
69: }
70:
71: return $string;
72: }
73: }