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

Skip to content

Commit 1521372

Browse files
committed
[RouterDebugCommand] add link to Controllers
1 parent f54c89c commit 1521372

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,16 +55,19 @@ protected function describeRouteCollection(RouteCollection $routes, array $optio
4755

4856
$tableRows = [];
4957
foreach ($routes->all() as $name => $route) {
58+
$controller = $route->getDefault('_controller');
59+
list ($controllerClass, $controllerMethod) = explode('::', $controller);
60+
5061
$row = [
5162
$name,
5263
$route->getMethods() ? implode('|', $route->getMethods()) : 'ANY',
5364
$route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY',
5465
'' !== $route->getHost() ? $route->getHost() : 'ANY',
55-
$route->getPath(),
66+
$this->transformControllerToLink($controllerClass, $controllerMethod, $route->getPath()),
5667
];
5768

5869
if ($showControllers) {
59-
$controller = $route->getDefault('_controller');
70+
$controller = $this->transformControllerToLink($controllerClass, $controllerMethod, $controller);
6071
$row[] = $controller ? $this->formatCallable($controller) : '';
6172
}
6273

@@ -101,7 +112,7 @@ protected function describeRoute(Route $route, array $options = [])
101112
$table->render();
102113
}
103114

104-
/**
115+
/**
105116
* {@inheritdoc}
106117
*/
107118
protected function describeContainerParameters(ParameterBag $parameters, array $options = [])
@@ -442,12 +453,39 @@ private function formatRouterConfig(array $config): string
442453

443454
$configAsString = '';
444455
foreach ($config as $key => $value) {
456+
if ('_controller' === $key) {
457+
list ($controllerClass, $controllerMethod) = explode('::', $value);
458+
$value = $this->transformControllerToLink($controllerClass, $controllerMethod, $value);
459+
}
460+
445461
$configAsString .= sprintf("\n%s: %s", $key, $this->formatValue($value));
446462
}
447463

448464
return trim($configAsString);
449465
}
450466

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+
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)