HtmlDumper.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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: break-all',
  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. 'ellipsis' => 'color:#FF8400',
  42. );
  43. private $displayOptions = array(
  44. 'maxDepth' => 1,
  45. 'maxStringLength' => 160,
  46. 'fileLinkFormat' => null,
  47. );
  48. private $extraDisplayOptions = array();
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function __construct($output = null, $charset = null, $flags = 0)
  53. {
  54. AbstractDumper::__construct($output, $charset, $flags);
  55. $this->dumpId = 'sf-dump-'.mt_rand();
  56. $this->displayOptions['fileLinkFormat'] = ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function setStyles(array $styles)
  62. {
  63. $this->headerIsDumped = false;
  64. $this->styles = $styles + $this->styles;
  65. }
  66. /**
  67. * Configures display options.
  68. *
  69. * @param array $displayOptions A map of display options to customize the behavior
  70. */
  71. public function setDisplayOptions(array $displayOptions)
  72. {
  73. $this->headerIsDumped = false;
  74. $this->displayOptions = $displayOptions + $this->displayOptions;
  75. }
  76. /**
  77. * Sets an HTML header that will be dumped once in the output stream.
  78. *
  79. * @param string $header An HTML string
  80. */
  81. public function setDumpHeader($header)
  82. {
  83. $this->dumpHeader = $header;
  84. }
  85. /**
  86. * Sets an HTML prefix and suffix that will encapse every single dump.
  87. *
  88. * @param string $prefix The prepended HTML string
  89. * @param string $suffix The appended HTML string
  90. */
  91. public function setDumpBoundaries($prefix, $suffix)
  92. {
  93. $this->dumpPrefix = $prefix;
  94. $this->dumpSuffix = $suffix;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function dump(Data $data, $output = null, array $extraDisplayOptions = array())
  100. {
  101. $this->extraDisplayOptions = $extraDisplayOptions;
  102. $result = parent::dump($data, $output);
  103. $this->dumpId = 'sf-dump-'.mt_rand();
  104. return $result;
  105. }
  106. /**
  107. * Dumps the HTML header.
  108. */
  109. protected function getDumpHeader()
  110. {
  111. $this->headerIsDumped = null !== $this->outputStream ? $this->outputStream : $this->lineDumper;
  112. if (null !== $this->dumpHeader) {
  113. return $this->dumpHeader;
  114. }
  115. $line = str_replace('{$options}', json_encode($this->displayOptions, JSON_FORCE_OBJECT), <<<'EOHTML'
  116. <script>
  117. Sfdump = window.Sfdump || (function (doc) {
  118. var refStyle = doc.createElement('style'),
  119. rxEsc = /([.*+?^${}()|\[\]\/\\])/g,
  120. idRx = /\bsf-dump-\d+-ref[012]\w+\b/,
  121. keyHint = 0 <= navigator.platform.toUpperCase().indexOf('MAC') ? 'Cmd' : 'Ctrl',
  122. addEventListener = function (e, n, cb) {
  123. e.addEventListener(n, cb, false);
  124. };
  125. (doc.documentElement.firstElementChild || doc.documentElement.children[0]).appendChild(refStyle);
  126. if (!doc.addEventListener) {
  127. addEventListener = function (element, eventName, callback) {
  128. element.attachEvent('on' + eventName, function (e) {
  129. e.preventDefault = function () {e.returnValue = false;};
  130. e.target = e.srcElement;
  131. callback(e);
  132. });
  133. };
  134. }
  135. function toggle(a, recursive) {
  136. var s = a.nextSibling || {}, oldClass = s.className, arrow, newClass;
  137. if ('sf-dump-compact' == oldClass) {
  138. arrow = '▼';
  139. newClass = 'sf-dump-expanded';
  140. } else if ('sf-dump-expanded' == oldClass) {
  141. arrow = '▶';
  142. newClass = 'sf-dump-compact';
  143. } else {
  144. return false;
  145. }
  146. a.lastChild.innerHTML = arrow;
  147. s.className = newClass;
  148. if (recursive) {
  149. try {
  150. a = s.querySelectorAll('.'+oldClass);
  151. for (s = 0; s < a.length; ++s) {
  152. if (a[s].className !== newClass) {
  153. a[s].className = newClass;
  154. a[s].previousSibling.lastChild.innerHTML = arrow;
  155. }
  156. }
  157. } catch (e) {
  158. }
  159. }
  160. return true;
  161. };
  162. function collapse(a, recursive) {
  163. var s = a.nextSibling || {}, oldClass = s.className;
  164. if ('sf-dump-expanded' == oldClass) {
  165. toggle(a, recursive);
  166. return true;
  167. }
  168. return false;
  169. };
  170. function expand(a, recursive) {
  171. var s = a.nextSibling || {}, oldClass = s.className;
  172. if ('sf-dump-compact' == oldClass) {
  173. toggle(a, recursive);
  174. return true;
  175. }
  176. return false;
  177. };
  178. function collapseAll(root) {
  179. var a = root.querySelector('a.sf-dump-toggle');
  180. if (a) {
  181. collapse(a, true);
  182. expand(a);
  183. return true;
  184. }
  185. return false;
  186. }
  187. function reveal(node) {
  188. var previous, parents = [];
  189. while ((node = node.parentNode || {}) && (previous = node.previousSibling) && 'A' === previous.tagName) {
  190. parents.push(previous);
  191. }
  192. if (0 !== parents.length) {
  193. parents.forEach(function (parent) {
  194. expand(parent);
  195. });
  196. return true;
  197. }
  198. return false;
  199. }
  200. function highlight(root, activeNode, nodes) {
  201. resetHighlightedNodes(root);
  202. Array.from(nodes||[]).forEach(function (node) {
  203. if (!/\bsf-dump-highlight\b/.test(node.className)) {
  204. node.className = node.className + ' sf-dump-highlight';
  205. }
  206. });
  207. if (!/\bsf-dump-highlight-active\b/.test(activeNode.className)) {
  208. activeNode.className = activeNode.className + ' sf-dump-highlight-active';
  209. }
  210. }
  211. function resetHighlightedNodes(root) {
  212. Array.from(root.querySelectorAll('.sf-dump-str, .sf-dump-key, .sf-dump-public, .sf-dump-protected, .sf-dump-private')).forEach(function (strNode) {
  213. strNode.className = strNode.className.replace(/\b sf-dump-highlight\b/, '');
  214. strNode.className = strNode.className.replace(/\b sf-dump-highlight-active\b/, '');
  215. });
  216. }
  217. return function (root, x) {
  218. root = doc.getElementById(root);
  219. var indentRx = new RegExp('^('+(root.getAttribute('data-indent-pad') || ' ').replace(rxEsc, '\\$1')+')+', 'm'),
  220. options = {$options},
  221. elt = root.getElementsByTagName('A'),
  222. len = elt.length,
  223. i = 0, s, h,
  224. t = [];
  225. while (i < len) t.push(elt[i++]);
  226. for (i in x) {
  227. options[i] = x[i];
  228. }
  229. function a(e, f) {
  230. addEventListener(root, e, function (e) {
  231. if ('A' == e.target.tagName) {
  232. f(e.target, e);
  233. } else if ('A' == e.target.parentNode.tagName) {
  234. f(e.target.parentNode, e);
  235. } else if (e.target.nextElementSibling && 'A' == e.target.nextElementSibling.tagName) {
  236. f(e.target.nextElementSibling, e, true);
  237. }
  238. });
  239. };
  240. function isCtrlKey(e) {
  241. return e.ctrlKey || e.metaKey;
  242. }
  243. function xpathString(str) {
  244. var parts = str.match(/[^'"]+|['"]/g).map(function (part) {
  245. if ("'" == part) {
  246. return '"\'"';
  247. }
  248. if ('"' == part) {
  249. return "'\"'";
  250. }
  251. return "'" + part + "'";
  252. });
  253. return "concat(" + parts.join(",") + ", '')";
  254. }
  255. addEventListener(root, 'mouseover', function (e) {
  256. if ('' != refStyle.innerHTML) {
  257. refStyle.innerHTML = '';
  258. }
  259. });
  260. a('mouseover', function (a, e, c) {
  261. if (c) {
  262. e.target.style.cursor = "pointer";
  263. } else if (a = idRx.exec(a.className)) {
  264. try {
  265. refStyle.innerHTML = 'pre.sf-dump .'+a[0]+'{background-color: #B729D9; color: #FFF !important; border-radius: 2px}';
  266. } catch (e) {
  267. }
  268. }
  269. });
  270. a('click', function (a, e, c) {
  271. if (/\bsf-dump-toggle\b/.test(a.className)) {
  272. e.preventDefault();
  273. if (!toggle(a, isCtrlKey(e))) {
  274. var r = doc.getElementById(a.getAttribute('href').substr(1)),
  275. s = r.previousSibling,
  276. f = r.parentNode,
  277. t = a.parentNode;
  278. t.replaceChild(r, a);
  279. f.replaceChild(a, s);
  280. t.insertBefore(s, r);
  281. f = f.firstChild.nodeValue.match(indentRx);
  282. t = t.firstChild.nodeValue.match(indentRx);
  283. if (f && t && f[0] !== t[0]) {
  284. r.innerHTML = r.innerHTML.replace(new RegExp('^'+f[0].replace(rxEsc, '\\$1'), 'mg'), t[0]);
  285. }
  286. if ('sf-dump-compact' == r.className) {
  287. toggle(s, isCtrlKey(e));
  288. }
  289. }
  290. if (c) {
  291. } else if (doc.getSelection) {
  292. try {
  293. doc.getSelection().removeAllRanges();
  294. } catch (e) {
  295. doc.getSelection().empty();
  296. }
  297. } else {
  298. doc.selection.empty();
  299. }
  300. } else if (/\bsf-dump-str-toggle\b/.test(a.className)) {
  301. e.preventDefault();
  302. e = a.parentNode.parentNode;
  303. e.className = e.className.replace(/sf-dump-str-(expand|collapse)/, a.parentNode.className);
  304. }
  305. });
  306. elt = root.getElementsByTagName('SAMP');
  307. len = elt.length;
  308. i = 0;
  309. while (i < len) t.push(elt[i++]);
  310. len = t.length;
  311. for (i = 0; i < len; ++i) {
  312. elt = t[i];
  313. if ('SAMP' == elt.tagName) {
  314. elt.className = 'sf-dump-expanded';
  315. a = elt.previousSibling || {};
  316. if ('A' != a.tagName) {
  317. a = doc.createElement('A');
  318. a.className = 'sf-dump-ref';
  319. elt.parentNode.insertBefore(a, elt);
  320. } else {
  321. a.innerHTML += ' ';
  322. }
  323. a.title = (a.title ? a.title+'\n[' : '[')+keyHint+'+click] Expand all children';
  324. a.innerHTML += '<span>▼</span>';
  325. a.className += ' sf-dump-toggle';
  326. x = 1;
  327. if ('sf-dump' != elt.parentNode.className) {
  328. x += elt.parentNode.getAttribute('data-depth')/1;
  329. }
  330. elt.setAttribute('data-depth', x);
  331. if (x > options.maxDepth) {
  332. toggle(a);
  333. }
  334. } else if ('sf-dump-ref' == elt.className && (a = elt.getAttribute('href'))) {
  335. a = a.substr(1);
  336. elt.className += ' '+a;
  337. if (/[\[{]$/.test(elt.previousSibling.nodeValue)) {
  338. a = a != elt.nextSibling.id && doc.getElementById(a);
  339. try {
  340. s = a.nextSibling;
  341. elt.appendChild(a);
  342. s.parentNode.insertBefore(a, s);
  343. if (/^[@#]/.test(elt.innerHTML)) {
  344. elt.innerHTML += ' <span>▶</span>';
  345. } else {
  346. elt.innerHTML = '<span>▶</span>';
  347. elt.className = 'sf-dump-ref';
  348. }
  349. elt.className += ' sf-dump-toggle';
  350. } catch (e) {
  351. if ('&' == elt.innerHTML.charAt(0)) {
  352. elt.innerHTML = '…';
  353. elt.className = 'sf-dump-ref';
  354. }
  355. }
  356. }
  357. }
  358. }
  359. if (doc.evaluate && Array.from && root.children.length > 1) {
  360. root.setAttribute('tabindex', 0);
  361. SearchState = function () {
  362. this.nodes = [];
  363. this.idx = 0;
  364. };
  365. SearchState.prototype = {
  366. next: function () {
  367. if (this.isEmpty()) {
  368. return this.current();
  369. }
  370. this.idx = this.idx < (this.nodes.length - 1) ? this.idx + 1 : this.idx;
  371. return this.current();
  372. },
  373. previous: function () {
  374. if (this.isEmpty()) {
  375. return this.current();
  376. }
  377. this.idx = this.idx > 0 ? this.idx - 1 : this.idx;
  378. return this.current();
  379. },
  380. isEmpty: function () {
  381. return 0 === this.count();
  382. },
  383. current: function () {
  384. if (this.isEmpty()) {
  385. return null;
  386. }
  387. return this.nodes[this.idx];
  388. },
  389. reset: function () {
  390. this.nodes = [];
  391. this.idx = 0;
  392. },
  393. count: function () {
  394. return this.nodes.length;
  395. },
  396. };
  397. function showCurrent(state)
  398. {
  399. var currentNode = state.current();
  400. if (currentNode) {
  401. reveal(currentNode);
  402. highlight(root, currentNode, state.nodes);
  403. }
  404. counter.textContent = (state.isEmpty() ? 0 : state.idx + 1) + ' of ' + state.count();
  405. }
  406. var search = doc.createElement('div');
  407. search.className = 'sf-dump-search-wrapper sf-dump-search-hidden';
  408. search.innerHTML = '
  409. <input type="text" class="sf-dump-search-input">
  410. <span class="sf-dump-search-count">0 of 0<\/span>
  411. <button type="button" class="sf-dump-search-input-previous" tabindex="-1">
  412. <svg viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg">
  413. <path d="M1683 1331l-166 165q-19 19-45 19t-45-19l-531-531-531 531q-19 19-45 19t-45-19l-166-165q-19-19-19-45.5t19-45.5l742-741q19-19 45-19t45 19l742 741q19 19 19 45.5t-19 45.5z"\/>
  414. <\/svg>
  415. <\/button>
  416. <button type="button" class="sf-dump-search-input-next" tabindex="-1">
  417. <svg viewBox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg">
  418. <path d="M1683 808l-742 741q-19 19-45 19t-45-19l-742-741q-19-19-19-45.5t19-45.5l166-165q19-19 45-19t45 19l531 531 531-531q19-19 45-19t45 19l166 165q19 19 19 45.5t-19 45.5z"\/>
  419. <\/svg>
  420. <\/button>
  421. ';
  422. root.insertBefore(search, root.firstChild);
  423. var state = new SearchState();
  424. var searchInput = search.querySelector('.sf-dump-search-input');
  425. var counter = search.querySelector('.sf-dump-search-count');
  426. var searchInputTimer = 0;
  427. var previousSearchQuery = '';
  428. addEventListener(searchInput, 'keyup', function (e) {
  429. var searchQuery = e.target.value;
  430. /* Don't perform anything if the pressed key didn't change the query */
  431. if (searchQuery === previousSearchQuery) {
  432. return;
  433. }
  434. previousSearchQuery = searchQuery;
  435. clearTimeout(searchInputTimer);
  436. searchInputTimer = setTimeout(function () {
  437. state.reset();
  438. collapseAll(root);
  439. resetHighlightedNodes(root);
  440. if ('' === searchQuery) {
  441. counter.textContent = '0 of 0';
  442. return;
  443. }
  444. var xpathResult = doc.evaluate('//pre[@id="' + root.id + '"]//span[@class="sf-dump-str" or @class="sf-dump-key" or @class="sf-dump-public" or @class="sf-dump-protected" or @class="sf-dump-private"][contains(child::text(), ' + xpathString(searchQuery) + ')]', document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
  445. while (node = xpathResult.iterateNext()) state.nodes.push(node);
  446. showCurrent(state);
  447. }, 400);
  448. });
  449. Array.from(search.querySelectorAll('.sf-dump-search-input-next, .sf-dump-search-input-previous')).forEach(function (btn) {
  450. addEventListener(btn, 'click', function (e) {
  451. e.preventDefault();
  452. var direction = -1 !== e.target.className.indexOf('next') ? 'next' : 'previous';
  453. 'next' === direction ? state.next() : state.previous();
  454. searchInput.focus();
  455. collapseAll(root);
  456. showCurrent(state);
  457. })
  458. });
  459. addEventListener(root, 'keydown', function (e) {
  460. var isSearchActive = !/\bsf-dump-search-hidden\b/.test(search.className);
  461. if ((114 === e.keyCode && !isSearchActive) || (isCtrlKey(e) && 70 === e.keyCode)) {
  462. /* F3 or CMD/CTRL + F */
  463. e.preventDefault();
  464. search.className = search.className.replace(/\bsf-dump-search-hidden\b/, '');
  465. searchInput.focus();
  466. } else if (isSearchActive) {
  467. if (27 === e.keyCode) {
  468. /* ESC key */
  469. search.className += ' sf-dump-search-hidden';
  470. e.preventDefault();
  471. resetHighlightedNodes(root);
  472. searchInput.value = '';
  473. } else if (
  474. (isCtrlKey(e) && 71 === e.keyCode) /* CMD/CTRL + G */
  475. || 13 === e.keyCode /* Enter */
  476. || 114 === e.keyCode /* F3 */
  477. ) {
  478. e.preventDefault();
  479. e.shiftKey ? state.previous() : state.next();
  480. collapseAll(root);
  481. showCurrent(state);
  482. }
  483. }
  484. });
  485. }
  486. if (0 >= options.maxStringLength) {
  487. return;
  488. }
  489. try {
  490. elt = root.querySelectorAll('.sf-dump-str');
  491. len = elt.length;
  492. i = 0;
  493. t = [];
  494. while (i < len) t.push(elt[i++]);
  495. len = t.length;
  496. for (i = 0; i < len; ++i) {
  497. elt = t[i];
  498. s = elt.innerText || elt.textContent;
  499. x = s.length - options.maxStringLength;
  500. if (0 < x) {
  501. h = elt.innerHTML;
  502. elt[elt.innerText ? 'innerText' : 'textContent'] = s.substring(0, options.maxStringLength);
  503. elt.className += ' sf-dump-str-collapse';
  504. elt.innerHTML = '<span class=sf-dump-str-collapse>'+h+'<a class="sf-dump-ref sf-dump-str-toggle" title="Collapse"> ◀</a></span>'+
  505. '<span class=sf-dump-str-expand>'+elt.innerHTML+'<a class="sf-dump-ref sf-dump-str-toggle" title="'+x+' remaining characters"> ▶</a></span>';
  506. }
  507. }
  508. } catch (e) {
  509. }
  510. };
  511. })(document);
  512. </script><style>
  513. pre.sf-dump {
  514. display: block;
  515. white-space: pre;
  516. padding: 5px;
  517. }
  518. pre.sf-dump:after {
  519. content: "";
  520. visibility: hidden;
  521. display: block;
  522. height: 0;
  523. clear: both;
  524. }
  525. pre.sf-dump span {
  526. display: inline;
  527. }
  528. pre.sf-dump .sf-dump-compact {
  529. display: none;
  530. }
  531. pre.sf-dump abbr {
  532. text-decoration: none;
  533. border: none;
  534. cursor: help;
  535. }
  536. pre.sf-dump a {
  537. text-decoration: none;
  538. cursor: pointer;
  539. border: 0;
  540. outline: none;
  541. color: inherit;
  542. }
  543. pre.sf-dump .sf-dump-ellipsis {
  544. display: inline-block;
  545. overflow: visible;
  546. text-overflow: ellipsis;
  547. max-width: 5em;
  548. white-space: nowrap;
  549. overflow: hidden;
  550. vertical-align: top;
  551. }
  552. pre.sf-dump .sf-dump-ellipsis+.sf-dump-ellipsis {
  553. max-width: none;
  554. }
  555. pre.sf-dump code {
  556. display:inline;
  557. padding:0;
  558. background:none;
  559. }
  560. .sf-dump-str-collapse .sf-dump-str-collapse {
  561. display: none;
  562. }
  563. .sf-dump-str-expand .sf-dump-str-expand {
  564. display: none;
  565. }
  566. .sf-dump-public.sf-dump-highlight,
  567. .sf-dump-protected.sf-dump-highlight,
  568. .sf-dump-private.sf-dump-highlight,
  569. .sf-dump-str.sf-dump-highlight,
  570. .sf-dump-key.sf-dump-highlight {
  571. background: rgba(111, 172, 204, 0.3);
  572. border: 1px solid #7DA0B1;
  573. border-radius: 3px;
  574. }
  575. .sf-dump-public.sf-dump-highlight-active,
  576. .sf-dump-protected.sf-dump-highlight-active,
  577. .sf-dump-private.sf-dump-highlight-active,
  578. .sf-dump-str.sf-dump-highlight-active,
  579. .sf-dump-key.sf-dump-highlight-active {
  580. background: rgba(253, 175, 0, 0.4);
  581. border: 1px solid #ffa500;
  582. border-radius: 3px;
  583. }
  584. pre.sf-dump .sf-dump-search-hidden {
  585. display: none;
  586. }
  587. pre.sf-dump .sf-dump-search-wrapper {
  588. float: right;
  589. font-size: 0;
  590. white-space: nowrap;
  591. max-width: 100%;
  592. text-align: right;
  593. }
  594. pre.sf-dump .sf-dump-search-wrapper > * {
  595. vertical-align: top;
  596. box-sizing: border-box;
  597. height: 21px;
  598. font-weight: normal;
  599. border-radius: 0;
  600. background: #FFF;
  601. color: #757575;
  602. border: 1px solid #BBB;
  603. }
  604. pre.sf-dump .sf-dump-search-wrapper > input.sf-dump-search-input {
  605. padding: 3px;
  606. height: 21px;
  607. font-size: 12px;
  608. border-right: none;
  609. width: 140px;
  610. border-top-left-radius: 3px;
  611. border-bottom-left-radius: 3px;
  612. color: #000;
  613. }
  614. pre.sf-dump .sf-dump-search-wrapper > .sf-dump-search-input-next,
  615. pre.sf-dump .sf-dump-search-wrapper > .sf-dump-search-input-previous {
  616. background: #F2F2F2;
  617. outline: none;
  618. border-left: none;
  619. font-size: 0;
  620. line-height: 0;
  621. }
  622. pre.sf-dump .sf-dump-search-wrapper > .sf-dump-search-input-next {
  623. border-top-right-radius: 3px;
  624. border-bottom-right-radius: 3px;
  625. }
  626. pre.sf-dump .sf-dump-search-wrapper > .sf-dump-search-input-next > svg,
  627. pre.sf-dump .sf-dump-search-wrapper > .sf-dump-search-input-previous > svg {
  628. pointer-events: none;
  629. width: 12px;
  630. height: 12px;
  631. }
  632. pre.sf-dump .sf-dump-search-wrapper > .sf-dump-search-count {
  633. display: inline-block;
  634. padding: 0 5px;
  635. margin: 0;
  636. border-left: none;
  637. line-height: 21px;
  638. font-size: 12px;
  639. }
  640. EOHTML
  641. );
  642. foreach ($this->styles as $class => $style) {
  643. $line .= 'pre.sf-dump'.('default' === $class ? ', pre.sf-dump' : '').' .sf-dump-'.$class.'{'.$style.'}';
  644. }
  645. return $this->dumpHeader = preg_replace('/\s+/', ' ', $line).'</style>'.$this->dumpHeader;
  646. }
  647. /**
  648. * {@inheritdoc}
  649. */
  650. public function enterHash(Cursor $cursor, $type, $class, $hasChild)
  651. {
  652. parent::enterHash($cursor, $type, $class, false);
  653. if ($hasChild) {
  654. if ($cursor->refIndex) {
  655. $r = Cursor::HASH_OBJECT !== $type ? 1 - (Cursor::HASH_RESOURCE !== $type) : 2;
  656. $r .= $r && 0 < $cursor->softRefHandle ? $cursor->softRefHandle : $cursor->refIndex;
  657. $this->line .= sprintf('<samp id=%s-ref%s>', $this->dumpId, $r);
  658. } else {
  659. $this->line .= '<samp>';
  660. }
  661. $this->dumpLine($cursor->depth);
  662. }
  663. }
  664. /**
  665. * {@inheritdoc}
  666. */
  667. public function leaveHash(Cursor $cursor, $type, $class, $hasChild, $cut)
  668. {
  669. $this->dumpEllipsis($cursor, $hasChild, $cut);
  670. if ($hasChild) {
  671. $this->line .= '</samp>';
  672. }
  673. parent::leaveHash($cursor, $type, $class, $hasChild, 0);
  674. }
  675. /**
  676. * {@inheritdoc}
  677. */
  678. protected function style($style, $value, $attr = array())
  679. {
  680. if ('' === $value) {
  681. return '';
  682. }
  683. $v = esc($value);
  684. if ('ref' === $style) {
  685. if (empty($attr['count'])) {
  686. return sprintf('<a class=sf-dump-ref>%s</a>', $v);
  687. }
  688. $r = ('#' !== $v[0] ? 1 - ('@' !== $v[0]) : 2).substr($value, 1);
  689. return sprintf('<a class=sf-dump-ref href=#%s-ref%s title="%d occurrences">%s</a>', $this->dumpId, $r, 1 + $attr['count'], $v);
  690. }
  691. if ('const' === $style && isset($attr['value'])) {
  692. $style .= sprintf(' title="%s"', esc(is_scalar($attr['value']) ? $attr['value'] : json_encode($attr['value'])));
  693. } elseif ('public' === $style) {
  694. $style .= sprintf(' title="%s"', empty($attr['dynamic']) ? 'Public property' : 'Runtime added dynamic property');
  695. } elseif ('str' === $style && 1 < $attr['length']) {
  696. $style .= sprintf(' title="%d%s characters"', $attr['length'], $attr['binary'] ? ' binary or non-UTF-8' : '');
  697. } elseif ('note' === $style && false !== $c = strrpos($v, '\\')) {
  698. return sprintf('<abbr title="%s" class=sf-dump-%s>%s</abbr>', $v, $style, substr($v, $c + 1));
  699. } elseif ('protected' === $style) {
  700. $style .= ' title="Protected property"';
  701. } elseif ('meta' === $style && isset($attr['title'])) {
  702. $style .= sprintf(' title="%s"', esc($this->utf8Encode($attr['title'])));
  703. } elseif ('private' === $style) {
  704. $style .= sprintf(' title="Private property defined in class:&#10;`%s`"', esc($this->utf8Encode($attr['class'])));
  705. }
  706. $map = static::$controlCharsMap;
  707. if (isset($attr['ellipsis'])) {
  708. $class = 'sf-dump-ellipsis';
  709. if (isset($attr['ellipsis-type'])) {
  710. $class = sprintf('"%s sf-dump-ellipsis-%s"', $class, $attr['ellipsis-type']);
  711. }
  712. $label = esc(substr($value, -$attr['ellipsis']));
  713. $style = str_replace(' title="', " title=\"$v\n", $style);
  714. $v = sprintf('<span class=%s>%s</span>', $class, substr($v, 0, -strlen($label)));
  715. if (!empty($attr['ellipsis-tail'])) {
  716. $tail = strlen(esc(substr($value, -$attr['ellipsis'], $attr['ellipsis-tail'])));
  717. $v .= sprintf('<span class=sf-dump-ellipsis>%s</span>%s', substr($label, 0, $tail), substr($label, $tail));
  718. } else {
  719. $v .= $label;
  720. }
  721. }
  722. $v = "<span class=sf-dump-{$style}>".preg_replace_callback(static::$controlCharsRx, function ($c) use ($map) {
  723. $s = '<span class=sf-dump-default>';
  724. $c = $c[$i = 0];
  725. do {
  726. $s .= isset($map[$c[$i]]) ? $map[$c[$i]] : sprintf('\x%02X', ord($c[$i]));
  727. } while (isset($c[++$i]));
  728. return $s.'</span>';
  729. }, $v).'</span>';
  730. if (isset($attr['file']) && $href = $this->getSourceLink($attr['file'], isset($attr['line']) ? $attr['line'] : 0)) {
  731. $attr['href'] = $href;
  732. }
  733. if (isset($attr['href'])) {
  734. $v = sprintf('<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>', esc($this->utf8Encode($attr['href'])), $v);
  735. }
  736. if (isset($attr['lang'])) {
  737. $v = sprintf('<code class="%s">%s</code>', esc($attr['lang']), $v);
  738. }
  739. return $v;
  740. }
  741. /**
  742. * {@inheritdoc}
  743. */
  744. protected function dumpLine($depth, $endOfValue = false)
  745. {
  746. if (-1 === $this->lastDepth) {
  747. $this->line = sprintf($this->dumpPrefix, $this->dumpId, $this->indentPad).$this->line;
  748. }
  749. if ($this->headerIsDumped !== (null !== $this->outputStream ? $this->outputStream : $this->lineDumper)) {
  750. $this->line = $this->getDumpHeader().$this->line;
  751. }
  752. if (-1 === $depth) {
  753. $args = array('"'.$this->dumpId.'"');
  754. if ($this->extraDisplayOptions) {
  755. $args[] = json_encode($this->extraDisplayOptions, JSON_FORCE_OBJECT);
  756. }
  757. // Replace is for BC
  758. $this->line .= sprintf(str_replace('"%s"', '%s', $this->dumpSuffix), implode(', ', $args));
  759. }
  760. $this->lastDepth = $depth;
  761. $this->line = mb_convert_encoding($this->line, 'HTML-ENTITIES', 'UTF-8');
  762. if (-1 === $depth) {
  763. AbstractDumper::dumpLine(0);
  764. }
  765. AbstractDumper::dumpLine($depth);
  766. }
  767. private function getSourceLink($file, $line)
  768. {
  769. $options = $this->extraDisplayOptions + $this->displayOptions;
  770. if ($fmt = $options['fileLinkFormat']) {
  771. return is_string($fmt) ? strtr($fmt, array('%f' => $file, '%l' => $line)) : $fmt->format($file, $line);
  772. }
  773. return false;
  774. }
  775. }
  776. function esc($str)
  777. {
  778. return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
  779. }