8000 [FrameworkBundle] feature: add the ability to search a route · symfony/symfony@41fb042 · GitHub
[go: up one dir, main page]

Skip to content

Commit 41fb042

Browse files
author
Amrouche Hamza
committed
[FrameworkBundle] feature: add the ability to search a route
1 parent 4d6c481 commit 41fb042

File tree

6 files changed

+162
-1
lines changed

6 files changed

+162
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ CHANGELOG
1010
* Added option in workflow dump command to label graph with a custom label
1111
* Using a `RouterInterface` that does not implement the `WarmableInterface` is deprecated and will not be supported in Symfony 5.0.
1212
* The `RequestDataCollector` class has been deprecated and will be removed in Symfony 5.0. Use the `Symfony\Component\HttpKernel\DataCollector\RequestDataCollector` class instead.
13-
13+
* Add the ability to search a route in `debug:router`.
14+
1415
4.0.0
1516
-----
1617

src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Console\Output\OutputInterface;
2121
use Symfony\Component\Console\Style\SymfonyStyle;
2222
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
23+
use Symfony\Component\Routing\RouteCollection;
2324
use Symfony\Component\Routing\RouterInterface;
2425
use Symfony\Component\Routing\Route;
2526

@@ -79,6 +80,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
7980
$routes = $this->router->getRouteCollection();
8081

8182
if ($name) {
83+
$matchingRoutes = $this->findRouteNameContaining($name, $routes);
84+
85+
if (!empty($matchingRoutes) && !$route = $routes->get($name)) {
86+
$default = 1 === count($matchingRoutes) ? $matchingRoutes[0] : null;
87+
$name = $io->choice('Select one of the following routes to display its information', $matchingRoutes, $default);
88+
}
89+
8290
if (!$route = $routes->get($name)) {
8391
throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
8492
}
@@ -142,4 +150,17 @@ private function extractCallable(Route $route)
142150
} catch (\InvalidArgumentException $e) {
143151
}
144152
}
153+
154+
private function findRouteNameContaining(string $name, RouteCollection $routes)
155+
{
156+
$foundRoutesNames = array();
157+
foreach ($routes as $routeName => $route) {
158+
if (false === stripos($routeName, $name)) {
159+
continue;
160+
}
161+
$foundRoutesNames[] = $routeName;
162+
}
163+
164+
return $foundRoutesNames;
165+
}
145166
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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\Tests\Functional;
13+
14+
use Symfony\Bundle\FrameworkBundle\Console\Application;
15+
use Symfony\Component\Console\Input\ArrayInput;
16+
use Symfony\Component\Console\Output\NullOutput;
17+
use Symfony\Component\Console\Tester\CommandTester;
18+
19+
/**
20+
* @group functional
21+
*/
22+
class RouterDebugCommandTest extends WebTestCase
23+
{
24+
private $application;
25+
26+
protected function setUp()
27+
{
28+
$kernel = static::createKernel(array('test_case' => 'RouterDebug', 'root_config' => 'config.yml'));
29+
$this->application = new Application($kernel);
30+
$this->application->doRun(new ArrayInput(array()), new NullOutput());
31+
}
32+
33+
public function testDumpAllRoutes()
34+
{
35+
$tester = $this->createCommandTester();
36+
$ret = $tester->execute(array());
37+
$display = $tester->getDisplay();
38+
39+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
40+
$this->assertContains('routerdebug_test', $display);
41+
$this->assertContains('/test', $display);
42+
$this->assertContains('/session', $display);
43+
}
44+
45+
public function testDumpOneRoutes()
46+
{
47+
$tester = $this->createCommandTester();
48+
$ret = $tester->execute(array('name' => 'routerdebug_session_welcome'));
49+
50+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
51+
$this->assertContains('+--------------+---------------------------------------------------------------------------------------------------------------+
52+
| Property | Value |
53+
+--------------+---------------------------------------------------------------------------------------------------------------+
54+
| Route Name | routerdebug_session_welcome |
55+
| Path | /session |
56+
| Path Regex | #^/session$#sD |
57+
| Host | ANY |
58+
| Host Regex | |
59+
| Scheme | ANY |
60+
| Method | ANY |
61+
| Requirements | NO CUSTOM |
62+
| Class | Symfony\Component\Routing\Route |
63+
| Defaults | _controller: FrameworkBundle:Session:welcome |
64+
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
65+
| Callable | Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction |
66+
+--------------+---------------------------------------------------------------------------------------------------------------+
67+
', $tester->getDisplay());
68+
}
69+
70+
public function testSearchRoutes()
71+
{
72+
$tester = $this->createCommandTester();
73+
$ret = $tester->execute(array('name' => 'routerdebug_tes'), array('interactive' => false));
74+
75+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
76+
$this->assertContains('+--------------+---------------------------------------------------------------------------------------------------------------+
77+
| Property | Value |
78+
+--------------+---------------------------------------------------------------------------------------------------------------+
79+
| Route Name | routerdebug_test |
80+
| Path | /test |
81+
| Path Regex | #^/test$#sD |
82+
| Host | ANY |
83+
| Host Regex | |
84+
| Scheme | ANY |
85+
| Method | ANY |
86+
| Requirements | NO CUSTOM |
87+
| Class | Symfony\Component\Routing\Route |
88+
| Defaults | _controller: FrameworkBundle:Session:welcome |
89+
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
90+
| Callable | Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction |
91+
+--------------+---------------------------------------------------------------------------------------------------------------+
92+
', $tester->getDisplay());
93+
}
94+
95+
/**
96+
* @return CommandTester
97+
*/
98+
private function createCommandTester()
99+
{
100+
$command = $this->application->find('debug:router');
101+
102+
return new CommandTester($command);
103+
}
104+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
13+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
14+
15+
return array(
16+
new FrameworkBundle(),
17+
new TestBundle(),
18+
);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
imports:
2+
- { resource: ../config/default.yml }
< 6D4E div role="region" aria-labelledby="heading-:Rdmlab:" class="position-relative" style="contain:layout">
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
routerdebug_session_welcome:
2+
path: /session
3+
defaults: { _controller: TestBundle:Session:welcome }
4+
5+
routerdebug_session_welcome_name:
6+
path: /session/{name}
7+
defaults: { _controller: TestBundle:Session:welcome }
8+
9+
routerdebug_session_logout:
10+
path: /session_logout
11+
defaults: { _controller: TestBundle:Session:logout}
12+
13+
routerdebug_test:
14+
path: /test
15+
defaults: { _controller: TestBundle:Session:welcome}

0 commit comments

Comments
 (0)
0