HtmlDumper.php 34 KB

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