Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 770cf8c

Browse files
committed
Add comand definition
1 parent 29b88af commit 770cf8c

File tree

8 files changed

+363
-16
lines changed

8 files changed

+363
-16
lines changed

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

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

1414
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Command\CommandDescription;
16+
use Symfony\Component\Console\Command\DescribableCommandInterface;
1517
use Symfony\Component\Console\Exception\RuntimeException;
1618
use Symfony\Component\Console\Input\InputInterface;
1719
use Symfony\Component\Console\Input\InputOption;
@@ -33,10 +35,8 @@
3335
*
3436
* @final
3537
*/
36-
class CacheClearCommand extends Command
38+
class CacheClearCommand extends Command implements DescribableCommandInterface
3739
{
38-
protected static $defaultName = 'cache:clear';
39-
4040
private $cacheClearer;
4141
private $filesystem;
4242

@@ -48,6 +48,14 @@ public function __construct(CacheClearerInterface $cacheClearer, Filesystem $fil
4848
$this->filesystem = $filesystem ?: new Filesystem();
4949
}
5050

51+
public static function describe(): CommandDescription
52+
{
53+
return new CommandDescription(
54+
'cache:clear',
55+
'Clears the cache'
56+
);
57+
}
58+
5159
/**
5260
* {@inheritdoc}
5361
*/
@@ -58,7 +66,6 @@ protected function configure()
5866
new InputOption('no-warmup', '', InputOption::VALUE_NONE, 'Do not warm up the cache'),
5967
new InputOption('no-optional-warmers', '', InputOption::VALUE_NONE, 'Skip optional cache warmers (faster)'),
6068
])
61-
->setDescription('Clears the cache')
6269
->setHelp(<<<'EOF'
6370
The <info>%command.name%</info> command clears the application cache for a given environment
6471
and debug mode:

src/Symfony/Component/Console/Application.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Console\Command\Command;
1515
use Symfony\Component\Console\Command\HelpCommand;
16+
use Symfony\Component\Console\Command\LazyCommand;
1617
use Symfony\Component\Console\Command\ListCommand;
1718
use Symfony\Component\Console\Command\SignalableCommandInterface;
1819
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
@@ -489,8 +490,10 @@ public function add(Command $command)
489490
return null;
490491
}
491492

492-
// Will throw if the command is not correctly initialized.
493-
$command->getDefinition();
493+
if (!$command instanceof LazyCommand) {
494+
// Will throw if the command is not correctly initialized.
495+
$command->getDefinition();
496+
}
494497

