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

Skip to content

Commit 3a5df79

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

File tree

6 files changed

+163
-1
lines changed

6 files changed

+163
-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: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
38+
$display = $tester->getDisplay();
39+
$this->assertContains('routerdebug_test', $display);
40+
$this->assertContains('/test', $display);
41+
$this->assertContains('/session', $display);
42+
43+
44+
}
45+
46+
public function testDumpOneRoutes()
47+
{
48+
$tester = $this->createCommandTester();
49+
$ret = $tester->execute(array('name' => 'routerdebug_session_welcome'));
50+
51+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
52+
$this->assertContains('+--------------+---------------------------------------------------------------------------------------------------------------+
53+
| Property | Value |
54+
+--------------+---------------------------------------------------------------------------------------------------------------+
55+
| Route Name | routerdebug_session_welcome |
56+
| Path | /session |
57+
| Path Regex | #^/session$#sD |
58+
| Host | ANY |
59+
| Host Regex | |
60+
| Scheme | ANY |
61+
| Method | ANY |
62+
| Requirements | NO CUSTOM |
63+
| Class | Symfony\Component\Routing\Route |
64+
| Defaults | _controller: FrameworkBundle:Session:welcome |
65+
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
66+
| Callable | Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction |
67+
+--------------+---------------------------------------------------------------------------------------------------------------+
68+
', $tester->getDisplay());
69+
}
70+
71+
public function testSearchRoutes()
72+
{
73+
$tester = $this->createCommandTester();
74+
$ret = $tester->execute(array('name' => 'routerdebug_tes'), array('interactive' => false));
75+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
76+
var_dump($tester->getDisplay());
77+
$this->assertContains('+--------------+---------------------------------------------------------------------------------------------------------------+
78+
| Property | Value |
79+
+--------------+---------------------------------------------------------------------------------------------------------------+
80+
| Route Name | routerdebug_test |
81+
| Path | /test |
82+
| Path Regex | #^/test$#sD |
83+
| Host | ANY |
84+
| Host Regex | |
85+
| Scheme | ANY |
86+
| Method | ANY |
87+
| Requirements | NO CUSTOM |
88+
| Class | Symfony\Component\Routing\Route |
89+
| Defaults | _controller: FrameworkBundle:Session:welcome |
90+
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
91+
| Callable | Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction |
92+
+--------------+---------------------------------------------------------------------------------------------------------------+
93+
', $tester->getDisplay());
94+
}
95+
96+
/**
97+
* @return CommandTester
98+
*/
99+
private function createCommandTester()
100+
{
101+
$command = $this->application->find('debug:router');
102+
103+
return new CommandTester($command);
104+
}
105+
}
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