Skip to content

[FrameworkBundle] feature: add the ability to search a route #26121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ CHANGELOG
is either the service ID or the FQCN of the controller.
* Deprecated `Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser`
* The `container.service_locator` tag of `ServiceLocator`s is now autoconfigured.
* Add the ability to search a route in `debug:router`.

4.0.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\RouteCollection;

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

if ($name) {
if (!$route = $routes->get($name)) {
if (!($route = $routes->get($name)) && $matchingRoutes = $this->findRouteNameContaining($name, $routes)) {
$default = 1 === count($matchingRoutes) ? $matchingRoutes[0] : null;
$name = $io->choice('Select one of the matching routes', $matchingRoutes, $default);
$route = $routes->get($name);
}

if (!$route) {
throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
}

Expand All @@ -95,4 +102,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
));
}
}

private function findRouteNameContaining(string $name, RouteCollection $routes): array
{
$foundRoutesNames = array();
foreach ($routes as $routeName => $route) {
if (false !== stripos($routeName, $name)) {
$foundRoutesNames[] = $routeName;
}
}

return $foundRoutesNames;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

/**
* @group functional
*/
class RouterDebugCommandTest extends WebTestCase
{
private $application;

protected function setUp()
{
$kernel = static::createKernel(array('test_case' => 'RouterDebug', 'root_config' => 'config.yml'));
$this->application = new Application($kernel);
}

public function testDumpAllRoutes()
{
$tester = $this->createCommandTester();
$ret = $tester->execute(array());
$display = $tester->getDisplay();

$this->assertSame(0, $ret, 'Returns 0 in case of success');
$this->assertContains('routerdebug_test', $display);
$this->assertContains('/test', $display);
$this->assertContains('/session', $display);
}

public function testDumpOneRoute()
{
$tester = $this->createCommandTester();
$ret = $tester->execute(array('name' => 'routerdebug_session_welcome'));

$this->assertSame(0, $ret, 'Returns 0 in case of success');
$this->assertContains('routerdebug_session_welcome', $tester->getDisplay());
$this->assertContains('/session', $tester->getDisplay());
}

public function testSearchMultipleRoutes()
{
$tester = $this->createCommandTester();
$tester->setInputs(array(3));
$ret = $tester->execute(array('name' => 'routerdebug'), array('interactive' => true));

$this->assertSame(0, $ret, 'Returns 0 in case of success');
$this->assertContains('Select one of the matching routes:', $tester->getDisplay());
$this->assertContains('routerdebug_test', $tester->getDisplay());
$this->assertContains('/test', $tester->getDisplay());
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The route "gerard" does not exist.
*/
public function testSearchWithThrow()
{
$tester = $this->createCommandTester();
$tester->execute(array('name' => 'gerard'), array('interactive' => true));
}

private function createCommandTester(): CommandTester
{
$command = $this->application->get('debug:router');

return new CommandTester($command);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;

return array(
new FrameworkBundle(),
new TestBundle(),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: ../config/default.yml }
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
routerdebug_session_welcome:
path: /session
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction }

routerdebug_session_welcome_name:
path: /session/{name}
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction }

routerdebug_session_logout:
path: /session_logout
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::logoutAction }

routerdebug_test:
path: /test
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SessionController::welcomeAction }