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\Kint;
29: use Kint\Object\BasicObject;
30: use Kint\Object\BlobObject;
31:
32: class PlainRenderer extends TextRenderer
33: {
34: public static $pre_render_sources = array(
35: 'script' => array(
36: array('Kint\\Renderer\\PlainRenderer', 'renderJs'),
37: array('Kint\\Renderer\\Text\\MicrotimePlugin', 'renderJs'),
38: ),
39: 'style' => array(
40: array('Kint\\Renderer\\PlainRenderer', 'renderCss'),
41: ),
42: 'raw' => array(),
43: );
44:
45: /**
46: * Path to the CSS file to load by default.
47: *
48: * @var string
49: */
50: public static $theme = 'plain.css';
51:
52: /**
53: * Output htmlentities instead of utf8.
54: *
55: * @var bool
56: */
57: public static $disable_utf8 = false;
58:
59: public static $needs_pre_render = true;
60:
61: public static $always_pre_render = false;
62:
63: protected $force_pre_render = false;
64: protected $pre_render;
65:
66: public function __construct()
67: {
68: parent::__construct();
69:
70: $this->pre_render = self::$needs_pre_render;
71:
72: if (self::$always_pre_render) {
73: $this->setPreRender(true);
74: }
75: }
76:
77: public function setCallInfo(array $info)
78: {
79: parent::setCallInfo($info);
80:
81: if (\in_array('@', $this->call_info['modifiers'], true)) {
82: $this->setPreRender(true);
83: }
84: }
85:
86: public function setStatics(array $statics)
87: {
88: parent::setStatics($statics);
89:
90: if (!empty($statics['return'])) {
91: $this->setPreRender(true);
92: }
93: }
94:
95: public function setPreRender($pre_render)
96: {
97: $this->pre_render = $pre_render;
98: $this->force_pre_render = true;
99: }
100:
101: public function getPreRender()
102: {
103: return $this->pre_render;
104: }
105:
106: public function colorValue($string)
107: {
108: return '<i>'.$string.'</i>';
109: }
110:
111: public function colorType($string)
112: {
113: return '<b>'.$string.'</b>';
114: }
115:
116: public function colorTitle($string)
117: {
118: return '<u>'.$string.'</u>';
119: }
120:
121: public function renderTitle(BasicObject $o)
122: {
123: if (self::$disable_utf8) {
124: return $this->utf8ToHtmlentity(parent::renderTitle($o));
125: }
126:
127: return parent::renderTitle($o);
128: }
129:
130: public function preRender()
131: {
132: $output = '';
133:
134: if ($this->pre_render) {
135: foreach (self::$pre_render_sources as $type => $values) {
136: $contents = '';
137: foreach ($values as $v) {
138: $contents .= \call_user_func($v, $this);
139: }
140:
141: if (!\strlen($contents)) {
142: continue;
143: }
144:
145: switch ($type) {
146: case 'script':
147: $output .= '<script class="kint-plain-script">'.$contents.'</script>';
148: break;
149: case 'style':
150: $output .= '<style class="kint-plain-style">'.$contents.'</style>';
151: break;
152: default:
153: $output .= $contents;
154: }
155: }
156:
157: // Don't pre-render on every dump
158: if (!$this->force_pre_render) {
159: self::$needs_pre_render = false;
160: }
161: }
162:
163: return $output.'<div class="kint-plain">';
164: }
165:
166: public function postRender()
167: {
168: if (self::$disable_utf8) {
169: return $this->utf8ToHtmlentity(parent::postRender()).'</div>';
170: }
171:
172: return parent::postRender().'</div>';
173: }
174:
175: public function ideLink($file, $line)
176: {
177: $path = $this->escape(Kint::shortenPath($file)).':'.$line;
178: $ideLink = Kint::getIdeLink($file, $line);
179:
180: if (!$ideLink) {
181: return $path;
182: }
183:
184: $class = '';
185:
186: if (\preg_match('/https?:\\/\\//i', $ideLink)) {
187: $class = 'class="kint-ide-link" ';
188: }
189:
190: return '<a '.$class.'href="'.$this->escape($ideLink).'">'.$path.'</a>';
191: }
192:
193: public function escape($string, $encoding = false)
194: {
195: if (false === $encoding) {
196: $encoding = BlobObject::detectEncoding($string);
197: }
198:
199: $original_encoding = $encoding;
200:
201: if (false === $encoding || 'ASCII' === $encoding) {
202: $encoding = 'UTF-8';
203: }
204:
205: $string = \htmlspecialchars($string, ENT_NOQUOTES, $encoding);
206:
207: // this call converts all non-ASCII characters into numeirc htmlentities
208: if (\function_exists('mb_encode_numericentity') && 'ASCII' !== $original_encoding) {
209: $string = \mb_encode_numericentity($string, array(0x80, 0xffff, 0, 0xffff), $encoding);
210: }
211:
212: return $string;
213: }
214:
215: protected function utf8ToHtmlentity($string)
216: {
217: return \str_replace(
218: array('┌', '═', '┐', '│', '└', '─', '┘'),
219: array('&#9484;', '&#9552;', '&#9488;', '&#9474;', '&#9492;', '&#9472;', '&#9496;'),
220: $string
221: );
222: }
223:
224: protected static function renderJs()
225: {
226: return \file_get_contents(KINT_DIR.'/resources/compiled/shared.js').\file_get_contents(KINT_DIR.'/resources/compiled/plain.js');
227: }
228:
229: protected static function renderCss()
230: {
231: if (\file_exists(KINT_DIR.'/resources/compiled/'.self::$theme)) {
232: return \file_get_contents(KINT_DIR.'/resources/compiled/'.self::$theme);
233: }
234:
235: return \file_get_contents(self::$theme);
236: }
237: }
238: