PageRenderTime 75ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/var-dumper/Dumper/HtmlDumper.php

https://gitlab.com/Urtekin/ertexAdmin
PHP | 452 lines | 348 code | 53 blank | 51 comment | 79 complexity | 9f2742a4df5e4cf54f01b937969284c1 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarDumper\Dumper;
  11. use Symfony\Component\VarDumper\Cloner\Cursor;
  12. use Symfony\Component\VarDumper\Cloner\Data;
  13. /**
  14. * HtmlDumper dumps variables as HTML.
  15. *
  16. * @author Nicolas Grekas <p@tchwork.com>
  17. */
  18. class HtmlDumper extends CliDumper
  19. {
  20. public static $defaultOutput = 'php://output';
  21. protected $dumpHeader;
  22. protected $dumpPrefix = '<pre class=sf-dump id=%s data-indent-pad="%s">';
  23. protected $dumpSuffix = '</pre><script>Sfdump("%s")</script>';
  24. protected $dumpId = 'sf-dump';
  25. protected $colors = true;
  26. protected $headerIsDumped = false;
  27. protected $lastDepth = -1;
  28. protected $styles = array(
  29. 'default' => 'background-color:#18171B; color:#FF8400; line-height:1.2em; font:12px Menlo, Monaco, Consolas, monospace; word-wrap: break-word; white-space: pre-wrap; position:relative; z-index:99999; word-break: normal',
  30. 'num' => 'font-weight:bold; color:#1299DA',
  31. 'const' => 'font-weight:bold',
  32. 'str' => 'font-weight:bold; color:#56DB3A',
  33. 'note' => 'color:#1299DA',
  34. 'ref' => 'color:#A0A0A0',
  35. 'public' => 'color:#FFFFFF',
  36. 'protected' => 'color:#FFFFFF',
  37. 'private' => 'color:#FFFFFF',
  38. 'meta' => 'color:#B729D9',
  39. 'key' => 'color:#56DB3A',
  40. 'index' => 'color:#1299DA',
  41. );
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function __construct($output = null, $charset = null)
  46. {
  47. AbstractDumper::__construct($output, $charset);
  48. $this->dumpId = 'sf-dump-'.mt_rand();
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function setOutput($output)
  54. {
  55. if ($output !== $prev = parent::setOutput($output)) {
  56. $this->headerIsDumped = false;
  57. }
  58. return $prev;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function setStyles(array $styles)
  64. {
  65. $this->headerIsDumped = false;
  66. $this->styles = $styles + $this->styles;
  67. }
  68. /**
  69. * Sets an HTML header that will be dumped once in the output stream.
  70. *
  71. * @param string $header An HTML string.
  72. */
  73. public function setDumpHeader($header)
  74. {
  75. $this->dumpHeader = $header;
  76. }
  77. /**
  78. * Sets an HTML prefix and suffix that will encapse every single dump.
  79. *
  80. * @param string $prefix The prepended HTML string.
  81. * @param string $suffix The appended HTML string.
  82. */
  83. public function setDumpBoundaries($prefix, $suffix)
  84. {
  85. $this->dumpPrefix = $prefix;
  86. $this->dumpSuffix = $suffix;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function dump(Data $data, $output = null)
  92. {
  93. parent::dump($data, $output);
  94. $this->dumpId = 'sf-dump-'.mt_rand();
  95. }
  96. /**
  97. * Dumps the HTML header.
  98. */
  99. protected function getDumpHeader()
  100. {
  101. $this->headerIsDumped = true;
  102. if (null !== $this->dumpHeader) {
  103. return $this->dumpHeader;
  104. }
  105. $line = <<<'EOHTML'
  106. <script>
  107. Sfdump = window.Sfdump || (function (doc) {
  108. var refStyle = doc.createElement('style'),
  109. rxEsc = /([.*+?^${}()|\[\]\/\\])/g,
  110. idRx = /\bsf-dump-\d+-ref[012]\w+\b/,
  111. keyHint = 0 <= navigator.platform.toUpperCase().indexOf('MAC') ? 'Cmd' : 'Ctrl',
  112. addEventListener = function (e, n, cb) {
  113. e.addEventListener(n, cb, false);
  114. };
  115. (doc.documentElement.firstElementChild || doc.documentElement.children[0]).appendChild(refStyle);
  116. if (!doc.addEventListener) {
  117. addEventListener = function (element, eventName, callback) {
  118. element.attachEvent('on' + eventName, function (e) {
  119. e.preventDefault = function () {e.returnValue = false;};
  120. e.target = e.srcElement;
  121. callback(e);
  122. });
  123. };
  124. }
  125. function toggle(a, recursive) {
  126. var s = a.nextSibling || {}, oldClass = s.className, arrow, newClass;
  127. if ('sf-dump-compact' == oldClass) {
  128. arrow = '▼';
  129. newClass = 'sf-dump-expanded';
  130. } else if ('sf-dump-expanded' == oldClass) {
  131. arrow = '▶';
  132. newClass = 'sf-dump-compact';
  133. } else {
  134. return false;
  135. }
  136. a.lastChild.innerHTML = arrow;
  137. s.className = newClass;
  138. if (recursive) {
  139. try {
  140. a = s.querySelectorAll('.'+oldClass);
  141. for (s = 0; s < a.length; ++s) {
  142. if (a[s].className !== newClass) {
  143. a[s].className = newClass;
  144. a[s].previousSibling.lastChild.innerHTML = arrow;
  145. }
  146. }
  147. } catch (e) {
  148. }
  149. }
  150. return true;
  151. };
  152. return function (root) {
  153. root = doc.getElementById(root);
  154. function a(e, f) {
  155. addEventListener(root, e, function (e) {
  156. if ('A' == e.target.tagName) {
  157. f(e.target, e);
  158. } else if ('A' == e.target.parentNode.tagName) {
  159. f(e.target.parentNode, e);
  160. }
  161. });
  162. };
  163. function isCtrlKey(e) {
  164. return e.ctrlKey || e.metaKey;
  165. }
  166. addEventListener(root, 'mouseover', function (e) {
  167. if ('' != refStyle.innerHTML) {
  168. refStyle.innerHTML = '';
  169. }
  170. });
  171. a('mouseover', function (a) {
  172. if (a = idRx.exec(a.className)) {
  173. try {
  174. refStyle.innerHTML = 'pre.sf-dump .'+a[0]+'{background-color: #B729D9; color: #FFF !important; border-radius: 2px}';
  175. } catch (e) {
  176. }
  177. }
  178. });
  179. a('click', function (a, e) {
  180. if (/\bsf-dump-toggle\b/.test(a.className)) {
  181. e.preventDefault();
  182. if (!toggle(a, isCtrlKey(e))) {
  183. var r = doc.getElementById(a.getAttribute('href').substr(1)),
  184. s = r.previousSibling,
  185. f = r.parentNode,
  186. t = a.parentNode;
  187. t.replaceChild(r, a);
  188. f.replaceChild(a, s);
  189. t.insertBefore(s, r);
  190. f = f.firstChild.nodeValue.match(indentRx);
  191. t = t.firstChild.nodeValue.match(indentRx);
  192. if (f && t && f[0] !== t[0]) {
  193. r.innerHTML = r.innerHTML.replace(new RegExp('^'+f[0].replace(rxEsc, '\\$1'), 'mg'), t[0]);
  194. }
  195. if ('sf-dump-compact' == r.className) {
  196. toggle(s, isCtrlKey(e));
  197. }
  198. }
  199. if (doc.getSelection) {
  200. try {
  201. doc.getSelection().removeAllRanges();
  202. } catch (e) {
  203. doc.getSelection().empty();
  204. }
  205. } else {
  206. doc.selection.empty();
  207. }
  208. }
  209. });
  210. var indentRx = new RegExp('^('+(root.getAttribute('data-indent-pad') || ' ').replace(rxEsc, '\\$1')+')+', 'm'),
  211. elt = root.getElementsByTagName('A'),
  212. len = elt.length,
  213. i = 0,
  214. t = [];
  215. while (i < len) t.push(elt[i++]);
  216. elt = root.getElementsByTagName('SAMP');
  217. len = elt.length;
  218. i = 0;
  219. while (i < len) t.push(elt[i++]);
  220. root = t;
  221. len = t.length;
  222. i = t = 0;
  223. while (i < len) {
  224. elt = root[i];
  225. if ("SAMP" == elt.tagName) {
  226. elt.className = "sf-dump-expanded";
  227. a = elt.previousSibling || {};
  228. if ('A' != a.tagName) {
  229. a = doc.createElement('A');
  230. a.className = 'sf-dump-ref';
  231. elt.parentNode.insertBefore(a, elt);
  232. } else {
  233. a.innerHTML += ' ';
  234. }
  235. a.title = (a.title ? a.title+'\n[' : '[')+keyHint+'+click] Expand all children';
  236. a.innerHTML += '<span>▼</span>';
  237. a.className += ' sf-dump-toggle';
  238. if ('sf-dump' != elt.parentNode.className) {
  239. toggle(a);
  240. }
  241. } else if ("sf-dump-ref" == elt.className && (a = elt.getAttribute('href'))) {
  242. a = a.substr(1);
  243. elt.className += ' '+a;
  244. if (/[\[{]$/.test(elt.previousSibling.nodeValue)) {
  245. a = a != elt.nextSibling.id && doc.getElementById(a);
  246. try {
  247. t = a.nextSibling;
  248. elt.appendChild(a);
  249. t.parentNode.insertBefore(a, t);
  250. if (/^[@#]/.test(elt.innerHTML)) {
  251. elt.innerHTML += ' <span>▶</span>';
  252. } else {
  253. elt.innerHTML = '<span>▶</span>';
  254. elt.className = 'sf-dump-ref';
  255. }
  256. elt.className += ' sf-dump-toggle';
  257. } catch (e) {
  258. if ('&' == elt.innerHTML.charAt(0)) {
  259. elt.innerHTML = '…';
  260. elt.className = 'sf-dump-ref';
  261. }
  262. }
  263. }
  264. }
  265. ++i;
  266. }
  267. };
  268. })(document);
  269. </script>
  270. <style>
  271. pre.sf-dump {
  272. display: block;
  273. white-space: pre;
  274. padding: 5px;
  275. }
  276. pre.sf-dump span {
  277. display: inline;
  278. }
  279. pre.sf-dump .sf-dump-compact {
  280. display: none;
  281. }
  282. pre.sf-dump abbr {
  283. text-decoration: none;
  284. border: none;
  285. cursor: help;
  286. }
  287. pre.sf-dump a {
  288. text-decoration: none;
  289. cursor: pointer;
  290. border: 0;
  291. outline: none;
  292. }
  293. EOHTML;
  294. foreach ($this->styles as $class => $style) {
  295. $line .= 'pre.sf-dump'.('default' !== $class ? ' .sf-dump-'.$class : '').'{'.$style.'}';
  296. }
  297. return $this->dumpHeader = preg_replace('/\s+/', ' ', $line).'</style>'.$this->dumpHeader;
  298. }
  299. /**
  300. * {@inheritdoc}
  301. */
  302. public function enterHash(Cursor $cursor, $type, $class, $hasChild)
  303. {
  304. parent::enterHash($cursor, $type, $class, false);
  305. if ($hasChild) {
  306. if ($cursor->refIndex) {
  307. $r = Cursor::HASH_OBJECT !== $type ? 1 - (Cursor::HASH_RESOURCE !== $type) : 2;
  308. $r .= $r && 0 < $cursor->softRefHandle ? $cursor->softRefHandle : $cursor->refIndex;
  309. $this->line .= sprintf('<samp id=%s-ref%s>', $this->dumpId, $r);
  310. } else {
  311. $this->line .= '<samp>';
  312. }
  313. $this->dumpLine($cursor->depth);
  314. }
  315. }
  316. /**
  317. * {@inheritdoc}
  318. */
  319. public function leaveHash(Cursor $cursor, $type, $class, $hasChild, $cut)
  320. {
  321. $this->dumpEllipsis($cursor, $hasChild, $cut);
  322. if ($hasChild) {
  323. $this->line .= '</samp>';
  324. }
  325. parent::leaveHash($cursor, $type, $class, $hasChild, 0);
  326. }
  327. /**
  328. * {@inheritdoc}
  329. */
  330. protected function style($style, $value, $attr = array())
  331. {
  332. if ('' === $value) {
  333. return '';
  334. }
  335. $v = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
  336. if ('ref' === $style) {
  337. if (empty($attr['count'])) {
  338. return sprintf('<a class=sf-dump-ref>%s</a>', $v);
  339. }
  340. $r = ('#' !== $v[0] ? 1 - ('@' !== $v[0]) : 2).substr($value, 1);
  341. return sprintf('<a class=sf-dump-ref href=#%s-ref%s title="%d occurrences">%s</a>', $this->dumpId, $r, 1 + $attr['count'], $v);
  342. }
  343. if ('const' === $style && array_key_exists('value', $attr)) {
  344. $style .= sprintf(' title="%s"', htmlspecialchars(json_encode($attr['value']), ENT_QUOTES, 'UTF-8'));
  345. } elseif ('public' === $style) {
  346. $style .= sprintf(' title="%s"', empty($attr['dynamic']) ? 'Public property' : 'Runtime added dynamic property');
  347. } elseif ('str' === $style && 1 < $attr['length']) {
  348. $style .= sprintf(' title="%s%s characters"', $attr['length'], $attr['binary'] ? ' binary or non-UTF-8' : '');
  349. } elseif ('note' === $style && false !== $c = strrpos($v, '\\')) {
  350. return sprintf('<abbr title="%s" class=sf-dump-%s>%s</abbr>', $v, $style, substr($v, $c + 1));
  351. } elseif ('protected' === $style) {
  352. $style .= ' title="Protected property"';
  353. } elseif ('private' === $style) {
  354. $style .= sprintf(' title="Private property defined in class:&#10;`%s`"', $attr['class']);
  355. }
  356. $map = static::$controlCharsMap;
  357. $style = "<span class=sf-dump-{$style}>";
  358. $v = preg_replace_callback(static::$controlCharsRx, function ($c) use ($map, $style) {
  359. $s = '</span>';
  360. $c = $c[$i = 0];
  361. do {
  362. $s .= isset($map[$c[$i]]) ? $map[$c[$i]] : sprintf('\x%02X', ord($c[$i]));
  363. } while (isset($c[++$i]));
  364. return $s.$style;
  365. }, $v, -1, $cchrCount);
  366. if ($cchrCount && '<' === $v[0]) {
  367. $v = substr($v, 7);
  368. } else {
  369. $v = $style.$v;
  370. }
  371. if ($cchrCount && '>' === substr($v, -1)) {
  372. $v = substr($v, 0, -strlen($style));
  373. } else {
  374. $v .= '</span>';
  375. }
  376. return $v;
  377. }
  378. /**
  379. * {@inheritdoc}
  380. */
  381. protected function dumpLine($depth, $endOfValue = false)
  382. {
  383. if (-1 === $this->lastDepth) {
  384. $this->line = sprintf($this->dumpPrefix, $this->dumpId, $this->indentPad).$this->line;
  385. }
  386. if (!$this->headerIsDumped) {
  387. $this->line = $this->getDumpHeader().$this->line;
  388. }
  389. if (-1 === $depth) {
  390. $this->line .= sprintf($this->dumpSuffix, $this->dumpId);
  391. }
  392. $this->lastDepth = $depth;
  393. $this->line = mb_convert_encoding($this->line, 'HTML-ENTITIES', 'UTF-8');
  394. if (-1 === $depth) {
  395. AbstractDumper::dumpLine(0);
  396. }
  397. AbstractDumper::dumpLine($depth);
  398. }
  399. }