comparison vendor/symfony/var-dumper/Dumper/HtmlDumper.php @ 0:4c8ae668cc8c

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