8000 [FrameworkBundle] Add file links to named controllers in debug:router · symfony/symfony@c3e552b · GitHub
[go: up one dir, main page]

Skip to content

Commit c3e552b

Browse files
committed
[FrameworkBundle] Add file links to named controllers in debug:router
1 parent 3987914 commit c3e552b

File tree

5 files changed

+95
-42
lines changed

5 files changed

+95
-42
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
5.1.0
55
-----
66

7+
* Added link to source for controllers registered as named services
78
* Added link to source on controller on `router:match`/`debug:router` (when `framework.ide` is configured)
89
* Added `Routing\Loader` and `Routing\Loader\Configurator` namespaces to ease defining routes with default controllers
910
* Added the `framework.router.context` configuration node to configure the `RequestContext`
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\Bundle\FrameworkBundle\Command;
13+
14+
use Symfony\Component\Config\ConfigCache;
15+
use Symfony\Component\Config\FileLocator;
16+
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
19+
20+
/**
21+
* @internal
22+
*
23+
* @author Robin Chalas <robin.chalas@gmail.com>
24+
* @author Nicolas Grekas <p@tchwork.com
25+
*/
26+
trait BuildDebugContainerTrait
27+
{
28+
protected $containerBuilder;
29+
30+
/**
31+
* Loads the ContainerBuilder from the cache.
32+
*
33+
* @throws \LogicException
34+
*/
35+
protected function getContainerBuilder(): ContainerBuilder
36+
{
37+
if ($this->containerBuilder) {
38+
return $this->containerBuilder;
39+
}
40+
41+
$kernel = $this->getApplication()->getKernel();
42+
43+
if (!$kernel->isDebug() || !(new ConfigCache($kernel->getContainer()->getParameter('debug.container.dump'), true))->isFresh()) {
44+
$buildContainer = \Closure::bind(function () { return $this->buildContainer(); }, $kernel, \get_class($kernel));
45+
$container = $buildContainer();
46+
$container->getCompilerPassConfig()->setRemovingPasses([]);
47+
$container->getCompilerPassConfig()->setAfterRemovingPasses([]);
48+
$container->compile();
49+
} else {
50+
(new XmlFileLoader($container = new ContainerBuilder(), new 9E88 FileLocator()))->load($kernel->getContainer()->getParameter('debug.container.dump'));
51+
$locatorPass = new ServiceLocatorTagPass();
52+
$locatorPass->process($container);
53+
}
54+
55+
return $this->containerBuilder = $container;
56+
}
57+
}

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

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,15 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

1414
use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
15-
use Symfony\Component\Config\ConfigCache;
16-
use Symfony\Component\Config\FileLocator;
1715
use Symfony\Component\Console\Command\Command;
1816
use Symfony\Component\Console\Exception\InvalidArgumentException;
1917
use Symfony\Component\Console\Input\InputArgument;
2018
use Symfony\Component\Console\Input\InputInterface;
2119
use Symfony\Component\Console\Input\InputOption;
2220
use Symfony\Component\Console\Output\OutputInterface;
2321
use Symfony\Component\Console\Style\SymfonyStyle;
24-
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
2522
use Symfony\Component\DependencyInjection\ContainerBuilder;
2623
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
27-
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2824
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2925

