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\Object;
27:
28: use Kint\Object\Representation\Representation;
29: use Kint\Object\Representation\SourceRepresentation;
30: use ReflectionFunction;
31: use ReflectionMethod;
32:
33: class TraceFrameObject extends BasicObject
34: {
35: public $trace;
36: public $hints = array('trace_frame');
37:
38: public function __construct(BasicObject $base, array $raw_frame)
39: {
40: parent::__construct();
41:
42: $this->transplant($base);
43:
44: $this->trace = array(
45: 'function' => isset($raw_frame['function']) ? $raw_frame['function'] : null,
46: 'line' => isset($raw_frame['line']) ? $raw_frame['line'] : null,
47: 'file' => isset($raw_frame['file']) ? $raw_frame['file'] : null,
48: 'class' => isset($raw_frame['class']) ? $raw_frame['class'] : null,
49: 'type' => isset($raw_frame['type']) ? $raw_frame['type'] : null,
50: 'object' => null,
51: 'args' => null,
52: );
53:
54: if ($this->trace['class'] && \method_exists($this->trace['class'], $this->trace['function'])) {
55: $func = new ReflectionMethod($this->trace['class'], $this->trace['function']);
56: $this->trace['function'] = new MethodObject($func);
57: } elseif (!$this->trace['class'] && \function_exists($this->trace['function'])) {
58: $func = new ReflectionFunction($this->trace['function']);
59: $this->trace['function'] = new MethodObject($func);
60: }
61:
62: foreach ($this->value->contents as $frame_prop) {
63: if ('object' === $frame_prop->name) {
64: $this->trace['object'] = $frame_prop;
65: $this->trace['object']->name = null;
66: $this->trace['object']->operator = BasicObject::OPERATOR_NONE;
67: }
68: if ('args' === $frame_prop->name) {
69: $this->trace['args'] = $frame_prop->value->contents;
70:
71: if ($this->trace['function'] instanceof MethodObject) {
72: foreach (\array_values($this->trace['function']->parameters) as $param) {
73: if (isset($this->trace['args'][$param->position])) {
74: $this->trace['args'][$param->position]->name = $param->getName();
75: }
76: }
77: }
78: }
79: }
80:
81: $this->clearRepresentations();
82:
83: if (isset($this->trace['file'], $this->trace['line']) && \is_readable($this->trace['file'])) {
84: $this->addRepresentation(new SourceRepresentation($this->trace['file'], $this->trace['line']));
85: }
86:
87: if ($this->trace['args']) {
88: $args = new Representation('Arguments');
89: $args->contents = $this->trace['args'];
90: $this->addRepresentation($args);
91: }
92:
93: if ($this->trace['object']) {
94: $callee = new Representation('object');
95: $callee->label = 'Callee object ['.$this->trace['object']->classname.']';
96: $callee->contents[] = $this->trace['object'];
97: $this->addRepresentation($callee);
98: }
99: }
100: }
101: