You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

991 lines
34 KiB

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