HtmlDumper.php 33 KB

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