1: <?php
2:
3: /*
4: * The MIT License (MIT)
5: *
6: * Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
7: *
8: * Permission is hereby granted, free of charge, to any person obtaining a copy of
9: * this software and associated documentation files (the "Software"), to deal in
10: * the Software without restriction, including without limitation the rights to
11: * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12: * the Software, and to permit persons to whom the Software is furnished to do so,
13: * subject to the following conditions:
14: *
15: * The above copyright notice and this permission notice shall be included in all
16: * copies or substantial portions of the Software.
17: *
18: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20: * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21: * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22: * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23: * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24: */
25:
26: namespace Kint\Renderer\Text;
27:
28: use Kint\Object\BasicObject;
29: use Kint\Object\MethodObject;
30:
31: class TracePlugin extends Plugin
32: {
33: public function render(BasicObject $o)
34: {
35: $out = '';
36:
37: if (0 == $o->depth) {
38: $out .= $this->renderer->colorTitle($this->renderer->renderTitle($o)).PHP_EOL;
39: }
40:
41: $out .= $this->renderer->renderHeader($o).':'.PHP_EOL;
42:
43: $indent = \str_repeat(' ', ($o->depth + 1) * $this->renderer->indent_width);
44:
45: $i = 1;
46: foreach ($o->value->contents as $frame) {
47: $framedesc = $indent.\str_pad($i.': ', 4, ' ');
48:
49: if ($frame->trace['file']) {
50: $framedesc .= $this->renderer->ideLink($frame->trace['file'], $frame->trace['line']).PHP_EOL;
51: } else {
52: $framedesc .= 'PHP internal call'.PHP_EOL;
53: }
54:
55: $framedesc .= $indent.' ';
56:
57: if ($frame->trace['class']) {
58: $framedesc .= $this->renderer->escape($frame->trace['class']);
59:
60: if ($frame->trace['object']) {
61: $framedesc .= $this->renderer->escape('->');
62: } else {
63: $framedesc .= '::';
64: }
65: }
66:
67: if (\is_string($frame->trace['function'])) {
68: $framedesc .= $this->renderer->escape($frame->trace['function']).'(...)';
69: } elseif ($frame->trace['function'] instanceof MethodObject) {
70: $framedesc .= $this->renderer->escape($frame->trace['function']->getName());
71: $framedesc .= '('.$this->renderer->escape($frame->trace['function']->getParams()).')';
72: }
73:
74: $out .= $this->renderer->colorType($framedesc).PHP_EOL.PHP_EOL;
75:
76: if ($source = $frame->getRepresentation('source')) {
77: $line_wanted = $source->line;
78: $source = $source->source;
79:
80: // Trim empty lines from the start and end of the source
81: foreach ($source as $linenum => $line) {
82: if (\trim($line) || $linenum === $line_wanted) {
83: break;
84: }
85:
86: unset($source[$linenum]);
87: }
88:
89: foreach (\array_reverse($source, true) as $linenum => $line) {
90: if (\trim($line) || $linenum === $line_wanted) {
91: break;
92: }
93:
94: unset($source[$linenum]);
95: }
96:
97: foreach ($source as $lineno => $line) {
98: if ($lineno == $line_wanted) {
99: $out .= $indent.$this->renderer->colorValue($this->renderer->escape($line)).PHP_EOL;
100: } else {
101: $out .= $indent.$this->renderer->escape($line).PHP_EOL;
102: }
103: }
104: }
105:
106: ++$i;
107: }
108:
109: return $out;
110: }
111: }
112: