Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e932bae

Browse filesBrowse files
committed
bug #17569 [FrameworkBundle] read commands from bundles when accessing list (havvg)
This PR was merged into the 2.3 branch. Discussion ---------- [FrameworkBundle] read commands from bundles when accessing list | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | This allows access to the list of commands registered by the kernel (bundle and later service ids) programmatically when you do not `run` the application. Commits ------- 0fe3088 register commands from kernel when accessing list
2 parents 1f22290 + 0fe3088 commit e932bae
Copy full SHA for e932bae

File tree

Expand file treeCollapse file tree

3 files changed

+118
-14
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+118
-14
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Console/Application.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Console/Application.php
+29-9Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Console;
1313

14-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1514
use Symfony\Component\Console\Application as BaseApplication;
1615
use Symfony\Component\Console\Input\InputInterface;
1716
use Symfony\Component\Console\Input\InputOption;
1817
use Symfony\Component\Console\Output\OutputInterface;
19-
use Symfony\Component\HttpKernel\KernelInterface;
20-
use Symfony\Component\HttpKernel\Kernel;
18+
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
2119
use Symfony\Component\HttpKernel\Bundle\Bundle;
20+
use Symfony\Component\HttpKernel\Kernel;
21+
use Symfony\Component\HttpKernel\KernelInterface;
2222

2323
/**
2424
* Application.
@@ -69,12 +69,6 @@ public function doRun(InputInterface $input, OutputInterface $output)
6969
{
7070
$this->kernel->boot();
7171

72-
if (!$this->commandsRegistered) {
73-
$this->registerCommands();
74-
75-
$this->commandsRegistered = true;
76-
}
77-
7872
$container = $this->kernel->getContainer();
7973

8074
foreach ($this->all() as $command) {
@@ -96,8 +90,34 @@ public function doRun(InputInterface $input, OutputInterface $output)
9690
return parent::doRun($input, $output);
9791
}
9892

93+
/**
94+
* {@inheritdoc}
95+
*/
96+
public function get($name)
97+
{
98+
$this->registerCommands();
99+
100+
return parent::get($name);
101+
}
102+
103+
/**
104+
* {@inheritdoc}
105+
*/
106+
public function all($namespace = null)
107+
{
108+
$this->registerCommands();
109+
110+
return parent::all($namespace);
111+
}
112+
99113
protected function registerCommands()
100114
{
115+
if ($this->commandsRegistered) {
116+
return;
117+
}
118+
119+
$this->commandsRegistered = true;
120+
101121
foreach ($this->kernel->getBundles() as $bundle) {
102122
if ($bundle instanceof Bundle) {
103123
$bundle->registerCommands($this);

‎src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php
+85-1Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Console;
1313

14-
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1514
use Symfony\Bundle\FrameworkBundle\Console\Application;
15+
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
16+
use Symfony\Component\Console\Command\Command;
1617
use Symfony\Component\Console\Input\ArrayInput;
1718
use Symfony\Component\Console\Output\NullOutput;
1819
use Symfony\Component\Console\Tester\ApplicationTester;
@@ -38,6 +39,89 @@ public function testBundleCommandsAreRegistered()
3839

3940
$application = new Application($kernel);
4041
$application->doRun(new ArrayInput(array('list')), new NullOutput());
42+
43+
// Calling twice: registration should only be done once.
44+
$application->doRun(new ArrayInput(array('list')), new NullOutput());
45+
}
46+
47+
public function testBundleCommandsAreRetrievable()
48+
{
49+
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle');
50+
$bundle->expects($this->once())->method('registerCommands');
51+
52+
$kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
53+
$kernel
54+
->expects($this->any())
55+
->method('getBundles')
56+
->will($this->returnValue(array($bundle)))
57+
;
58+
59+
$application = new Application($kernel);
60+
$application->all();
61+
62+
// Calling twice: registration should only be done once.
63+
$application->all();
64+
}
65+
66+
public function testBundleSingleCommandIsRetrievable()
67+
{
68+
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle');
69+
$bundle->expects($this->once())->method('registerCommands');
70+
71+
$kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
72+
$kernel
73+
->expects($this->any())
74+
->method('getBundles')
75+
->will($this->returnValue(array($bundle)))
76+
;
77+
78+
$application = new Application($kernel);
79+
80+
$command = new Command('example');
81+
$application->add($command);
82+
83+
$this->assertSame($command, $application->get('example'));
84+
}
85+
86+
public function testBundleCommandCanBeFound()
87+
{
88+
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle');
89+
$bundle->expects($this->once())->method('registerCommands');
90+
91+
$kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
92+
$kernel
93+
->expects($this->any())
94+
->method('getBundles')
95+
->will($this->returnValue(array($bundle)))
96+
;
97+
98+
$application = new Application($kernel);
99+
100+
$command = new Command('example');
101+
$application->add($command);
102+
103+
$this->assertSame($command, $application->find('example'));
104+
}
105+
106+
public function testBundleCommandCanBeFoundByAlias()
107+
{
108+
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle');
109+
$bundle->expects($this->once())->method('registerCommands');
110+
111+
$kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
112+
$kernel
113+
->expects($this->any())
114+
->method('getBundles')
115+
->will($this->returnValue(array($bundle)))
116+
;
117+
118+
$application = new Application($kernel);
119+
120+
$command = new Command('example');
121+
$command->setAliases(array('alias'));
122+
$application->add($command);
123+
124+
$this->assertSame($command, $application->find('alias'));
41125
}
42126

43127
public function testBundleCommandsHaveRightContainer()

‎src/Symfony/Component/Console/Application.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Application.php
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ public function has($name)
436436
public function getNamespaces()
437437
{
438438
$namespaces = array();
439-
foreach ($this->commands as $command) {
439+
foreach ($this->all() as $command) {
440440
$namespaces = array_merge($namespaces, $this->extractAllNamespaces($command->getName()));
441441

442442
foreach ($command->getAliases() as $alias) {
@@ -530,7 +530,7 @@ public function find($name)
530530

531531
// name
532532
$commands = array();
533-
foreach ($this->commands as $command) {
533+
foreach ($this->all() as $command) {
534534
$extractedNamespace = $this->extractNamespace($command->getName());
535535
if ($extractedNamespace === $namespace
536536
|| !empty($namespace) && 0 === strpos($extractedNamespace, $namespace)
@@ -556,7 +556,7 @@ public function find($name)
556556

557557
// aliases
558558
$aliases = array();
559-
foreach ($this->commands as $command) {
559+
foreach ($this->all() as $command) {
560560
foreach ($command->getAliases() as $alias) {
561561
$extractedNamespace = $this->extractNamespace($alias);
562562
if ($extractedNamespace === $namespace
@@ -1028,7 +1028,7 @@ private function findAlternativeCommands($name, $abbrevs)
10281028
return $item->getName();
10291029
};
10301030

1031-
return $this->findAlternatives($name, $this->commands, $abbrevs, $callback);
1031+
return $this->findAlternatives($name, $this->all(), $abbrevs, $callback);
10321032
}
10331033

10341034
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.