8000 [RouterDebugCommand] add link to Controllers by nicoweb · Pull Request #30520 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;

Expand All @@ -34,12 +35,14 @@ class RouterDebugCommand extends Command
{
protected static $defaultName = 'debug:router';
private $router;
private $fileLinkFormatter;

public function __construct(RouterInterface $router)
public function __construct(RouterInterface $router, FileLinkFormatter $fileLinkFormatter = null)
{
parent::__construct();

$this->router = $router;
$this->fileLinkFormatter = $fileLinkFormatter;
}

/**
Expand Down Expand Up @@ -74,7 +77,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$name = $input->getArgument('name');
$helper = new DescriptorHelper();
$helper = new DescriptorHelper($this->fileLinkFormatter);
$routes = $this->router->getRouteCollection();

if ($name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

Expand All @@ -34,6 +35,13 @@
*/
class TextDescriptor extends Descriptor
{
private $fileLinkFormatter;

public function __construct(FileLinkFormatter $fileLinkFormatter = null)
{
$this->fileLinkFormatter = $fileLinkFormatter;
}

/**
* {@inheritdoc}
*/
Expand All @@ -48,17 +56,18 @@ protected function describeRouteCollection(RouteCollection $routes, array $optio

$tableRows = [];
foreach ($routes->all() as $name => $route) {
$controller = $route->getDefault('_controller');

$row = [
$name,
$route->getMethods() ? implode('|', $route->getMethods()) : 'ANY',
$route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY',
'' !== $route->getHost() ? $route->getHost() : 'ANY',
$route->getPath(),
$this->formatControllerLink($controller, $route->getPath()),
];

if ($showControllers) {
$controller = $route->getDefault('_controller');
$row[] = $controller ? $this->formatCallable($controller) : '';
$row[] = $controller ? $this->formatControllerLink($controller, $this->formatCallable($controller)) : '';
}

$tableRows[] = $row;
Expand Down Expand Up @@ -514,6 +523,35 @@ private function formatRouterConfig(array $config): string
return trim($configAsString);
}

private function formatControllerLink($controller, string $anchorText): string
{
if (null === $this->fileLinkFormatter) {
return $anchorText;
}

try {
if (\is_array($controller)) {
$r = new \ReflectionMethod($controller);
} elseif ($controller instanceof \Closure) {
$r = new \ReflectionFunction($controller);
} elseif (method_exists($controller, '__invoke')) {
$r = new \ReflectionMethod($controller, '__invoke');
} elseif (!\is_string($controller)) {
return $anchorText;
} elseif (false !== strpos($controller, '::')) {
$r = new \ReflectionMethod($controller);
} else {
$r = new \ReflectionFunction($controller);
}
} catch (\ReflectionException $e) {
return $anchorText;
}

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

return sprintf('<href=%s>%s</>', $fileLink, $anchorText);
}

private function formatCallable($callable): string
{
if (\is_array($callable)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Bundle\FrameworkBundle\Console\Descriptor\TextDescriptor;
use Symfony\Bundle\FrameworkBundle\Console\Descriptor\XmlDescriptor;
use Symfony\Component\Console\Helper\DescriptorHelper as BaseDescriptorHelper;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;

/**
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
Expand All @@ -24,10 +25,10 @@
*/
class DescriptorHelper extends BaseDescriptorHelper
{
public function __construct()
public function __construct(FileLinkFormatter $fileLinkFormatter = null)
{
$this
->register('txt', new TextDescriptor())
->register('txt', new TextDescriptor($fileLinkFormatter))
->register('xml', new XmlDescriptor())
->register('json', new JsonDescriptor())
->register('md', new MarkdownDescriptor())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@

<service id="console.command.router_debug" class="Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand">
<argument type="service" id="router" />
<argument type="service" id="debug.file_link_formatter" on-invalid="null" />
<tag name="console.command" command="debug:router" />
</service>

Expand Down
0