23
23
use Symfony \Component \DependencyInjection \ParameterBag \ParameterBag ;
24
24
use Symfony \Component \DependencyInjection \Reference ;
25
25
use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
26
+ use Symfony \Component \HttpKernel \Debug \FileLinkFormatter ;
26
27
use Symfony \Component \Routing \Route ;
27
28
use Symfony \Component \Routing \RouteCollection ;
28
29
33
34
*/
34
35
class TextDescriptor extends Descriptor
35
36
{
37
+ private $ fileLinkFormatter ;
38
+
39
+ public function __construct (FileLinkFormatter $ fileLinkFormatter = null )
40
+ {
41
+ $ this ->fileLinkFormatter = $ fileLinkFormatter ;
42
+ }
43
+
36
44
/**
37
45
* {@inheritdoc}
38
46
*/
@@ -47,16 +55,19 @@ protected function describeRouteCollection(RouteCollection $routes, array $optio
47
55
48
56
$ tableRows = [];
49
57
foreach ($ routes ->all () as $ name => $ route ) {
58
+ $ controller = $ route ->getDefault ('_controller ' );
59
+ list ($ controllerClass , $ controllerMethod ) = explode (':: ' , $ controller );
60
+
50
61
$ row = [
51
62
$ name ,
52
63
$ route ->getMethods () ? implode ('| ' , $ route ->getMethods ()) : 'ANY ' ,
53
64
$ route ->getSchemes () ? implode ('| ' , $ route ->getSchemes ()) : 'ANY ' ,
54
65
'' !== $ route ->getHost () ? $ route ->getHost () : 'ANY ' ,
55
- $ route ->getPath (),
66
+ $ this -> transformControllerToLink ( $ controllerClass , $ controllerMethod , $ route ->getPath () ),
56
67
];
57
68
58
69
if ($ showControllers ) {
59
- $ controller = $ route -> getDefault ( ' _controller ' );
70
+ $ controller = $ this -> transformControllerToLink ( $ controllerClass , $ controllerMethod , $ controller );
60
71
$ row [] = $ controller ? $ this ->formatCallable ($ controller ) : '' ;
61
72
}
62
73
@@ -101,7 +112,7 @@ protected function describeRoute(Route $route, array $options = [])
101
112
$ table ->render ();
102
113
}
103
114
104
- /**
115
+ /**
105
116
* {@inheritdoc}
106
117
*/
107
118
protected function describeContainerParameters (ParameterBag $ parameters , array $ options = [])
@@ -442,12 +453,39 @@ private function formatRouterConfig(array $config): string
442
453
443
454
$ configAsString = '' ;
444
455
foreach ($ config as $ key => $ value ) {
456
+ if ('_controller ' === $ key ) {
457
+ list ($ controllerClass , $ controllerMethod ) = explode (':: ' , $ value );
458
+ $ value = $ this ->transformControllerToLink ($ controllerClass , $ controllerMethod , $ value );
459
+ }
460
+
445
461
$ configAsString .= sprintf ("\n%s: %s " , $ key , $ this ->formatValue ($ value ));
446
462
}
447
463
448
464
return trim ($ configAsString );
449
465
}
450
466
467
+ private function transformControllerToLink (string $ className , string $ method = null , string $ anchorText = null )
468
+ {
469
+ $ fileLink = $ this ->getFileLink ($ className , $ method );
470
+
471
+ return sprintf ('<href=%s>%s</> ' , $ fileLink , $ anchorText );
472
+ }
473
+
474
+ private function getFileLink (string $ className , string $ method = null ): ?string
475
+ {
476
+ try {
477
+ $ r = new \ReflectionClass ($ className );
478
+ } catch (\ReflectionException $ e ) {
479
+ return null ;
480
+ }
481
+
482
+ if (null !== $ method ) {
483
+ $ r = $ r ->getMethod ($ method );
484
+ }
485
+
486
+ return $ this ->fileLinkFormatter ->format ($ r ->getFileName (), $ r ->getStartLine ());
487
+ }
488
+
451
489
private function formatCallable ($ callable ): string
452
490
{
453
491
if (\is_array ($ callable )) {
0 commit comments