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

Skip to content

Commit e30c1cd

Browse files
author
Amrouche Hamza
committed
[FrameworkBundle] feature: add the ability to search a route
1 parent d65c43b commit e30c1cd

File tree

6 files changed

+136
-2
lines changed

6 files changed

+136
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ CHANGELOG
1717
is either the service ID or the FQCN of the controller.
1818
* Deprecated `Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser`
1919
* The `container.service_locator` tag of `ServiceLocator`s is now autoconfigured.
20-
20+
* Add the ability to search a route in `debug:router`.
21+
2122
4.0.0
2223
-----
2324

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Console\Output\OutputInterface;
2020
use Symfony\Component\Console\Style\SymfonyStyle;
2121
use Symfony\Component\Routing\RouterInterface;
22+
use Symfony\Component\Routing\RouteCollection;
2223

2324
/**
2425
* A console command for retrieving information about routes.
@@ -76,7 +77,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
7677
$routes = $this->router->getRouteCollection();
7778

7879
if ($name) {
79-
if (!$route = $routes->get($name)) {
80+
if (!($route = $routes->get($name)) && $matchingRoutes = $this->findRouteNameContaining($name, $routes)) {
81+
$default = 1 === count($matchingRoutes) ? $matchingRoutes[0] : null;
82+
$name = $io->choice('Select one of the following routes to display its information', $matchingRoutes, $default);
83+
$route = $routes->get($name);
84+
}
85+
86+
if (!$route) {
8087
throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
8188
}
8289

@@ -95,4 +102,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
95102
));
96103
}
97104
}
105+
private function findRouteNameContaining(string $name, RouteCollection $routes): array
106+
{
107+
$foundRoutesNames = array();
108+
foreach ($routes as $routeName => $route) {
109+
if (false !== stripos($routeName, $name)) {
110+
$foundRoutesNames[] = $routeName;
111+
}
112+
}
113+
114+
return $foundRoutesNames;
115+
}
98116
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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\Tester\CommandTester;
16+
17+
/**
18+
* @group functional
19+
*/
20+
class RouterDebugCommandTest extends WebTestCase
21+
{
22+
private $application;
23+
24+
protected function setUp()
25+
{
26+
$kernel = static::createKernel(array('test_case' => 'RouterDebug', 'root_config' => 'config.yml'));
27+
$this->application = new Application($kernel);
28+
}
29+
30+
public function testDumpAllRoutes()
31+
{
32+
$tester = $this->createCommandTester();
33+
$ret = $tester->execute(array());
34+
$display = $tester->getDisplay();
35+
36+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
37+
$this->assertContains('routerdebug_test', $display);
38+
$this->assertContains('/test', $display);
39+
$this->assertContains('/session', $display);
40+
}
41+
42+
public function testDumpOneRoutes()
43+
{
44+
$tester = $this->createCommandTester();
45+
$ret = $tester->execute(array('name' => 'routerdebug_session_welcome'));
46+
47+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
48+
$this->assertContains('routerdebug_session_welcome', $tester->getDisplay());
49+
$this->assertContains('/session', $tester->getDisplay());
50+
}
51+
52+
public function testSearchMultipleRoutes()
53+
{
54+
$tester = $this->createCommandTester();
55+
$tester->setInputs(array(3));
56+
$ret = $tester->execute(array('name' => 'routerdebug'), array('interactive' => true));
57+
58+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
59+
$this->assertContains('Select one of the following routes to display its information:', $tester->getDisplay());
60+
$this->assertContains('routerdebug_test', $tester->getDisplay());
61+
$this->assertContains('/test', $tester->getDisplay());
62+
}
63+
64+
/**
65+
* @expectedException \InvalidArgumentException
66+
* @expectedExceptionMessage The route "gerard" does not exist.
67+
*/
68+
public function testSearchWithThrow()
69+
{
70+
$tester = $this->createCommandTester();
71+
$tester->execute(array('name' => 'gerard'), array('interactive' => true));
72+
}
73+
74+
private function createCommandTester(): CommandTester
75+
{
76+
$command = $this->application->get('debug:router');
77+
78+
return new CommandTester($command);
79+
}
80+
}
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