3026
/**
@@ -36,12 +32,9 @@
3632
*/
3733
class ContainerDebugCommand extends Command
3834
{
39-
protected static $defaultName = 'debug:container';
35+
use BuildDebugContainerTrait;
4036

41-
/**
42-
* @var ContainerBuilder|null
43-
*/
44-
protected $containerBuilder;
37+
protected static $defaultName = 'debug:container';
4538

4639
/**
4740
* {@inheritdoc}
@@ -219,34 +212,6 @@ protected function validateInput(InputInterface $input)
219212
}
220213
}
221214

222-
/**
223-
* Loads the ContainerBuilder from the cache.
224-
*
225-
* @throws \LogicException
226-
*/
227-
protected function getContainerBuilder(): ContainerBuilder
228-
{
229-
if ($this->containerBuilder) {
230-
return $this->containerBuilder;
231-
}
232-
233-
$kernel = $this->getApplication()->getKernel();
234-
235-
if (!$kernel->isDebug() || !(new ConfigCache($kernel->getContainer()->getParameter('debug.container.dump'), true))->isFresh()) {
236-
$buildContainer = \Closure::bind(function () { return $this->buildContainer(); }, $kernel, \get_class($kernel));
237-
$container = $buildContainer();
238-
$container->getCompilerPassConfig()->setRemovingPasses([]);
239-
$container->getCompilerPassConfig()->setAfterRemovingPasses([]);
240-
$container->compile();
241-
} else {
242-
(new XmlFileLoader($container = new ContainerBuilder(), new FileLocator()))->load($kernel->getContainer()->getParameter('debug.container.dump'));
243-
$locatorPass = new ServiceLocatorTagPass();
244-
$locatorPass->process($container);
245-
}
246-
247-
return $this->containerBuilder = $container;
248-
}
249-
250215
private function findProperServiceName(InputInterface $input, SymfonyStyle $io, ContainerBuilder $builder, string $name, bool $showHidden): string
251216
{
252217
$name = ltrim($name, '\\');

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

Lines changed: 6 additions & 0 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\DependencyInjection\ContainerBuilder;
2223
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
2324
use Symfony\Component\Routing\RouteCollection;
2425
use Symfony\Component\Routing\RouterInterface;
@@ -33,6 +34,8 @@
3334
*/
3435
class RouterDebugCommand extends Command
3536
{
37+
use BuildDebugContainerTrait;
38+
3639
protected static $defaultName = 'debug:router';
3740
private $router;
3841
private $fileLinkFormatter;
@@ -79,6 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7982
$name = $input->getArgument('name');
8083
$helper = new DescriptorHelper($this->fileLinkFormatter);
8184
$routes = $this->router->getRouteCollection();
85+
$container = $this->fileLinkFormatter ? \Closure::fromCallable([$this, 'getContainerBuilder']) : null;
8286

8387
if ($name) {
8488
if (!($route = $routes->get($name)) && $matchingRoutes = $this->findRouteNameContaining($name, $routes)) {
@@ -96,13 +100,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
96100
'raw_text' => $input->getOption('raw'),
97101
'name' => $name,
98102
'output' => $io,
103+
'container' => $container,
99104
]);
100105
} else {
101106
$helper->describe($io, $routes, [
102107
'format' => $input->getOption('format'),
103108
'raw_text' => $input->getOption('raw'),
104109
'show_controllers' => $input->getOption('show-controllers'),
105110
'output' => $io,
111+
'container' => $container,
106112
]);
107113
}
108114

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ protected function describeRouteCollection(RouteCollection $routes, array $optio
6161
$route->getMethods() ? implode('|', $route->getMethods()) : 'ANY',
6262
$route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY',
6363
'' !== $route->getHost() ? $route->getHost() : 'ANY',
64-
$this->formatControllerLink($controller, $route->getPath()),
64+
$this->formatControllerLink($controller, $route->getPath(), $options['container'] ?? null),
6565
];
6666

6767
if ($showControllers) {
68-
$row[] = $controller ? $this->formatControllerLink($controller, $this->formatCallable($controller)) : '';
68+
$row[] = $controller ? $this->formatControllerLink($controller, $this->formatCallable($controller), $options['container'] ?? null) : '';
6969
}
7070

7171
$tableRows[] = $row;
@@ -84,7 +84,7 @@ protected function describeRoute(Route $route, array $options = [])
8484
{
8585
$defaults = $route->getDefaults();
8686
if (isset($defaults['_controller'])) {
87-
$defaults['_controller'] = $this->formatControllerLink($defaults['_controller'], $this->formatCallable($defaults['_controller']));
87+
$defaults['_controller'] = $this->formatControllerLink($defaults['_controller'], $this->formatCallable($defaults['_controller']), $options['container'] ?? null);
8888
}
8989

9090
$tableHeaders = ['Property', 'Value'];
@@ -528,7 +528,7 @@ private function formatRouterConfig(array $config): string
528528
return trim($configAsString);
529529
}
530530

531-
private function formatControllerLink($controller, string $anchorText): string
531+
private function formatControllerLink($controller, string $anchorText, callable $getContainer = null): string
532532
{
533533
if (null === $this->fileLinkFormatter) {
534534
return $anchorText;
@@ -549,7 +549,31 @@ private function formatControllerLink($controller, string $anchorText): string
549549
$r = new \ReflectionFunction($controller);
550550
}
551551
} catch (\ReflectionException $e) {
552-
return $anchorText;
552 FE88 +
$id = $controller;
553+
$method = '__invoke';
554+
555+
if ($pos = strpos($controller, '::')) {
556+
$id = substr($controller, 0, $pos);
557+
$method = substr($controller, $pos + 2);
558+
}
559+
560+
if (!$getContainer || !($container = $getContainer()) || !$container->has($id)) {
561+
return $anchorText;
562+
}
563+
564+
try {
565+
$definition = $container->findDefinition($id);
566+
$factory = $definition->getFactory();
567+
if (($factory[1] ?? null) && !$factory[0] instanceof Definition) {
568+
$r = new \ReflectionMethod($factory[0], $factory[1]);
569+
} elseif (\is_string($factory)) {
570+
$r = new \ReflectionFunction($factory);
571+
} elseif (($class = $definition->getClass())) {
572+
$r = new \ReflectionMethod($class, $method);
573+
}
574+
} catch (\ReflectionException $e) {
575+
return $anchorText;
576+
}
553577
}
554578

555579
$fileLink = $this->fileLinkFormatter->format($r->getFileName(), $r->getStartLine());

0 commit comments

Comments
 (0)
0