Skip to content

Commit e4dce22

Browse files
committed
[FrameworkBundle] Inject @controller_name_converter in debug:router
1 parent ddc9d2e commit e4dce22

File tree

4 files changed

+50
-46
lines changed

4 files changed

+50
-46
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@
2929
*/
3030
class RouterDebugCommand extends ContainerAwareCommand
3131
{
32+
private $controllerNameParser;
33+
34+
public function __construct(ControllerNameParser $controllerNameParser = null)
35+
{
36+
parent::__construct();
37+
38+
$this->controllerNameParser = $controllerNameParser;
39+
}
40+
3241
/**
3342
* {@inheritdoc}
3443
*/
@@ -110,10 +119,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
110119

111120
private function convertController(Route $route)
112121
{
113-
$nameParser = new ControllerNameParser($this->getApplication()->getKernel());
114122
if ($route->hasDefault('_controller')) {
123+
if (null === $this->controllerNameParser) {
124+
$this->controllerNameParser = new ControllerNameParser($this->getApplication()->getKernel());
125+
}
115126
try {
116-
$route->setDefault('_controller', $nameParser->build($route->getDefault('_controller')));
127+
$route->setDefault('_controller', $this->controllerNameParser->build($route->getDefault('_controller')));
117128
} catch (\InvalidArgumentException $e) {
118129
}
119130
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,11 @@
9898
<argument type="service" id="router.request_context" on-invalid="ignore" />
9999
<argument type="service" id="logger" on-invalid="ignore" />
100100
</service>
101+
102+
<service id="router.command.debug" class="Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand" public="false">
103+
<tag name="console.command" />
104+
<argument type="service" id="controller_name_converter" />
105+
</service>
106+
101107
</services>
102108
</container>

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

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Command;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Bundle\FrameworkBundle\Console\Application;
15+
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
16+
use Symfony\Component\Console\Application;
1617
use Symfony\Component\Console\Tester\CommandTester;
1718
use Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand;
1819
use Symfony\Component\HttpKernel\KernelInterface;
@@ -52,15 +53,16 @@ public function testDebugInvalidRoute()
5253
*/
5354
private function createCommandTester()
5455
{
55-
$application = new Application($this->getKernel());
56+
$application = new Application();
5657

57-
$command = new RouterDebugCommand();
58+
$command = new RouterDebugCommand(new ControllerNameParser($this->getMockBuilder(KernelInterface::class)->getMock()));
59+
$command->setContainer($this->getContainer());
5860
$application->add($command);
5961

6062
return new CommandTester($application->find('debug:router'));
6163
}
6264

63-
private function getKernel()
65+
private function getContainer()
6466
{
6567
$routeCollection = new RouteCollection();
6668
$routeCollection->add('foo', new Route('foo'));
@@ -82,25 +84,14 @@ private function getKernel()
8284
->with('router')
8385
->will($this->returnValue(true))
8486
;
87+
8588
$container
86-
->expects($this->any())
8789
->method('get')
88-
->with('router')
89-
->willReturn($router)
90-
;
91-
92-
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
93-
$kernel
94-
->expects($this->any())
95-
->method('getContainer')
96-
->willReturn($container)
97-
;
98-
$kernel
99-
->expects($this->once())
100-
->method('getBundles')
101-
->willReturn(array())
102-
;
90+
->will($this->returnValueMap(array(
91+
array('router', 1, $router),
92+
array('controller_name_converter', 1, $loader),
93+
)));
10394

104-
return $kernel;
95+
return $container;
10596
}
10697
}

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

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Command;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Bundle\FrameworkBundle\Console\Application;
15+
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
16+
use Symfony\Component\Console\Application;
1617
use Symfony\Component\Console\Tester\CommandTester;
1718
use Symfony\Bundle\FrameworkBundle\Command\RouterMatchCommand;
1819
use Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand;
@@ -46,14 +47,20 @@ public function testWithNotMatchPath()
4647
*/
4748
private function createCommandTester()
4849
{
49-
$application = new Application($this->getKernel());
50-
$application->add(new RouterMatchCommand());
51-
$application->add(new RouterDebugCommand());
50+
$application = new Application();
51+
52+
$command = new RouterMatchCommand();
53+
$command->setContainer($this->getContainer());
54+
$application->add($command);
55+
56+
$command = new RouterDebugCommand(new ControllerNameParser($this->getMockBuilder(KernelInterface::class)->getMock()));
57+
$command->setContainer($this->getContainer());
58+
$application->add($command);
5259

5360
return new CommandTester($application->find('router:match'));
5461
}
5562

56-
private function getKernel()
63+
private function getContainer()
5764
{
5865
$routeCollection = new RouteCollection();
5966
$routeCollection->add('foo', new Route('foo'));
@@ -76,27 +83,16 @@ private function getKernel()
7683

7784
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
7885
$container
79-
->expects($this->atLeastOnce())
86+
->expects($this->once())
8087
->method('has')
8188
->with('router')
8289
->will($this->returnValue(true));
83-
$container
84-
->expects($this->any())
85-
->method('get')
86-
->willReturn($router);
87-
88-
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
89-
$kernel
90-
->expects($this->any())
91-
->method('getContainer')
92-
->willReturn($container)
93-
;
94-
$kernel
95-
->expects($this->once())
96-
->method('getBundles')
97-
->willReturn(array())
98-
;
90+
$container->method('get')
91+
->will($this->returnValueMap(array(
92+
array('router', 1, $router),
93+
array('controller_name_converter', 1, $loader),
94+
)));
9995

100-
return $kernel;
96+
return $container;
10197
}
10298
}

0 commit comments

Comments
 (0)