495498
if (!$command->getName()) {
496499
throw new LogicException(sprintf('The command defined in "%s" cannot have an empty name.', get_debug_type($command)));

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Command
4545
private $aliases = [];
4646
private $definition;
4747
private $hidden = false;
48+
private $enabled = true;
4849
private $help = '';
4950
private $description = '';
5051
private $fullDefinition;
@@ -73,8 +74,15 @@ public static function getDefaultName()
7374
public function __construct(string $name = null)
7475
{
7576
$this->definition = new InputDefinition();
76-
77-
if (null !== $name || null !== $name = static::getDefaultName()) {
77+
if ($this instanceof DescribableCommandInterface) {
78+
$description = static::describe();
79+
$this->setName($description->getName())
80+
->setAliases($description->getAliases())
81+
->setHidden($description->isHidden())
82+
->setDescription($description->getDescription());
83+
84+
$this->enabled = $description->isEnabled();
85+
} elseif (null !== $name || null !== $name = static::getDefaultName()) {
7886
$this->setName($name);
7987
}
8088

@@ -138,7 +146,7 @@ public function getApplication()
138146
*/
139147
public function isEnabled()
140148
{
141-
return true;
149+
return $this->enabled;
142150
}
143151

144152
/**
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Command;
13+
14+
/**
15+
* Interface for command reacting to signal.
16+
*
17+
* @author Jérémy Derussé <[email protected]>
18+
*/
19+
final class CommandDescription
20+
{
21+
private $name;
22+
private $aliases;
23+
private $description;
24+
private $hidden;
25+
private $enabled;
26+
27+
public function __construct(string $name, string $description = '', array $aliases = [], bool $hidden = false, bool $enabled = true)
28+
{
29+
$this->name = $name;
30+
$this->aliases = $aliases;
31+
$this->description = $description;
32+
$this->hidden = $hidden;
33+
$this->enabled = $enabled;
34+
}
35+
36+
public function getName(): string
37+
{
38+
return $this->name;
39+
}
40+
41+
public function getAliases(): array
42+
{
43+
return $this->aliases;
44+
}
45+
46+
public function getDescription(): string
47+
{
48+
return $this->description;
49+
}
50+
51+
public function isEnabled(): bool
52+
{
53+
return $this->enabled;
54+
}
55+
56+
public function isHidden(): bool
57+
{
58+
return $this->hidden;
59+
}
60+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Command;
13+
14+
/**
15+
* Interface for command reacting to signal.
16+
*
17+
* @author Jérémy Derussé <[email protected]>
18+
*/
19+
interface DescribableCommandInterface
20+
{
21+
public static function describe(): CommandDescription;
22+
}
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Command;
13+
14+
use Symfony\Component\Console\Application;
15+
use Symfony\Component\Console\Helper\HelperSet;
16+
use Symfony\Component\Console\Input\InputDefinition;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Output\OutputInterface;
19+
20+
/**
21+
* @author Nicolas Grekas <[email protected]>
22+
*/
23+
final class LazyCommand extends Command
24+
{
25+
private $command;
26+
private $enabled;
27+
28+
public function __construct(CommandDescription $description, \Closure $commandFactory)
29+
{
30+
$this->setName($description->getName())
31+
->setAliases($description->getAliases())
32+
->setHidden($description->isHidden())
33+
->setDescription($description->getDescription());
34+
$this->enabled = $description->isEnabled();
35+
36+
$this->command = $commandFactory;
37+
}
38+
39+
public function ignoreValidationErrors(): void
40+
{
41+
$this->getCommand()->ignoreValidationErrors();
42+
}
43+
44+
public function setApplication(Application $application = null): void
45+
{
46+
if ($this->command instanceof parent) {
47+
$this->command->setApplication($application);
48+
}
49+
50+
parent::setApplication($application);
51+
}
52+
53+
public function setHelperSet(HelperSet $helperSet): void
54+
{
55+
if ($this->command instanceof parent) {
56+
$this->command->setHelperSet($helperSet);
57+
}
58+
59+
parent::setHelperSet($helperSet);
60+
}
61+
62+
public function isEnabled(): bool
63+
{
64+
return $this->enabled;
65+
}
66+
67+
public function run(InputInterface $input, OutputInterface $output): int
68+
{
69+
return $this->getCommand()->run($input, $output);
70+
}
71+
72+
/**
73+
* @return $this
74+
*/
75+
public function setCode(callable $code): self
76+
{
77+
$this->getCommand()->setCode($code);
78+
79+
return $this;
80+
}
81+
82+
/**
83+
* @internal
84+
*/
85+
public function mergeApplicationDefinition(bool $mergeArgs = true): void
86+
{
87+
$this->getCommand()->mergeApplicationDefinition($mergeArgs);
88+
}
89+
90+
/**
91+
* @return $this
92+
*/
93+
public function setDefinition($definition): self
94+
{
95+
$this->getCommand()->setDefinition($definition);
96+
97+
return $this;
98+
}
99+
100+
public function getDefinition(): InputDefinition
101+
{
102+
return $this->getCommand()->getDefinition();
103+
}
104+
105+
public function getNativeDefinition(): InputDefinition
106+
{
107+
return $this->getCommand()->getNativeDefinition();
108+
}
109+
110+
/**
111+
* @return $this
112+
*/
113+
public function addArgument(string $name, int $mode = null, string $description = '', $default = null): self
114+
{
115+
$this->getCommand()->addArgument($name, $mode, $description, $default);
116+
117+
return $this;
118+
}
119+
120+
/**
121+
* @return $this
122+
*/
123+
public function addOption(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null): self
124+
{
125+
$this->getCommand()->addOption($name, $shortcut, $mode, $description, $default);
126+
127+
return $this;
128+
}
129+
130+
/**
131+
* @return $this
132+
*/
133+
public function setProcessTitle(string $title): self
134+
{
135+
$this->getCommand()->setProcessTitle($title);
136+
137+
return $this;
138+
}
139+
140+
/**
141+
* @return $this
142+
*/
143+
public function setHelp(string $help): self
144+
{
145+
$this->getCommand()->setHelp($help);
146+
147+
return $this;
148+
}
149+
150+
public function getHelp(): string
151+
{
152+
return $this->getCommand()->getHelp();
153+
}
154+
155+
public function getProcessedHelp(): string
156+
{
157+
return $this->getCommand()->getProcessedHelp();
158+
}
159+
160+
public function getSynopsis(bool $short = false): string
161+
{
162+
return $this->getCommand()->getSynopsis($short);
163+
}
164+
165+
/**
166+
* @return $this
167+
*/
168+
public function addUsage(string $usage): self
169+
{
170+
$this->getCommand()->addUsage($usage);
171+
172+
return $this;
173+
}
174+
175+
public function getUsages(): array
176+
{
177+
return $this->getCommand()->getUsages();
178+
}
179+
180+
/**
181+
* @return mixed
182+
*/
183+
public function getHelper(string $name)
184+
{
185+
return $this->getCommand()->getHelper($name);
186+
}
187+
188+
public function getCommand(): parent
189+
{
190+
if (!$this->command instanceof \Closure) {
191+
return $this->command;
192+
}
193+
194+
$command = $this->command = ($this->command)();
195+
$command->setApplication($this->getApplication());
196+
197+
if (null !== $this->getHelperSet()) {
198+
$command->setHelperSet($this->getHelperSet());
199+
}
200+
201+
$command->setName($this->getName())
202+
->setAliases($this->getAliases())
203+
->setHidden($this->isHidden())
204+
->setDescription($this->getDescription());
205+
206+
// Will throw if the command is not correctly initialized.
207+
$command->getDefinition();
208+
209+
return $command;
210+
}
211+
}

0 commit comments

Comments
 (0)