HtmlDumper.php 33 KB

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