Skip to content

Commit

Permalink
feature #26121 [FrameworkBundle] feature: add the ability to search a…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
fabpot committed Mar 21, 2018
2 parents c559fa3 + ef0df02 commit 14ab56e
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 110 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
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
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.

@@ -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);
}
}
@@ -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(),
);
@@ -0,0 +1,2 @@
imports:
- { resource: ../config/default.yml }
@@ -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 }

0 comments on commit 14ab56e

Please sign in to comment.