HtmlDumper.php 32 KB

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