HtmlDumper.php 34 KB

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