Skip to content

Commit 5517fbc

Browse files
feature #36270 [FrameworkBundle] Add file links to named controllers in debug:router (chalasr)
This PR was merged into the 5.1-dev branch. Discussion ---------- [FrameworkBundle] Add file links to named controllers in debug:router | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Before ![Screenshot 2020-03-31 at 21 52 11](https://user-images.githubusercontent.com/7502063/78069168-ee189380-7399-11ea-90ef-dedce6f96131.png) After ![Screenshot 2020-03-31 at 21 51 11](https://user-images.githubusercontent.com/7502063/78069198-fb358280-7399-11ea-8ab8-eaa24f76bbac.png) Commits ------- 932ae91 [FrameworkBundle] Add file links to named controllers in debug:router
2 parents 0647508 + 932ae91 commit 5517fbc

File tree

5 files changed

+86
-42
lines changed

5 files changed

+86
-42
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

+1
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`
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 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

+2-37
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

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
*/
3434
class RouterDebugCommand extends Command
3535
{
36+
use BuildDebugContainerTrait;
37+
3638
protected static $defaultName = 'debug:router';
3739
private $router;
3840
private $fileLinkFormatter;
@@ -79,6 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7981
$name = $input->getArgument('name');
8082
$helper = new DescriptorHelper($this->fileLinkFormatter);
8183
$routes = $this->router->getRouteCollection();
84+
$container = $this->fileLinkFormatter ? \Closure::fromCallable([$this, 'getContainerBuilder']) : null;
8285

8386
if ($name) {
8487
if (!($route = $routes->get($name)) && $matchingRoutes = $this->findRouteNameContaining($name, $routes)) {
@@ -96,13 +99,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9699
'raw_text' => $input->getOption('raw'),
97100
'name' => $name,
98101
'output' => $io,
102+
'container' => $container,
99103
]);
100104
} else {
101105
$helper->describe($io, $routes, [
102106
'format' => $input->getOption('format'),
103107
'raw_text' => $input->getOption('raw'),
104108
'show_controllers' => $input->getOption('show-controllers'),
105109
'output' => $io,
110+
'container' => $container,
106111
]);
107112
}
108113

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

+21-5
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,23 @@ private function formatControllerLink($controller, string $anchorText): string
549549
$r = new \ReflectionFunction($controller);
550550
}
551551
} catch (\ReflectionException $e) {
552-
return $anchorText;
552+
$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+
$r = new \ReflectionMethod($container->findDefinition($id)->getClass(), $method);
566+
} catch (\ReflectionException $e) {
567+
return $anchorText;
568+
}
553569
}
554570

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

0 commit comments

Comments
 (0)