From a96690cce5671effa237da6f2f261411998d511f Mon Sep 17 00:00:00 2001 From: Laurent VOULLEMIER Date: Thu, 19 Mar 2020 16:38:44 +0100 Subject: [PATCH] [FrameworkBundle][Routing] Add link to source to router:match --- .../Bundle/FrameworkBundle/CHANGELOG.md | 1 + .../Console/Descriptor/TextDescriptor.php | 7 +++- .../Console/Descriptor/TextDescriptorTest.php | 36 ++++++++++++++++++- .../Fixtures/Descriptor/route_1_link.txt | 18 ++++++++++ .../Fixtures/Descriptor/route_2_link.txt | 18 ++++++++++ 5 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_1_link.txt create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_2_link.txt diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 46f15a2669..8cf2cb453a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 5.1.0 ----- + * Added link to source on controller on `router:match`/`debug:router` (when `framework.ide` is configured) * Added `Routing\Loader` and `Routing\Loader\Configurator` namespaces to ease defining routes with default controllers * Added the `framework.router.context` configuration node to configure the `RequestContext` * Made `MicroKernelTrait::configureContainer()` compatible with `ContainerConfigurator` diff --git a/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php b/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php index 12f2797bc5..b34503192e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php +++ b/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php @@ -82,6 +82,11 @@ class TextDescriptor extends Descriptor protected function describeRoute(Route $route, array $options = []) { + $defaults = $route->getDefaults(); + if (isset($defaults['_controller'])) { + $defaults['_controller'] = $this->formatControllerLink($defaults['_controller'], $this->formatCallable($defaults['_controller'])); + } + $tableHeaders = ['Property', 'Value']; $tableRows = [ ['Route Name', isset($options['name']) ? $options['name'] : ''], @@ -93,7 +98,7 @@ class TextDescriptor extends Descriptor ['Method', ($route->getMethods() ? implode('|', $route->getMethods()) : 'ANY')], ['Requirements', ($route->getRequirements() ? $this->formatRouterConfig($route->getRequirements()) : 'NO CUSTOM')], ['Class', \get_class($route)], - ['Defaults', $this->formatRouterConfig($route->getDefaults())], + ['Defaults', $this->formatRouterConfig($defaults)], ['Options', $this->formatRouterConfig($route->getOptions())], ]; diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/TextDescriptorTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/TextDescriptorTest.php index 4ed0446320..4c2caba543 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/TextDescriptorTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/TextDescriptorTest.php @@ -12,9 +12,13 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor; use Symfony\Bundle\FrameworkBundle\Console\Descriptor\TextDescriptor; +use Symfony\Component\HttpKernel\Debug\FileLinkFormatter; +use Symfony\Component\Routing\Route; class TextDescriptorTest extends AbstractDescriptorTest { + private $fileLinkFormatter = null; + protected function setUp(): void { putenv('COLUMNS=121'); @@ -27,11 +31,41 @@ class TextDescriptorTest extends AbstractDescriptorTest protected function getDescriptor() { - return new TextDescriptor(); + return new TextDescriptor($this->fileLinkFormatter); } protected function getFormat() { return 'txt'; } + + public function getDescribeRouteWithControllerLinkTestData() + { + $getDescribeData = $this->getDescribeRouteTestData(); + + foreach ($getDescribeData as $key => &$data) { + $routeStub = $data[0]; + $routeStub->setDefault('_controller', sprintf('%s::%s', MyController::class, '__invoke')); + $file = $data[2]; + $file = preg_replace('#(\..*?)$#', '_link$1', $file); + $data = file_get_contents(__DIR__.'/../../Fixtures/Descriptor/'.$file); + $data = [$routeStub, $data, $file]; + } + + return $getDescribeData; + } + + /** @dataProvider getDescribeRouteWithControllerLinkTestData */ + public function testDescribeRouteWithControllerLink(Route $route, $expectedDescription) + { + $this->fileLinkFormatter = new FileLinkFormatter('myeditor://open?file=%f&line=%l'); + parent::testDescribeRoute($route, str_replace('[:file:]', __FILE__, $expectedDescription)); + } +} + +class MyController +{ + public function __invoke() + { + } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_1_link.txt b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_1_link.txt new file mode 100644 index 0000000000..8d86bc7be8 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_1_link.txt @@ -0,0 +1,18 @@ ++--------------+-----------------------------------------------------------------------------------------------+ +| Property | Value | ++--------------+-----------------------------------------------------------------------------------------------+ +| Route Name | | +| Path | /hello/{name} | +| Path Regex | #PATH_REGEX# | +| Host | localhost | +| Host Regex | #HOST_REGEX# | +| Scheme | http|https | +| Method | GET|HEAD | +| Requirements | name: [a-z]+ | +| Class | Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub | +| Defaults | _controller: ]8;;myeditor://open?file=[:file:]&line=68\Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\MyController::__invoke()]8;;\ | +| | name: Joseph | +| Options | compiler_class: Symfony\Component\Routing\RouteCompiler | +| | opt1: val1 | +| | opt2: val2 | ++--------------+-----------------------------------------------------------------------------------------------+ diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_2_link.txt b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_2_link.txt new file mode 100644 index 0000000000..a244b515ca --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_2_link.txt @@ -0,0 +1,18 @@ ++--------------+-----------------------------------------------------------------------------------------------+ +| Property | Value | ++--------------+-----------------------------------------------------------------------------------------------+ +| Route Name | | +| Path | /name/add | +| Path Regex | #PATH_REGEX# | +| Host | localhost | +| Host Regex | #HOST_REGEX# | +| Scheme | http|https | +| Method | PUT|POST | +| Requirements | NO CUSTOM | +| Class | Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\RouteStub | +| Defaults | _controller: ]8;;myeditor://open?file=[:file:]&line=68\Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor\MyController::__invoke()]8;;\ | +| Options | compiler_class: Symfony\Component\Routing\RouteCompiler | +| | opt1: val1 | +| | opt2: val2 | +| Condition | context.getMethod() in ['GET', 'HEAD', 'POST'] | ++--------------+-----------------------------------------------------------------------------------------------+