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

Skip to content

Commit 29561eb

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

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: 19 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,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
7980
$routes = $this->router->getRouteCollection();
8081

8182
if ($name) {
83+
$matchingRoutes = $this->findRouteNameContaining($name, $routes);
84+
if ($matchingRoutes && !$route = $routes->get($name)) {
85+
$default = 1 === count($matchingRoutes) ? $matchingRoutes[0] : null;
86+
$name = $io->choice('Select one of the following routes to display its information', $matchingRoutes, $default);
87+
}
88+
8289
if (!$route = $routes->get($name)) {
8390
throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
8491
}
@@ -142,4 +149,16 @@ private function extractCallable(Route $route)
142149
} catch (\InvalidArgumentException $e) {
143150
}
144151
}
152+
153+
private function findRouteNameContaining(string $name, RouteCollection $routes): array
154+
{
155+
$foundRoutesNames = array();
156+
foreach ($routes as $routeName => $route) {
157+
if (false !== stripos($routeName, $name)) {
158+
$foundRoutesNames[] = $routeName;
159+
}
160+
}
161+
162+
return $foundRoutesNames;
163+
}
145164
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 testSearchMultipleRoutes()
71+
{
72+
$tester = $this->createCommandTester();
73+
$tester->setInputs(array(3));
74+
$ret = $tester->execute(array('name' => 'routerdebug'), array('interactive' => true));
75+
76+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
77+
$this->assertContains('Select one of the following routes to display its information:', $tester->getDisplay());
78+
$this->assertContains('+--------------+---------------------------------------------------------------------------------------------------------------+
79+
| Property | Value |
80+
+--------------+---------------------------------------------------------------------------------------------------------------+
81+
| Route Name | routerdebug_test |
82+
| Path | /test |
83+
| Path Regex | #^/test$#sD |
84+
| Host | ANY |
85+
| Host Regex | |
86+
| Scheme | ANY |
87+
| Method | ANY |
88+
| Requirements | NO CUSTOM |
89+
| Class | Symfony\Component\Routing\Route |
90+
| Defaults | _controller: FrameworkBundle:Session:welcome |
91+
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
92+
| Callable | Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction |
93+
+--------------+---------------------------------------------------------------------------------------------------------------+
94+
', $tester->getDisplay());
95+
}
96+
97+
/**
98+
* @return CommandTester
99+
*/
100+
private function createCommandTester()
101+
{
102+
$command = $this->application->find('debug:router');
103+
104+
return new CommandTester($command);
105+
}
106+
}
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 }
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