HtmlDumper.php 29 KB

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