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;
27:
28: use Kint\Object\BasicObject;
29: use Kint\Object\InstanceObject;
30:
31: abstract class Renderer
32: {
33: const SORT_NONE = 0;
34: const SORT_VISIBILITY = 1;
35: const SORT_FULL = 2;
36:
37: protected $call_info = array();
38: protected $statics = array();
39: protected $show_trace = true;
40:
41: abstract public function render(BasicObject $o);
42:
43: abstract public function renderNothing();
44:
45: public function setCallInfo(array $info)
46: {
47: if (!isset($info['params'])) {
48: $info['params'] = null;
49: }
50:
51: if (!isset($info['modifiers']) || !\is_array($info['modifiers'])) {
52: $info['modifiers'] = array();
53: }
54:
55: if (!isset($info['callee'])) {
56: $info['callee'] = null;
57: }
58:
59: if (!isset($info['caller'])) {
60: $info['caller'] = null;
61: }
62:
63: if (!isset($info['trace']) || !\is_array($info['trace'])) {
64: $info['trace'] = array();
65: }
66:
67: $this->call_info = array(
68: 'params' => $info['params'],
69: 'modifiers' => $info['modifiers'],
70: 'callee' => $info['callee'],
71: 'caller' => $info['caller'],
72: 'trace' => $info['trace'],
73: );
74: }
75:
76: public function getCallInfo()
77: {
78: return $this->call_info;
79: }
80:
81: public function setStatics(array $statics)
82: {
83: $this->statics = $statics;
84: $this->setShowTrace(!empty($statics['display_called_from']));
85: }
86:
87: public function getStatics()
88: {
89: return $this->statics;
90: }
91:
92: public function setShowTrace($show_trace)
93: {
94: $this->show_trace = $show_trace;
95: }
96:
97: public function getShowTrace()
98: {
99: return $this->show_trace;
100: }
101:
102: /**
103: * Returns the first compatible plugin available.
104: *
105: * @param array $plugins Array of hints to class strings
106: * @param array $hints Array of object hints
107: *
108: * @return array Array of hints to class strings filtered and sorted by object hints
109: */
110: public function matchPlugins(array $plugins, array $hints)
111: {
112: $out = array();
113:
114: foreach ($hints as $key) {
115: if (isset($plugins[$key])) {
116: $out[$key] = $plugins[$key];
117: }
118: }
119:
120: return $out;
121: }
122:
123: public function filterParserPlugins(array $plugins)
124: {
125: return $plugins;
126: }
127:
128: public function preRender()
129: {
130: return '';
131: }
132:
133: public function postRender()
134: {
135: return '';
136: }
137:
138: public static function sortPropertiesFull(BasicObject $a, BasicObject $b)
139: {
140: $sort = BasicObject::sortByAccess($a, $b);
141: if ($sort) {
142: return $sort;
143: }
144:
145: $sort = BasicObject::sortByName($a, $b);
146: if ($sort) {
147: return $sort;
148: }
149:
150: return InstanceObject::sortByHierarchy($a->owner_class, $b->owner_class);
151: }
152:
153: /**
154: * Sorts an array of BasicObject.
155: *
156: * @param BasicObject[] $contents Object properties to sort
157: * @param int $sort
158: *
159: * @return BasicObject[]
160: */
161: public static function sortProperties(array $contents, $sort)
162: {
163: switch ($sort) {
164: case self::SORT_VISIBILITY:
165: /** @var array<array-key, BasicObject[]> Containers to quickly stable sort by type */
166: $containers = array(
167: BasicObject::ACCESS_PUBLIC => array(),
168: BasicObject::ACCESS_PROTECTED => array(),
169: BasicObject::ACCESS_PRIVATE => array(),
170: BasicObject::ACCESS_NONE => array(),
171: );
172:
173: foreach ($contents as $item) {
174: $containers[$item->access][] = $item;
175: }
176:
177: return \call_user_func_array('array_merge', $containers);
178: case self::SORT_FULL:
179: \usort($contents, array('Kint\\Renderer\\Renderer', 'sortPropertiesFull'));
180: // no break
181: default:
182: return $contents;
183: }
184: }
185: }
186: