Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d90c9fa

Browse files
committed
[RouterDebugCommand] add link to Controllers
1 parent 18cd342 commit d90c9fa

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Console\Input\InputOption;
2020
use Symfony\Component\Console\Output\OutputInterface;
2121
use Symfony\Component\Console\Style\SymfonyStyle;
22+
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
2223
use Symfony\Component\Routing\RouteCollection;
2324
use Symfony\Component\Routing\RouterInterface;
2425

@@ -34,12 +35,14 @@ class RouterDebugCommand extends Command
3435
{
3536
protected static $defaultName = 'debug:router';
3637
private $router;
38+
private $fileLinkFormatter;
3739

38-
public function __construct(RouterInterface $router)
40+
public function __construct(RouterInterface $router, FileLinkFormatter $fileLinkFormatter = null)
3941
{
4042
parent::__construct();
4143

4244
$this->router = $router;
45+
$this->fileLinkFormatter = $fileLinkFormatter;
4346
}
4447

4548
/**
@@ -74,7 +77,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7477
{
7578
$io = new SymfonyStyle($input, $output);
7679
$name = $input->getArgument('name');
77-
$helper = new DescriptorHelper();
80+
$helper = new DescriptorHelper($this->fileLinkFormatter);
7881
$routes = $this->router->getRouteCollection();
7982

8083
if ($name) {

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2424
use Symfony\Component\DependencyInjection\Reference;
2525
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
26+
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
2627
use Symfony\Component\Routing\Route;
2728
use Symfony\Component\Routing\RouteCollection;
2829

@@ -33,6 +34,13 @@
3334
*/
3435
class TextDescriptor extends Descriptor
3536
{
37+
private $fileLinkFormatter;
38+
39+
public function __construct(FileLinkFormatter $fileLinkFormatter = null)
40+
{
41+
$this->fileLinkFormatter = $fileLinkFormatter;
42+
}
43+
3644
/**
3745
* {@inheritdoc}
3846
*/
@@ -47,17 +55,18 @@ protected function describeRouteCollection(RouteCollection $routes, array $optio
4755

4856
$tableRows = [];
4957
foreach ($routes->all() as $name => $route) {
58+
$controller = $route->getDefault('_controller');
59+
5060
$row = [
5161
$name,
5262
$route->getMethods() ? implode('|', $route->getMethods()) : 'ANY',
5363
$route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY',
5464
'' !== $route->getHost() ? $route->getHost() : 'ANY',
55-
$route->getPath(),
65+
$this->formatControllerLink($controller, $route->getPath()),
5666
];
5767

5868
if ($showControllers) {
59-
$controller = $route->getDefault('_controller');
60-
$row[] = $controller ? $this->formatCallable($controller) : '';
69+
$row[] = $controller ? $this->formatControllerLink($controller, $this->formatCallable($controller)) : '';
6170
}
6271

6372
$tableRows[] = $row;
@@ -448,6 +457,35 @@ private function formatRouterConfig(array $config): string
448457
return trim($configAsString);
449458
}
450459

460+
private function formatControllerLink($controller, string $anchorText): string
461+
{
462+
if (null === $this->fileLinkFormatter) {
463+
return $anchorText;
464+
}
465+
466+
try {
467+
if (\is_array($controller)) {
468+
$r = new \ReflectionMethod($controller);
469+
} elseif ($controller instanceof \Closure) {
470+
$r = new \ReflectionFunction($controller);
471+
} elseif (method_exists($controller, '__invoke')) {
472+
$r = new \ReflectionMethod($controller, '__invoke');
473+
} elseif (!\is_string($controller)) {
474+
return $anchorText;
475+
} elseif (false !== strpos($controller, '::')) {
476+
$r = new \ReflectionMethod($controller);
477+
} else {
478+
$r = new \ReflectionFunction($controller);
479+
}
480+
} catch (\ReflectionException $e) {
481+
return $anchorText;
482+
}
483+
484+
$fileLink = $this->fileLinkFormatter->format($r->getFileName(), $r->getStartLine());
485+
486+
return sprintf('<href=%s>%s</>', $fileLink, $anchorText);
487+
}
488+
451489
private function formatCallable($callable): string
452490
{
453491
if (\is_array($callable)) {

src/Symfony/Bundle/FrameworkBundle/Console/Helper/DescriptorHelper.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Bundle\FrameworkBundle\Console\Descriptor\TextDescriptor;
1717
use Symfony\Bundle\FrameworkBundle\Console\Descriptor\XmlDescriptor;
1818
use Symfony\Component\Console\Helper\DescriptorHelper as BaseDescriptorHelper;
19+
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
1920

2021
/**
2122
* @author Jean-François Simon <[email protected]>
@@ -24,10 +25,10 @@
2425
*/
2526
class DescriptorHelper extends BaseDescriptorHelper
2627
{
27-
public function __construct()
28+
public function __construct(FileLinkFormatter $fileLinkFormatter = null)
2829
{
2930
$this
30-
->register('txt', new TextDescriptor())
31+
->register('txt', new TextDescriptor($fileLinkFormatter))
3132
->register('xml', new XmlDescriptor())
3233
->register('json', new JsonDescriptor())
3334
->register('md', new MarkdownDescriptor())

src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191

9292
<service id="console.command.router_debug" class="Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand">
9393
<argument type="service" id="router" />
94+
<argument type="service" id="debug.file_link_formatter" />
9495
<tag name="console.command" command="debug:router" />
9596
</service>
9697

0 commit comments

Comments
 (0)