Skip to content

Commit

Permalink
feature #30520 [RouterDebugCommand] add link to Controllers (nicoweb)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.3-dev branch.

Discussion
----------

[RouterDebugCommand] add link to Controllers

| Q             | A
| ------------- | ---
| Branch?       | master for features
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Adds a link to the controller method on your IDE from dev's terminal:

<img width="734" alt="pr-debug-router" src="https://user-images.githubusercontent.com/29813575/54141007-1f3bc400-4425-11e9-82d0-1b37498d4953.png">

Configuration in your `services.yaml`:

```yaml
parameters:
    debug.file_link_format: phpstorm://open?file=%%f&line=%%l
```

Commits
-------

e9fca21 [RouterDebugCommand] add link to Controllers
  • Loading branch information
fabpot committed Apr 3, 2019
2 parents 539f4ca + e9fca21 commit 99bf6c2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
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
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
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
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 comments on commit 99bf6c2

Please sign in to comment.