Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commite932bae

Browse 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 parents1f22290 +0fe3088 commite932bae

File tree

3 files changed

+118
-14
lines changed

3 files changed

+118
-14
lines changed

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
namespaceSymfony\Bundle\FrameworkBundle\Console;
1313

14-
useSymfony\Component\DependencyInjection\ContainerAwareInterface;
1514
useSymfony\Component\Console\ApplicationasBaseApplication;
1615
useSymfony\Component\Console\Input\InputInterface;
1716
useSymfony\Component\Console\Input\InputOption;
1817
useSymfony\Component\Console\Output\OutputInterface;
19-
useSymfony\Component\HttpKernel\KernelInterface;
20-
useSymfony\Component\HttpKernel\Kernel;
18+
useSymfony\Component\DependencyInjection\ContainerAwareInterface;
2119
useSymfony\Component\HttpKernel\Bundle\Bundle;
20+
useSymfony\Component\HttpKernel\Kernel;
21+
useSymfony\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
returnparent::doRun($input,$output);
9791
}
9892

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

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

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
namespaceSymfony\Bundle\FrameworkBundle\Tests\Console;
1313

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

3940
$application =newApplication($kernel);
4041
$application->doRun(newArrayInput(array('list')),newNullOutput());
42+
43+
// Calling twice: registration should only be done once.
44+
$application->doRun(newArrayInput(array('list')),newNullOutput());
45+
}
46+
47+
publicfunctiontestBundleCommandsAreRetrievable()
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 =newApplication($kernel);
60+
$application->all();
61+
62+
// Calling twice: registration should only be done once.
63+
$application->all();
64+
}
65+
66+
publicfunctiontestBundleSingleCommandIsRetrievable()
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 =newApplication($kernel);
79+
80+
$command =newCommand('example');
81+
$application->add($command);
82+
83+
$this->assertSame($command,$application->get('example'));
84+
}
85+
86+
publicfunctiontestBundleCommandCanBeFound()
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 =newApplication($kernel);
99+
100+
$command =newCommand('example');
101+
$application->add($command);
102+
103+
$this->assertSame($command,$application->find('example'));
104+
}
105+
106+
publicfunctiontestBundleCommandCanBeFoundByAlias()
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 =newApplication($kernel);
119+
120+
$command =newCommand('example');
121+
$command->setAliases(array('alias'));
122+
$application->add($command);
123+
124+
$this->assertSame($command,$application->find('alias'));
41125
}
42126

43127
publicfunctiontestBundleCommandsHaveRightContainer()

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ public function has($name)
436436
publicfunctiongetNamespaces()
437437
{
438438
$namespaces =array();
439-
foreach ($this->commandsas$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->commandsas$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->commandsas$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)

[8]ページ先頭

©2009-2025 Movatter.jp