菜谱项目

HtmlDumper.php 29KB

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