Skip to content

Commit ace979c

Browse files
committed
Add support for command lazy-loading
1 parent de418f4 commit ace979c

File tree

5 files changed

+35
-18
lines changed

5 files changed

+35
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ CHANGELOG
1818
`Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass` instead
1919
* Deprecated `TranslatorPass`, use
2020
`Symfony\Component\Translation\DependencyInjection\TranslatorPass` instead
21+
* Added `command` attribute to the `console.command` tag which takes the command
22+
name as value, using it makes the command lazy
2123

2224
3.3.0
2325
-----

Console/Application.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,7 @@ public function doRun(InputInterface $input, OutputInterface $output)
6868
{
6969
$this->kernel->boot();
7070

71-
$container = $this->kernel->getContainer();
72-
73-
foreach ($this->all() as $command) {
74-
if ($command instanceof ContainerAwareInterface) {
75-
$command->setContainer($container);
76-
}
77-
}
78-
79-
$this->setDispatcher($container->get('event_dispatcher'));
71+
$this->setDispatcher($this->kernel->getContainer()->get('event_dispatcher'));
8072

8173
return parent::doRun($input, $output);
8274
}
@@ -98,7 +90,13 @@ public function get($name)
9890
{
9991
$this->registerCommands();
10092

101-
return parent::get($name);
93+
$command = parent::get($name);
94+
95+
if ($command instanceof ContainerAwareInterface) {
96+
$command->setContainer($this->kernel->getContainer());
97+
}
98+
99+
return $command;
102100
}
103101

104102
/**
@@ -144,9 +142,15 @@ protected function registerCommands()
144142
}
145143
}
146144

145+
if ($container->has('console.command_loader')) {
146+
$this->setCommandLoader($container->get('console.command_loader'));
147+
}
148+
147149
if ($container->hasParameter('console.command.ids')) {
148150
foreach ($container->getParameter('console.command.ids') as $id) {
149-
$this->add($container->get($id));
151+
if (false !== $id) {
152+
$this->add($container->get($id));
153+
}
150154
}
151155
}
152156
}

Tests/Command/RouterDebugCommandTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,15 @@ private function getKernel()
7777

7878
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
7979
$container
80-
->expects($this->once())
80+
->expects($this->atLeastOnce())
8181
->method('has')
82-
->with('router')
83-
->will($this->returnValue(true))
82+
->will($this->returnCallback(function ($id) {
83+
if ('console.command_loader' === $id) {
84+
return false;
85+
}
86+
87+
return true;
88+
}))
8489
;
8590
$container
8691
->expects($this->any())

Tests/Command/RouterMatchCommandTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,14 @@ private function getKernel()
7878
$container
7979
->expects($this->atLeastOnce())
8080
->method('has')
81-
->with('router')
82-
->will($this->returnValue(true));
81+
->will($this->returnCallback(function ($id) {
82+
if ('console.command_loader' === $id) {
83+
return false;
84+
}
85+
86+
return true;
87+
}))
88+
;
8389
$container
8490
->expects($this->any())
8591
->method('get')

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"fig/link-util": "^1.0",
3636
"symfony/asset": "~3.3|~4.0",
3737
"symfony/browser-kit": "~2.8|~3.0|~4.0",
38-
"symfony/console": "~3.3|~4.0",
38+
"symfony/console": "~3.4|~4.0",
3939
"symfony/css-selector": "~2.8|~3.0|~4.0",
4040
"symfony/dom-crawler": "~2.8|~3.0|~4.0",
4141
"symfony/polyfill-intl-icu": "~1.0",
@@ -64,7 +64,7 @@
6464
"phpdocumentor/type-resolver": "<0.2.0",
6565
"phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0",
6666
"symfony/asset": "<3.3",
67-
"symfony/console": "<3.3",
67+
"symfony/console": "<3.4",
6868
"symfony/form": "<3.3",
6969
"symfony/property-info": "<3.3",
7070
"symfony/serializer": "<3.3",

0 commit comments

Comments
 (0)