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

Commitf04cd0f

Browse files
committed
[Console] Add support for command lazy-loading
1 parent7b59412 commitf04cd0f

File tree

15 files changed

+382
-44
lines changed

15 files changed

+382
-44
lines changed

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

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

7070
$container =$this->kernel->getContainer();
71+
$this->setDispatcher($container->get('event_dispatcher'));
7172

72-
foreach ($this->all()as$command) {
73-
if ($commandinstanceof ContainerAwareInterface) {
74-
$command->setContainer($container);
75-
}
73+
if ($container->has('console.command_loader')) {
74+
$this->setCommandLoader($container->get('console.command_loader'));
7675
}
7776

78-
$this->setDispatcher($container->get('event_dispatcher'));
79-
8077
returnparent::doRun($input,$output);
8178
}
8279

@@ -97,7 +94,13 @@ public function get($name)
9794
{
9895
$this->registerCommands();
9996

100-
returnparent::get($name);
97+
$command =parent::get($name);
98+
99+
if ($commandinstanceof ContainerAwareInterface) {
100+
$command->setContainer($this->kernel->getContainer());
101+
}
102+
103+
return$command;
101104
}
102105

103106
/**
@@ -136,9 +139,12 @@ protected function registerCommands()
136139
}
137140
}
138141

142+
// @deprecated since version 3.4, to be removed in 4.0
139143
if ($container->hasParameter('console.command.ids')) {
140144
foreach ($container->getParameter('console.command.ids')as$id) {
141-
$this->add($container->get($id));
145+
if (false !==$id) {
146+
$this->add($container->get($id));
147+
}
142148
}
143149
}
144150
}

‎src/Symfony/Bundle/FrameworkBundle/composer.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"fig/link-util":"^1.0",
3737
"symfony/asset":"~3.3",
3838
"symfony/browser-kit":"~2.8|~3.0",
39-
"symfony/console":"~3.3",
39+
"symfony/console":"~3.4",
4040
"symfony/css-selector":"~2.8|~3.0",
4141
"symfony/dom-crawler":"~2.8|~3.0",
4242
"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",

‎src/Symfony/Bundle/SecurityBundle/Resources/config/console.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<serviceid="security.console.user_password_encoder_command"class="Symfony\Bundle\SecurityBundle\Command\UserPasswordEncoderCommand">
1111
<argumenttype="service"id="security.encoder_factory"/>
1212
<argumenttype="collection" /><!-- encoders' user classes-->
13-
<tagname="console.command" />
13+
<tagname="console.command"command="security:encode-password"/>
1414
</service>
1515
</services>
1616
</container>

‎src/Symfony/Bundle/SecurityBundle/Tests/Functional/UserPasswordEncoderCommandTest.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ protected function setUp()
222222
$kernel->boot();
223223

224224
$application =newApplication($kernel);
225-
225+
if ($kernel->getContainer()->has('console.command_loader')) {
226+
$application->setCommandLoader($kernel->getContainer()->get('console.command_loader'));
227+
}
226228
$passwordEncoderCommand =$application->get('security:encode-password');
227229

228230
$this->passwordEncoderCommandTester =newCommandTester($passwordEncoderCommand);

‎src/Symfony/Bundle/SecurityBundle/composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"require-dev": {
2626
"symfony/asset":"~2.8|~3.0",
2727
"symfony/browser-kit":"~2.8|~3.0",
28-
"symfony/console":"~3.2",
28+
"symfony/console":"~3.4",
2929
"symfony/css-selector":"~2.8|~3.0",
3030
"symfony/dom-crawler":"~2.8|~3.0",
3131
"symfony/form":"^2.8.18|^3.2.5",

‎src/Symfony/Bundle/WebServerBundle/Resources/config/webserver.xml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@
1010
<serviceid="web_server.command.server_run"class="Symfony\Bundle\WebServerBundle\Command\ServerRunCommand">
1111
<argument>%kernel.root_dir%/../web</argument>
1212
<argument>%kernel.environment%</argument>
13-
<tagname="console.command" />
13+
<tagname="console.command"command="server:run"/>
1414
</service>
1515

1616
<serviceid="web_server.command.server_start"class="Symfony\Bundle\WebServerBundle\Command\ServerStartCommand">
1717
<argument>%kernel.root_dir%/../web</argument>
1818
<argument>%kernel.environment%</argument>
19-
<tagname="console.command" />
19+
<tagname="console.command"command="server:start"/>
2020
</service>
2121

2222
<serviceid="web_server.command.server_stop"class="Symfony\Bundle\WebServerBundle\Command\ServerStopCommand">
23-
<tagname="console.command" />
23+
<tagname="console.command"command="server:stop"/>
2424
</service>
2525

2626
<serviceid="web_server.command.server_status"class="Symfony\Bundle\WebServerBundle\Command\ServerStatusCommand">
27-
<tagname="console.command" />
27+
<tagname="console.command"command="server:status"/>
2828
</service>
2929
</services>
3030
</container>

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

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespaceSymfony\Component\Console;
1313

14+
useSymfony\Component\Console\CommandLoader\CommandLoaderInterface;
1415
useSymfony\Component\Console\Exception\ExceptionInterface;
1516
useSymfony\Component\Console\Formatter\OutputFormatter;
1617
useSymfony\Component\Console\Helper\DebugFormatterHelper;
@@ -64,6 +65,7 @@ class Application
6465
private$runningCommand;
6566
private$name;
6667
private$version;
68+
private$commandLoader;
6769
private$catchExceptions =true;
6870
private$autoExit =true;
6971
private$definition;
@@ -96,6 +98,11 @@ public function setDispatcher(EventDispatcherInterface $dispatcher)
9698
$this->dispatcher =$dispatcher;
9799
}
98100

101+
publicfunctionsetCommandLoader(CommandLoaderInterface$commandLoader)
102+
{
103+
$this->commandLoader =$commandLoader;
104+
}
105+
99106
/**
100107
* Runs the current application.
101108
*
@@ -431,6 +438,10 @@ public function add(Command $command)
431438
thrownewLogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.',get_class($command)));
432439
}
433440

441+
if (!$command->getName()) {
442+
thrownewLogicException(sprintf('The command defined in "%s" cannot have an empty name.',get_class($command)));
443+
}
444+
434445
$this->commands[$command->getName()] =$command;
435446

436447
foreach ($command->getAliases()as$alias) {
@@ -451,12 +462,18 @@ public function add(Command $command)
451462
*/
452463
publicfunctionget($name)
453464
{
454-
if (!isset($this->commands[$name])) {
465+
if (isset($this->commands[$name])) {
466+
$command =$this->commands[$name];
467+
}elseif ($this->commandLoader &&$this->commandLoader->has($name)) {
468+
$command =$this->commandLoader->get($name);
469+
if (!$command->getName()) {
470+
$command->setName($name);
471+
}
472+
$this->add($command);
473+
}else {
455474
thrownewCommandNotFoundException(sprintf('The command "%s" does not exist.',$name));
456475
}
457476

458-
$command =$this->commands[$name];
459-
460477
if ($this->wantHelps) {
461478
$this->wantHelps =false;
462479

@@ -478,7 +495,7 @@ public function get($name)
478495
*/
479496
publicfunctionhas($name)
480497
{
481-
returnisset($this->commands[$name]);
498+
returnisset($this->commands[$name]) ||$this->commandLoader &&$this->commandLoader->has($name);
482499
}
483500

484501
/**
@@ -555,7 +572,7 @@ public function findNamespace($namespace)
555572
*/
556573
publicfunctionfind($name)
557574
{
558-
$allCommands =array_keys($this->commands);
575+
$allCommands =$this->commandLoader ?array_merge($this->commandLoader->getCommandNames(),array_keys($this->commands)) :array_keys($this->commands);
559576
$expr =preg_replace_callback('{([^:]+|)}',function ($matches) {returnpreg_quote($matches[1]).'[^:]*'; },$name);
560577
$commands =preg_grep('{^'.$expr.'}',$allCommands);
561578

@@ -581,12 +598,12 @@ public function find($name)
581598

582599
// filter out aliases for commands which are already on the list
583600
if (count($commands) >1) {
584-
$commandList =$this->commands;
585-
$commands =array_filter($commands,function ($nameOrAlias)use ($commandList,$commands) {
586-
$commandName =$commandList[$nameOrAlias]->getName();
601+
$commandList =$this->commandLoader ?array_merge(array_flip($this->commandLoader->getCommandNames()),$this->commands) :$this->commands;
602+
$commands =array_unique(array_filter($commands,function ($nameOrAlias)use ($commandList,$commands) {
603+
$commandName =$commandList[$nameOrAlias]instanceof Command ?$commandList[$nameOrAlias]->getName() :$nameOrAlias;
587604

588605
return$commandName ===$nameOrAlias || !in_array($commandName,$commands);
589-
});
606+
}));
590607
}
591608

592609
$exact =in_array($name,$commands,true);
@@ -598,6 +615,9 @@ public function find($name)
598615
$maxLen =max(Helper::strlen($abbrev),$maxLen);
599616
}
600617
$abbrevs =array_map(function ($cmd)use ($commandList,$usableWidth,$maxLen) {
618+
if (!$commandList[$cmd]instanceof Command) {
619+
return$cmd;
620+
}
601621
$abbrev =str_pad($cmd,$maxLen,'').''.$commandList[$cmd]->getDescription();
602622

603623
return Helper::strlen($abbrev) >$usableWidth ? Helper::substr($abbrev,0,$usableWidth -3).'...' :$abbrev;
@@ -622,7 +642,18 @@ public function find($name)
622642
publicfunctionall($namespace =null)
623643
{
624644
if (null ===$namespace) {
625-
return$this->commands;
645+
if (!$this->commandLoader) {
646+
return$this->commands;
647+
}
648+
649+
$commands =$this->commands;
650+
foreach ($this->commandLoader->getCommandNames()as$name) {
651+
if (!isset($commands[$name])) {
652+
$commands[$name] =$this->commandLoader->get($name);
653+
}
654+
}
655+
656+
return$commands;
626657
}
627658

628659
$commands =array();
@@ -632,6 +663,14 @@ public function all($namespace = null)
632663
}
633664
}
634665

666+
if ($this->commandLoader) {
667+
foreach ($this->commandLoader->getCommandNames()as$name) {
668+
if (!isset($commands[$name]) &&$namespace ===$this->extractNamespace($name,substr_count($namespace,':') +1)) {
669+
$commands[$name] =$this->commandLoader->get($name);
670+
}
671+
}
672+
}
673+
635674
return$commands;
636675
}
637676

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ public function __construct($name = null)
6161
}
6262

6363
$this->configure();
64-
65-
if (!$this->name) {
66-
thrownewLogicException(sprintf('The command defined in "%s" cannot have an empty name.',get_class($this)));
67-
}
6864
}
6965

7066
/**
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespaceSymfony\Component\Console\CommandLoader;
4+
5+
useSymfony\Component\Console\Command\Command;
6+
useSymfony\Component\Console\Exception\CommandNotFoundException;
7+
8+
/**
9+
* @author Robin Chalas <robin.chalas@gmail.com>
10+
*/
11+
interface CommandLoaderInterface
12+
{
13+
/**
14+
* Returns a registered command by name.
15+
*
16+
* @param string $name The command name
17+
*
18+
* @return Command A Command object
19+
*
20+
* @throws CommandNotFoundException When given command name does not exist
21+
*/
22+
publicfunctionget($name);
23+
24+
/**
25+
* Checks if a command exists by name.
26+
*
27+
* @param string $name The command name for which to check the existence
28+
*
29+
* @return bool
30+
*/
31+
publicfunctionhas($name);
32+
33+
/**
34+
* @return iterable All registered commands mapped by names
35+
*/
36+
publicfunctionall();
37+
38+
/**
39+
* @return string[] All registered command names
40+
*/
41+
publicfunctiongetCommandNames();
42+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespaceSymfony\Component\Console\CommandLoader;
4+
5+
usePsr\Container\ContainerInterface;
6+
useSymfony\Component\Console\Exception\CommandNotFoundException;
7+
8+
/**
9+
* Loads commands from a PSR-11 container.
10+
*
11+
* @author Robin Chalas <robin.chalas@gmail.com>
12+
*/
13+
class ContainerCommandLoaderimplements CommandLoaderInterface
14+
{
15+
private$container;
16+
private$commandNames;
17+
18+
publicfunction__construct(ContainerInterface$container,array$commandNames)
19+
{
20+
$this->container =$container;
21+
$this->commandNames =$commandNames;
22+
}
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
publicfunctionget($name)
28+
{
29+
if (!in_array($name,$this->commandNames,true)) {
30+
thrownewCommandNotFoundException(sprintf('Command "%s" does not exist.',$name));
31+
}
32+
33+
return$this->container->get($name);
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
publicfunctionhas($name)
40+
{
41+
returnin_array($name,$this->commandNames,true);
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
publicfunctionall()
48+
{
49+
foreach ($this->commandNamesas$name) {
50+
yield$name =>$this->container->get($name);
51+
}
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
publicfunctiongetCommandNames()
58+
{
59+
return$this->commandNames;
60+
}
61+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp