HtmlDumper.php 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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 .sf-dump-ellipsization {
  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. text-overflow: ellipsis;
  601. white-space: nowrap;
  602. overflow: hidden;
  603. }
  604. pre.sf-dump .sf-dump-ellipsis-tail {
  605. flex-shrink: 0;
  606. }
  607. pre.sf-dump code {
  608. display:inline;
  609. padding:0;
  610. background:none;
  611. }
  612. .sf-dump-public.sf-dump-highlight,
  613. .sf-dump-protected.sf-dump-highlight,
  614. .sf-dump-private.sf-dump-highlight,
  615. .sf-dump-str.sf-dump-highlight,
  616. .sf-dump-key.sf-dump-highlight {
  617. background: rgba(111, 172, 204, 0.3);
  618. border: 1px solid #7DA0B1;
  619. border-radius: 3px;
  620. }
  621. .sf-dump-public.sf-dump-highlight-active,
  622. .sf-dump-protected.sf-dump-highlight-active,
  623. .sf-dump-private.sf-dump-highlight-active,
  624. .sf-dump-str.sf-dump-highlight-active,
  625. .sf-dump-key.sf-dump-highlight-active {
  626. background: rgba(253, 175, 0, 0.4);
  627. border: 1px solid #ffa500;
  628. border-radius: 3px;
  629. }
  630. pre.sf-dump .sf-dump-search-hidden {
  631. display: none !important;
  632. }
  633. pre.sf-dump .sf-dump-search-wrapper {
  634. font-size: 0;
  635. white-space: nowrap;
  636. margin-bottom: 5px;
  637. display: flex;
  638. position: -webkit-sticky;
  639. position: sticky;
  640. top: 5px;
  641. }
  642. pre.sf-dump .sf-dump-search-wrapper > * {
  643. vertical-align: top;
  644. box-sizing: border-box;
  645. height: 21px;
  646. font-weight: normal;
  647. border-radius: 0;
  648. background: #FFF;
  649. color: #757575;
  650. border: 1px solid #BBB;
  651. }
  652. pre.sf-dump .sf-dump-search-wrapper > input.sf-dump-search-input {
  653. padding: 3px;
  654. height: 21px;
  655. font-size: 12px;
  656. border-right: none;
  657. border-top-left-radius: 3px;
  658. border-bottom-left-radius: 3px;
  659. color: #000;
  660. min-width: 15px;
  661. width: 100%;
  662. }
  663. pre.sf-dump .sf-dump-search-wrapper > .sf-dump-search-input-next,
  664. pre.sf-dump .sf-dump-search-wrapper > .sf-dump-search-input-previous {
  665. background: #F2F2F2;
  666. outline: none;
  667. border-left: none;
  668. font-size: 0;
  669. line-height: 0;
  670. }
  671. pre.sf-dump .sf-dump-search-wrapper > .sf-dump-search-input-next {
  672. border-top-right-radius: 3px;
  673. border-bottom-right-radius: 3px;
  674. }
  675. pre.sf-dump .sf-dump-search-wrapper > .sf-dump-search-input-next > svg,
  676. pre.sf-dump .sf-dump-search-wrapper > .sf-dump-search-input-previous > svg {
  677. pointer-events: none;
  678. width: 12px;
  679. height: 12px;
  680. }
  681. pre.sf-dump .sf-dump-search-wrapper > .sf-dump-search-count {
  682. display: inline-block;
  683. padding: 0 5px;
  684. margin: 0;
  685. border-left: none;
  686. line-height: 21px;
  687. font-size: 12px;
  688. }
  689. EOHTML
  690. );
  691. foreach ($this->styles as $class => $style) {
  692. $line .= 'pre.sf-dump'.('default' === $class ? ', pre.sf-dump' : '').' .sf-dump-'.$class.'{'.$style.'}';
  693. }
  694. $line .= 'pre.sf-dump .sf-dump-ellipsis-note{'.$this->styles['note'].'}';
  695. return $this->dumpHeader = preg_replace('/\s+/', ' ', $line).'</style>'.$this->dumpHeader;
  696. }
  697. /**
  698. * @return void
  699. */
  700. public function dumpString(Cursor $cursor, string $str, bool $bin, int $cut)
  701. {
  702. if ('' === $str && isset($cursor->attr['img-data'], $cursor->attr['content-type'])) {
  703. $this->dumpKey($cursor);
  704. $this->line .= $this->style('default', $cursor->attr['img-size'] ?? '', []);
  705. $this->line .= $cursor->depth >= $this->displayOptions['maxDepth'] ? ' <samp class=sf-dump-compact>' : ' <samp class=sf-dump-expanded>';
  706. $this->endValue($cursor);
  707. $this->line .= $this->indentPad;
  708. $this->line .= sprintf('<img src="data:%s;base64,%s" /></samp>', $cursor->attr['content-type'], base64_encode($cursor->attr['img-data']));
  709. $this->endValue($cursor);
  710. } else {
  711. parent::dumpString($cursor, $str, $bin, $cut);
  712. }
  713. }
  714. /**
  715. * @return void
  716. */
  717. public function enterHash(Cursor $cursor, int $type, string|int|null $class, bool $hasChild)
  718. {
  719. if (Cursor::HASH_OBJECT === $type) {
  720. $cursor->attr['depth'] = $cursor->depth;
  721. }
  722. parent::enterHash($cursor, $type, $class, false);
  723. if ($cursor->skipChildren || $cursor->depth >= $this->displayOptions['maxDepth']) {
  724. $cursor->skipChildren = false;
  725. $eol = ' class=sf-dump-compact>';
  726. } else {
  727. $this->expandNextHash = false;
  728. $eol = ' class=sf-dump-expanded>';
  729. }
  730. if ($hasChild) {
  731. $this->line .= '<samp data-depth='.($cursor->depth + 1);
  732. if ($cursor->refIndex) {
  733. $r = Cursor::HASH_OBJECT !== $type ? 1 - (Cursor::HASH_RESOURCE !== $type) : 2;
  734. $r .= $r && 0 < $cursor->softRefHandle ? $cursor->softRefHandle : $cursor->refIndex;
  735. $this->line .= sprintf(' id=%s-ref%s', $this->dumpId, $r);
  736. }
  737. $this->line .= $eol;
  738. $this->dumpLine($cursor->depth);
  739. }
  740. }
  741. /**
  742. * @return void
  743. */
  744. public function leaveHash(Cursor $cursor, int $type, string|int|null $class, bool $hasChild, int $cut)
  745. {
  746. $this->dumpEllipsis($cursor, $hasChild, $cut);
  747. if ($hasChild) {
  748. $this->line .= '</samp>';
  749. }
  750. parent::leaveHash($cursor, $type, $class, $hasChild, 0);
  751. }
  752. protected function style(string $style, string $value, array $attr = []): string
  753. {
  754. if ('' === $value && ('label' !== $style || !isset($attr['file']) && !isset($attr['href']))) {
  755. return '';
  756. }
  757. $v = esc($value);
  758. if ('ref' === $style) {
  759. if (empty($attr['count'])) {
  760. return sprintf('<a class=sf-dump-ref>%s</a>', $v);
  761. }
  762. $r = ('#' !== $v[0] ? 1 - ('@' !== $v[0]) : 2).substr($value, 1);
  763. return sprintf('<a class=sf-dump-ref href=#%s-ref%s title="%d occurrences">%s</a>', $this->dumpId, $r, 1 + $attr['count'], $v);
  764. }
  765. $dumpClasses = ['sf-dump-'.$style];
  766. $dumpTitle = '';
  767. if ('const' === $style && isset($attr['value'])) {
  768. $dumpTitle = esc(\is_scalar($attr['value']) ? $attr['value'] : json_encode($attr['value']));
  769. } elseif ('public' === $style) {
  770. $dumpTitle = empty($attr['dynamic']) ? 'Public property' : 'Runtime added dynamic property';
  771. } elseif ('str' === $style && 1 < $attr['length']) {
  772. $dumpTitle = sprintf('%d%s characters', $attr['length'], $attr['binary'] ? ' binary or non-UTF-8' : '');
  773. } elseif ('note' === $style && 0 < ($attr['depth'] ?? 0) && false !== $c = strrpos($value, '\\')) {
  774. $attr += [
  775. 'ellipsis' => \strlen($value) - $c,
  776. 'ellipsis-type' => 'note',
  777. 'ellipsis-tail' => 1,
  778. ];
  779. } elseif ('protected' === $style) {
  780. $dumpTitle = 'Protected property';
  781. } elseif ('meta' === $style && isset($attr['title'])) {
  782. $dumpTitle = esc($this->utf8Encode($attr['title']));
  783. } elseif ('private' === $style) {
  784. $dumpTitle = sprintf('Private property defined in class:&#10;`%s`', esc($this->utf8Encode($attr['class'])));
  785. }
  786. if (isset($attr['ellipsis'])) {
  787. $dumpClasses[] = 'sf-dump-ellipsization';
  788. $ellipsisClass = 'sf-dump-ellipsis';
  789. if (isset($attr['ellipsis-type'])) {
  790. $ellipsisClass .= ' sf-dump-ellipsis-'.$attr['ellipsis-type'];
  791. }
  792. $label = esc(substr($value, -$attr['ellipsis']));
  793. $dumpTitle = $v."\n".$dumpTitle;
  794. $v = sprintf('<span class="%s">%s</span>', $ellipsisClass, substr($v, 0, -\strlen($label)));
  795. if (!empty($attr['ellipsis-tail'])) {
  796. $tail = \strlen(esc(substr($value, -$attr['ellipsis'], $attr['ellipsis-tail'])));
  797. $v .= sprintf('<span class="%s">%s</span><span class="sf-dump-ellipsis-tail">%s</span>', $ellipsisClass, substr($label, 0, $tail), substr($label, $tail));
  798. } else {
  799. $v .= sprintf('<span class="sf-dump-ellipsis-tail">%s</span>', $label);
  800. }
  801. }
  802. $map = static::$controlCharsMap;
  803. $v = sprintf(
  804. '<span class=%s%s%1$s%s>%s</span>',
  805. 1 === count($dumpClasses) ? '' : '"',
  806. implode(' ', $dumpClasses),
  807. $dumpTitle ? ' title="'.$dumpTitle.'"' : '',
  808. preg_replace_callback(static::$controlCharsRx, function ($c) use ($map) {
  809. $s = $b = '<span class="sf-dump-default';
  810. $c = $c[$i = 0];
  811. if ($ns = "\r" === $c[$i] || "\n" === $c[$i]) {
  812. $s .= ' sf-dump-ns';
  813. }
  814. $s .= '">';
  815. do {
  816. if (("\r" === $c[$i] || "\n" === $c[$i]) !== $ns) {
  817. $s .= '</span>'.$b;
  818. if ($ns = !$ns) {
  819. $s .= ' sf-dump-ns';
  820. }
  821. $s .= '">';
  822. }
  823. $s .= $map[$c[$i]] ?? sprintf('\x%02X', \ord($c[$i]));
  824. } while (isset($c[++$i]));
  825. return $s.'</span>';
  826. }, $v)
  827. );
  828. if (!($attr['binary'] ?? false)) {
  829. $v = preg_replace_callback(static::$unicodeCharsRx, function ($c) {
  830. return '<span class=sf-dump-default>\u{'.strtoupper(dechex(mb_ord($c[0]))).'}</span>';
  831. }, $v);
  832. }
  833. if (isset($attr['file']) && $href = $this->getSourceLink($attr['file'], $attr['line'] ?? 0)) {
  834. $attr['href'] = $href;
  835. }
  836. if (isset($attr['href'])) {
  837. if ('label' === $style) {
  838. $v .= '^';
  839. }
  840. $target = isset($attr['file']) ? '' : ' target="_blank"';
  841. $v = sprintf('<a href="%s"%s rel="noopener noreferrer">%s</a>', esc($this->utf8Encode($attr['href'])), $target, $v);
  842. }
  843. if (isset($attr['lang'])) {
  844. $v = sprintf('<code class="%s">%s</code>', esc($attr['lang']), $v);
  845. }
  846. if ('label' === $style) {
  847. $v .= ' ';
  848. }
  849. return $v;
  850. }
  851. /**
  852. * @return void
  853. */
  854. protected function dumpLine(int $depth, bool $endOfValue = false)
  855. {
  856. if (-1 === $this->lastDepth) {
  857. $this->line = sprintf($this->dumpPrefix, $this->dumpId, $this->indentPad).$this->line;
  858. }
  859. if ($this->headerIsDumped !== ($this->outputStream ?? $this->lineDumper)) {
  860. $this->line = $this->getDumpHeader().$this->line;
  861. }
  862. if (-1 === $depth) {
  863. $args = ['"'.$this->dumpId.'"'];
  864. if ($this->extraDisplayOptions) {
  865. $args[] = json_encode($this->extraDisplayOptions, \JSON_FORCE_OBJECT);
  866. }
  867. // Replace is for BC
  868. $this->line .= sprintf(str_replace('"%s"', '%s', $this->dumpSuffix), implode(', ', $args));
  869. }
  870. $this->lastDepth = $depth;
  871. $this->line = mb_encode_numericentity($this->line, [0x80, 0x10FFFF, 0, 0x1FFFFF], 'UTF-8');
  872. if (-1 === $depth) {
  873. AbstractDumper::dumpLine(0);
  874. }
  875. AbstractDumper::dumpLine($depth);
  876. }
  877. private function getSourceLink(string $file, int $line): string|false
  878. {
  879. $options = $this->extraDisplayOptions + $this->displayOptions;
  880. if ($fmt = $options['fileLinkFormat']) {
  881. return \is_string($fmt) ? strtr($fmt, ['%f' => $file, '%l' => $line]) : $fmt->format($file, $line);
  882. }
  883. return false;
  884. }
  885. }
  886. function esc(string $str): string
  887. {
  888. return htmlspecialchars($str, \ENT_QUOTES, 'UTF-8');
  889. }