[RouterDebugCommand] add link to Controllers
This commit is contained in:
parent
bce6124f8f
commit
e9fca21d6b
@ -19,6 +19,7 @@ use Symfony\Component\Console\Input\InputInterface;
|
|||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
|
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
|
||||||
use Symfony\Component\Routing\RouteCollection;
|
use Symfony\Component\Routing\RouteCollection;
|
||||||
use Symfony\Component\Routing\RouterInterface;
|
use Symfony\Component\Routing\RouterInterface;
|
||||||
|
|
||||||
@ -34,12 +35,14 @@ class RouterDebugCommand extends Command
|
|||||||
{
|
{
|
||||||
protected static $defaultName = 'debug:router';
|
protected static $defaultName = 'debug:router';
|
||||||
private $router;
|
private $router;
|
||||||
|
private $fileLinkFormatter;
|
||||||
|
|
||||||
public function __construct(RouterInterface $router)
|
public function __construct(RouterInterface $router, FileLinkFormatter $fileLinkFormatter = null)
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
$this->router = $router;
|
$this->router = $router;
|
||||||
|
$this->fileLinkFormatter = $fileLinkFormatter;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -74,7 +77,7 @@ EOF
|
|||||||
{
|
{
|
||||||
$io = new SymfonyStyle($input, $output);
|
$io = new SymfonyStyle($input, $output);
|
||||||
$name = $input->getArgument('name');
|
$name = $input->getArgument('name');
|
||||||
$helper = new DescriptorHelper();
|
$helper = new DescriptorHelper($this->fileLinkFormatter);
|
||||||
$routes = $this->router->getRouteCollection();
|
$routes = $this->router->getRouteCollection();
|
||||||
|
|
||||||
if ($name) {
|
if ($name) {
|
||||||
|
@ -24,6 +24,7 @@ use Symfony\Component\DependencyInjection\Definition;
|
|||||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
|
||||||
use Symfony\Component\DependencyInjection\Reference;
|
use Symfony\Component\DependencyInjection\Reference;
|
||||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||||
|
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
|
||||||
use Symfony\Component\Routing\Route;
|
use Symfony\Component\Routing\Route;
|
||||||
use Symfony\Component\Routing\RouteCollection;
|
use Symfony\Component\Routing\RouteCollection;
|
||||||
|
|
||||||
@ -34,6 +35,13 @@ use Symfony\Component\Routing\RouteCollection;
|
|||||||
*/
|
*/
|
||||||
class TextDescriptor extends Descriptor
|
class TextDescriptor extends Descriptor
|
||||||
{
|
{
|
||||||
|
private $fileLinkFormatter;
|
||||||
|
|
||||||
|
public function __construct(FileLinkFormatter $fileLinkFormatter = null)
|
||||||
|
{
|
||||||
|
$this->fileLinkFormatter = $fileLinkFormatter;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
@ -48,17 +56,18 @@ class TextDescriptor extends Descriptor
|
|||||||
|
|
||||||
$tableRows = [];
|
$tableRows = [];
|
||||||
foreach ($routes->all() as $name => $route) {
|
foreach ($routes->all() as $name => $route) {
|
||||||
|
$controller = $route->getDefault('_controller');
|
||||||
|
|
||||||
$row = [
|
$row = [
|
||||||
$name,
|
$name,
|
||||||
$route->getMethods() ? implode('|', $route->getMethods()) : 'ANY',
|
$route->getMethods() ? implode('|', $route->getMethods()) : 'ANY',
|
||||||
$route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY',
|
$route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY',
|
||||||
'' !== $route->getHost() ? $route->getHost() : 'ANY',
|
'' !== $route->getHost() ? $route->getHost() : 'ANY',
|
||||||
$route->getPath(),
|
$this->formatControllerLink($controller, $route->getPath()),
|
||||||
];
|
];
|
||||||
|
|
||||||
if ($showControllers) {
|
if ($showControllers) {
|
||||||
$controller = $route->getDefault('_controller');
|
$row[] = $controller ? $this->formatControllerLink($controller, $this->formatCallable($controller)) : '';
|
||||||
$row[] = $controller ? $this->formatCallable($controller) : '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$tableRows[] = $row;
|
$tableRows[] = $row;
|
||||||
@ -514,6 +523,35 @@ class TextDescriptor extends Descriptor
|
|||||||
return trim($configAsString);
|
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
|
private function formatCallable($callable): string
|
||||||
{
|
{
|
||||||
if (\is_array($callable)) {
|
if (\is_array($callable)) {
|
||||||
|
@ -16,6 +16,7 @@ use Symfony\Bundle\FrameworkBundle\Console\Descriptor\MarkdownDescriptor;
|
|||||||
use Symfony\Bundle\FrameworkBundle\Console\Descriptor\TextDescriptor;
|
use Symfony\Bundle\FrameworkBundle\Console\Descriptor\TextDescriptor;
|
||||||
use Symfony\Bundle\FrameworkBundle\Console\Descriptor\XmlDescriptor;
|
use Symfony\Bundle\FrameworkBundle\Console\Descriptor\XmlDescriptor;
|
||||||
use Symfony\Component\Console\Helper\DescriptorHelper as BaseDescriptorHelper;
|
use Symfony\Component\Console\Helper\DescriptorHelper as BaseDescriptorHelper;
|
||||||
|
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
|
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
|
||||||
@ -24,10 +25,10 @@ use Symfony\Component\Console\Helper\DescriptorHelper as BaseDescriptorHelper;
|
|||||||
*/
|
*/
|
||||||
class DescriptorHelper extends BaseDescriptorHelper
|
class DescriptorHelper extends BaseDescriptorHelper
|
||||||
{
|
{
|
||||||
public function __construct()
|
public function __construct(FileLinkFormatter $fileLinkFormatter = null)
|
||||||
{
|
{
|
||||||
$this
|
$this
|
||||||
->register('txt', new TextDescriptor())
|
->register('txt', new TextDescriptor($fileLinkFormatter))
|
||||||
->register('xml', new XmlDescriptor())
|
->register('xml', new XmlDescriptor())
|
||||||
->register('json', new JsonDescriptor())
|
->register('json', new JsonDescriptor())
|
||||||
->register('md', new MarkdownDescriptor())
|
->register('md', new MarkdownDescriptor())
|
||||||
|
@ -111,6 +111,7 @@
|
|||||||
|
|
||||||
<service id="console.command.router_debug" class="Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand">
|
<service id="console.command.router_debug" class="Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand">
|
||||||
<argument type="service" id="router" />
|
<argument type="service" id="router" />
|
||||||
|
<argument type="service" id="debug.file_link_formatter" on-invalid="null" />
|
||||||
<tag name="console.command" command="debug:router" />
|
<tag name="console.command" command="debug:router" />
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user