24
24
*/
25
25
class CliDumper extends AbstractDumper
26
26
{
27
+ const DUMP_DUMP_LOCATION = 16 ;
28
+
27
29
public static $ defaultColors ;
28
30
public static $ defaultOutput = 'php://stdout ' ;
29
31
@@ -88,6 +90,12 @@ public function __construct($output = null, string $charset = null, int $flags =
88
90
]);
89
91
}
90
92
93
+ $ backtrace = debug_backtrace ();
94
+ $ caller = array_shift ($ backtrace );
95
+ $ this ->attr ['file ' ] = $ caller ['file ' ];
96
+ $ this ->attr ['line ' ] = $ caller ['line ' ];
97
+ $ this ->handlesHrefGracefully = 'JetBrains-JediTerm ' !== getenv ('TERMINAL_EMULATOR ' );
98
+
91
99
$ this ->displayOptions ['fileLinkFormat ' ] = ini_get ('xdebug.file_link_format ' ) ?: get_cfg_var ('xdebug.file_link_format ' ) ?: 'file://%f ' ;
92
100
}
93
101
@@ -131,23 +139,12 @@ public function setDisplayOptions(array $displayOptions)
131
139
$ this ->displayOptions = $ displayOptions + $ this ->displayOptions ;
132
140
}
133
141
134
- /**
135
- * {@inheritdoc}
136
- */
137
- public function dump (Data $ data , $ output = null , array $ extraDisplayOptions = [])
138
- {
139
- $ this ->extraDisplayOptions = $ extraDisplayOptions ;
140
- $ result = parent ::dump ($ data , $ output );
141
-
142
- return $ result ;
143
- }
144
-
145
142
/**
146
143
* Configures file of the dump call function.
147
144
*
148
145
* @param string $file File path issued from debug_backtrace
149
146
*/
150
- public function setFile (string $ file )
147
+ public function setFile (? string $ file )
151
148
{
152
149
$ this ->attr ['file ' ] = $ file ;
153
150
}
@@ -157,7 +154,7 @@ public function setFile(string $file)
157
154
*
158
155
* @param int $line File line issued from debug_backtrace
159
156
*/
160
- public function setLine (int $ line )
157
+ public function setLine (? int $ line )
161
158
{
162
159
$ this ->attr ['line ' ] = $ line ;
163
160
}
@@ -334,7 +331,7 @@ public function enterHash(Cursor $cursor, $type, $class, $hasChild)
334
331
$ prefix = substr ($ prefix , 0 , -1 );
335
332
}
336
333
337
- $ this ->line .= $ prefix ;
334
+ $ this ->line .= $ prefix . $ this -> dumpLocation ( $ cursor , $ attr ) ;
338
335
339
336
if ($ hasChild ) {
340
337
$ this ->dumpLine ($ cursor ->depth );
@@ -470,10 +467,6 @@ protected function style($style, $value, $attr = [])
470
467
$ this ->colors = $ this ->supportsColors ();
471
468
}
472
469
473
- if (null === $ this ->handlesHrefGracefully ) {
474
- $ this ->handlesHrefGracefully = 'JetBrains-JediTerm ' !== getenv ('TERMINAL_EMULATOR ' );
475
- }
476
-
477
470
if (isset ($ attr ['ellipsis ' ], $ attr ['ellipsis-type ' ])) {
478
471
$ prefix = substr ($ value , 0 , -$ attr ['ellipsis ' ]);
479
472
if ('cli ' === \PHP_SAPI && 'path ' === $ attr ['ellipsis-type ' ] && isset ($ _SERVER [$ pwd = '\\' === \DIRECTORY_SEPARATOR ? 'CD ' : 'PWD ' ]) && 0 === strpos ($ prefix , $ _SERVER [$ pwd ])) {
@@ -487,47 +480,31 @@ protected function style($style, $value, $attr = [])
487
480
}
488
481
489
482
$ value = $ this ->style ('default ' , $ prefix ).$ this ->style ($ style , $ value );
490
-
491
- goto href;
492
- }
493
-
494
- $ map = static ::$ controlCharsMap ;
495
- $ startCchr = $ this ->colors ? "\033[m \033[ {$ this ->styles ['default ' ]}m " : '' ;
496
- $ endCchr = $ this ->colors ? "\033[m \033[ {$ this ->styles [$ style ]}m " : '' ;
497
- $ value = preg_replace_callback (static ::$ controlCharsRx , function ($ c ) use ($ map , $ startCchr , $ endCchr ) {
498
- $ s = $ startCchr ;
499
- $ c = $ c [$ i = 0 ];
500
- do {
501
- $ s .= isset ($ map [$ c [$ i ]]) ? $ map [$ c [$ i ]] : sprintf ('\x%02X ' , \ord ($ c [$ i ]));
502
- } while (isset ($ c [++$ i ]));
503
-
504
- return $ s .$ endCchr ;
505
- }, $ value , -1 , $ cchrCount );
506
-
507
- if ($ this ->colors ) {
508
- if ($ cchrCount && "\033" === $ value [0 ]) {
509
- $ value = substr ($ value , \strlen ($ startCchr ));
510
- } else {
511
- $ value = "\033[ {$ this ->styles [$ style ]}m " .$ value ;
512
- }
513
- if ($ cchrCount && $ endCchr === substr ($ value , -\strlen ($ endCchr ))) {
514
- $ value = substr ($ value , 0 , -\strlen ($ endCchr ));
515
- } else {
516
- $ value .= "\033[ {$ this ->styles ['default ' ]}m " ;
517
- }
518
- }
519
-
520
- href:
521
- if ($ this ->colors && $ this ->handlesHrefGracefully ) {
522
- if (isset ($ attr ['file ' ]) && $ href = $ this ->getSourceLink ($ attr ['file ' ], isset ($ attr ['line ' ]) ? $ attr ['line ' ] : 0 )) {
523
- if ('note ' === $ style ) {
524
- $ value .= "\033]8;; {$ href }\033\\^ \033]8;; \033\\" ;
483
+ } else {
484
+ $ map = static ::$ controlCharsMap ;
485
+ $ startCchr = $ this ->colors ? "\033[m \033[ {$ this ->styles ['default ' ]}m " : '' ;
486
+ $ endCchr = $ this ->colors ? "\033[m \033[ {$ this ->styles [$ style ]}m " : '' ;
487
+ $ value = preg_replace_callback (static ::$ controlCharsRx , function ($ c ) use ($ map , $ startCchr , $ endCchr ) {
488
+ $ s = $ startCchr ;
489
+ $ c = $ c [$ i = 0 ];
490
+ do {
491
+ $ s .= isset ($ map [$ c [$ i ]]) ? $ map [$ c [$ i ]] : sprintf ('\x%02X ' , \ord ($ c [$ i ]));
492
+ } while (isset ($ c [++$ i ]));
493
+
494
+ return $ s .$ endCchr ;
495
+ }, $ value , -1 , $ cchrCount );
496
+
497
+ if ($ this ->colors ) {
498
+ if ($ cchrCount && "\033" === $ value [0 ]) {
499
+ $ value = substr ($ value , \strlen ($ startCchr ));
525
500
} else {
526
- $ attr ['href ' ] = $ href ;
501
+ $ value = "\033[ {$ this ->styles [$ style ]}m " .$ value ;
502
+ }
503
+ if ($ cchrCount && $ endCchr === substr ($ value , -\strlen ($ endCchr ))) {
504
+ $ value = substr ($ value , 0 , -\strlen ($ endCchr ));
505
+ } else {
506
+ $ value .= "\033[ {$ this ->styles ['default ' ]}m " ;
527
507
}
528
- }
529
- if (isset ($ attr ['href ' ])) {
530
- $ value .= " \033]8;; {$ attr ['href ' ]}\033\\[^] \033]8;; \033\\" ;
531
508
}
532
509
}
533
510
@@ -668,10 +645,29 @@ private function isWindowsTrueColor()
668
645
return $ result ;
669
646
}
670
647
671
- private function getSourceLink ($ file , $ line )
648
+ /**
649
+ * Return the substring with the dump() function file location to get displayed with a small caret after
650
+ * an hash.
651
+ *
652
+ * @param Cursor $cursor
653
+ * @param array $attr
654
+ * @return string
655
+ */
656
+ private function dumpLocation (Cursor $ cursor , $ attr = []) :string
657
+ {
658
+ if ($ this ->handlesHrefGracefully && $ cursor ->depth === 0 && isset ($ attr ['file ' ]) && $ href = $ this ->getSourceLink ($ attr ['file ' ], $ attr ['line ' ])) {
659
+ $ color = $ this ->colors ? "\033" : '' ;
660
+
661
+ return " {$ color }]8;; {$ href }{$ color }\\[^] {$ color }]8;; {$ color }\\" ;
662
+ }
663
+
664
+ return '' ;
665
+ }
666
+
667
+ private function getSourceLink ($ file , $ lineNumber )
672
668
{
673
669
if ($ fmt = $ this ->displayOptions ['fileLinkFormat ' ]) {
674
- return \is_string ($ fmt ) ? strtr ($ fmt , ['%f ' => $ file , '%l ' => $ line ]) : ($ fmt ->format ($ file , $ line ) ?: 'file:// ' .$ file );
670
+ return \is_string ($ fmt ) ? strtr ($ fmt , ['%f ' => $ file , '%l ' => $ lineNumber ]) : ($ fmt ->format ($ file , $ lineNumber ) ?: 'file:// ' .$ file );
675
671
}
676
672
677
673
return false ;
0 commit comments