Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 14ab56e

Browse files
committed
feature #26121 [FrameworkBundle] feature: add the ability to search a route (Simperfit)
This PR was merged into the 4.1-dev branch. Discussion ---------- [FrameworkBundle] feature: add the ability to search a route | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no <!-- don't forget to update UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | #26033 | License | MIT | Doc PR | symfony/symfony-docs#9236 This add the ability to search a route in the debug:router command. ![img_3271](https://user-images.githubusercontent.com/3451634/36034017-e60cbfda-0db2-11e8-841a-60bc75b0b631.jpeg) Commits ------- ef0df02 [FrameworkBundle] feature: add the ability to search a route
2 parents c559fa3 + ef0df02 commit 14ab56e

File tree

7 files changed

+136
-110
lines changed

7 files changed

+136
-110
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ 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+
* Add the ability to search a route in `debug:router`.
2021

2122
4.0.0
2223
-----

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

Lines changed: 20 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 matching routes', $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,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
95102
));
96103
}
97104
}
105+
106+
private function findRouteNameContaining(string $name, RouteCollection $routes): array
107+
{
108+
$foundRoutesNames = array();
109+
foreach ($routes as $routeName => $route) {
110+
if (false !== stripos($routeName, $name)) {
111+
$foundRoutesNames[] = $routeName;
112+
}
113+
}
114+
115+
return $foundRoutesNames;
116+
}
98117
}

src/Symfony/Bundle/FrameworkBundle/Tests/Command/RouterDebugCommandTest.php

Lines changed: 0 additions & 109 deletions
This file was deleted.
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 <[email protected]>
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 testDumpOneRoute()
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 matching routes:', $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 <[email protected]>
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: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction }
4+
5+
routerdebug_session_welcome_name:
6+
path: /session/{name}
7+
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction }
8+
9+
routerdebug_session_logout:
10+
path: /session_logout
11+
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::logoutAction }
12+
13+
routerdebug_test:
14+
path: /test
15+
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction }

0 commit comments

Comments
 (0)