Skip to content

Commit a2a975e

Browse files
committed
command refactor
1 parent c1a23dd commit a2a975e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+475
-598
lines changed

src/Command/CacheClearCommand.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
use Cake\Cache\Cache;
2020
use Cake\Cache\Engine\ApcuEngine;
2121
use Cake\Cache\Exception\InvalidArgumentException;
22-
use Cake\Console\Arguments;
23-
use Cake\Console\ConsoleIoInterface;
2422
use Cake\Console\ConsoleOptionParser;
2523

2624
/**
@@ -69,26 +67,24 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
6967
/**
7068
* Implement this method with your command's logic.
7169
*
72-
* @param \Cake\Console\Arguments $args The command arguments.
73-
* @param \Cake\Console\ConsoleIoInterface $io The console io
7470
* @return int|null The exit code or null for success
7571
*/
76-
public function execute(Arguments $args, ConsoleIoInterface $io): ?int
72+
public function execute(): ?int
7773
{
78-
$name = (string)$args->getArgument('engine');
74+
$name = (string)$this->args->getArgument('engine');
7975
try {
80-
$io->out("Clearing {$name}");
76+
$this->io->out("Clearing {$name}");
8177

8278
$engine = Cache::pool($name);
8379
Cache::clear($name);
8480
if ($engine instanceof ApcuEngine) {
85-
$io->warning("ApcuEngine detected: Cleared {$name} CLI cache successfully " .
81+
$this->io->warning("ApcuEngine detected: Cleared {$name} CLI cache successfully " .
8682
"but {$name} web cache must be cleared separately.");
8783
} else {
88-
$io->out("<success>Cleared {$name} cache</success>");
84+
$this->io->out("<success>Cleared {$name} cache</success>");
8985
}
9086
} catch (InvalidArgumentException $e) {
91-
$io->error($e->getMessage());
87+
$this->io->error($e->getMessage());
9288
$this->abort();
9389
}
9490

src/Command/CacheClearGroupCommand.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
use Cake\Cache\Cache;
2020
use Cake\Cache\Exception\InvalidArgumentException;
21-
use Cake\Console\Arguments;
22-
use Cake\Console\ConsoleIoInterface;
2321
use Cake\Console\ConsoleOptionParser;
2422

2523
/**
@@ -71,24 +69,22 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
7169
/**
7270
* Clears the cache group
7371
*
74-
* @param \Cake\Console\Arguments $args The command arguments.
75-
* @param \Cake\Console\ConsoleIoInterface $io The console io
7672
* @return int|null The exit code or null for success
7773
*/
78-
public function execute(Arguments $args, ConsoleIoInterface $io): ?int
74+
public function execute(): ?int
7975
{
80-
$group = (string)$args->getArgument('group');
76+
$group = (string)$this->args->getArgument('group');
8177
try {
8278
$groupConfigs = Cache::groupConfigs($group);
8379
} catch (InvalidArgumentException) {
84-
$io->error(sprintf('Cache group "%s" not found', $group));
80+
$this->io->error(sprintf('Cache group "%s" not found', $group));
8581

8682
return static::CODE_ERROR;
8783
}
8884

89-
$config = $args->getArgument('config');
85+
$config = $this->args->getArgument('config');
9086
if ($config !== null && Cache::getConfig($config) === null) {
91-
$io->error(sprintf('Cache config "%s" not found', $config));
87+
$this->io->error(sprintf('Cache config "%s" not found', $config));
9288

9389
return static::CODE_ERROR;
9490
}
@@ -99,14 +95,14 @@ public function execute(Arguments $args, ConsoleIoInterface $io): ?int
9995
}
10096

10197
if (!Cache::clearGroup($group, $groupConfig)) {
102-
$io->error(sprintf(
98+
$this->io->error(sprintf(
10399
'Error encountered clearing group "%s". Was unable to clear entries for "%s".',
104100
$group,
105101
$groupConfig,
106102
));
107103
$this->abort();
108104
} else {
109-
$io->success(sprintf('Cache "%s" was cleared.', $groupConfig));
105+
$this->io->success(sprintf('Cache "%s" was cleared.', $groupConfig));
110106
}
111107
}
112108

src/Command/CacheClearallCommand.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
namespace Cake\Command;
1818

1919
use Cake\Cache\Cache;
20-
use Cake\Console\Arguments;
21-
use Cake\Console\ConsoleIoInterface;
2220
use Cake\Console\ConsoleOptionParser;
2321

2422
/**
@@ -62,14 +60,12 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
6260
/**
6361
* Implement this method with your command's logic.
6462
*
65-
* @param \Cake\Console\Arguments $args The command arguments.
66-
* @param \Cake\Console\ConsoleIoInterface $io The console io
6763
* @return int|null The exit code or null for success
6864
*/
69-
public function execute(Arguments $args, ConsoleIoInterface $io): ?int
65+
public function execute(): ?int
7066
{
7167
foreach (Cache::configured() as $engine) {
72-
$this->executeCommand(CacheClearCommand::class, [$engine], $io);
68+
$this->executeCommand(CacheClearCommand::class, [$engine]);
7369
}
7470

7571
return static::CODE_SUCCESS;

src/Command/CacheListCommand.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
namespace Cake\Command;
1818

1919
use Cake\Cache\Cache;
20-
use Cake\Console\Arguments;
21-
use Cake\Console\ConsoleIoInterface;
2220
use Cake\Console\ConsoleOptionParser;
2321

2422
/**
@@ -60,14 +58,12 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
6058
/**
6159
* Get the list of cache prefixes
6260
*
63-
* @param \Cake\Console\Arguments $args The command arguments.
64-
* @param \Cake\Console\ConsoleIoInterface $io The console io
6561
* @return int|null The exit code or null for success
6662
*/
67-
public function execute(Arguments $args, ConsoleIoInterface $io): ?int
63+
public function execute(): ?int
6864
{
6965
foreach (Cache::configured() as $engine) {
70-
$io->out("- {$engine}");
66+
$this->io->out("- {$engine}");
7167
}
7268

7369
return static::CODE_SUCCESS;

src/Command/Command.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
*/
1717
namespace Cake\Command;
1818

19-
use Cake\Console\Arguments;
2019
use Cake\Console\BaseCommand;
21-
use Cake\Console\ConsoleIoInterface;
2220
use Cake\Log\LogTrait;
2321
use Cake\ORM\Locator\LocatorAwareTrait;
2422

@@ -37,11 +35,9 @@ class Command extends BaseCommand
3735
/**
3836
* Implement this method with your command's logic.
3937
*
40-
* @param \Cake\Console\Arguments $args The command arguments.
41-
* @param \Cake\Console\ConsoleIoInterface $io The console io
4238
* @return int|null|void The exit code or null for success
4339
*/
44-
public function execute(Arguments $args, ConsoleIoInterface $io)
40+
public function execute()
4541
{
4642
}
4743
}

src/Command/CompletionCommand.php

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616
*/
1717
namespace Cake\Command;
1818

19-
use Cake\Console\Arguments;
2019
use Cake\Console\BaseCommand;
2120
use Cake\Console\CommandCollection;
2221
use Cake\Console\CommandCollectionAwareInterface;
23-
use Cake\Console\ConsoleIoInterface;
2422
use Cake\Console\ConsoleOptionParser;
2523
use ReflectionClass;
2624

@@ -99,50 +97,44 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
9997
/**
10098
* Main function Prints out the list of commands.
10199
*
102-
* @param \Cake\Console\Arguments $args The command arguments.
103-
* @param \Cake\Console\ConsoleIoInterface $io The console io
104100
* @return int|null
105101
*/
106-
public function execute(Arguments $args, ConsoleIoInterface $io): ?int
102+
public function execute(): ?int
107103
{
108-
return match ($args->getArgument('mode')) {
109-
'commands' => $this->getCommands($args, $io),
110-
'subcommands' => $this->getSubcommands($args, $io),
111-
'options' => $this->getOptions($args, $io),
104+
return match ($this->args->getArgument('mode')) {
105+
'commands' => $this->getCommands(),
106+
'subcommands' => $this->getSubcommands(),
107+
'options' => $this->getOptions(),
112108
default => static::CODE_ERROR,
113109
};
114110
}
115111

116112
/**
117113
* Get the list of defined commands.
118114
*
119-
* @param \Cake\Console\Arguments $args The command arguments.
120-
* @param \Cake\Console\ConsoleIoInterface $io The console io
121115
* @return int
122116
*/
123-
protected function getCommands(Arguments $args, ConsoleIoInterface $io): int
117+
protected function getCommands(): int
124118
{
125119
$options = [];
126120
foreach ($this->commands as $key => $value) {
127121
$parts = explode(' ', $key);
128122
$options[] = $parts[0];
129123
}
130124
$options = array_unique($options);
131-
$io->out(implode(' ', $options));
125+
$this->io->out(implode(' ', $options));
132126

133127
return static::CODE_SUCCESS;
134128
}
135129

136130
/**
137131
* Get the list of defined sub-commands.
138132
*
139-
* @param \Cake\Console\Arguments $args The command arguments.
140-
* @param \Cake\Console\ConsoleIoInterface $io The console io
141133
* @return int
142134
*/
143-
protected function getSubcommands(Arguments $args, ConsoleIoInterface $io): int
135+
protected function getSubcommands(): int
144136
{
145-
$name = $args->getArgument('command');
137+
$name = $this->args->getArgument('command');
146138
if ($name === null || $name === '') {
147139
return static::CODE_SUCCESS;
148140
}
@@ -161,22 +153,20 @@ protected function getSubcommands(Arguments $args, ConsoleIoInterface $io): int
161153
}
162154
}
163155
$options = array_unique($options);
164-
$io->out(implode(' ', $options));
156+
$this->io->out(implode(' ', $options));
165157

166158
return static::CODE_SUCCESS;
167159
}
168160

169161
/**
170162
* Get the options for a command or subcommand
171163
*
172-
* @param \Cake\Console\Arguments $args The command arguments.
173-
* @param \Cake\Console\ConsoleIoInterface $io The console io
174164
* @return int|null
175165
*/
176-
protected function getOptions(Arguments $args, ConsoleIoInterface $io): ?int
166+
protected function getOptions(): ?int
177167
{
178-
$name = $args->getArgument('command');
179-
$subcommand = $args->getArgument('subcommand');
168+
$name = $this->args->getArgument('command');
169+
$subcommand = $this->args->getArgument('subcommand');
180170

181171
$options = [];
182172
foreach ($this->commands as $key => $value) {
@@ -191,28 +181,21 @@ protected function getOptions(Arguments $args, ConsoleIoInterface $io): ?int
191181
continue;
192182
}
193183

194-
// Handle class strings
195-
if (is_string($value)) {
196-
$reflection = new ReflectionClass($value);
197-
$value = $reflection->newInstance();
198-
assert($value instanceof BaseCommand);
199-
}
200-
201-
if (method_exists($value, 'getOptionParser')) {
202-
/** @var \Cake\Console\ConsoleOptionParser $parser */
203-
$parser = $value->getOptionParser();
184+
$reflection = new ReflectionClass($value);
185+
$value = $reflection->newInstance($this->io);
186+
assert($value instanceof BaseCommand);
204187

205-
foreach ($parser->options() as $name => $option) {
206-
$options[] = "--{$name}";
207-
$short = $option->short();
208-
if ($short) {
209-
$options[] = "-{$short}";
210-
}
188+
$parser = $value->getOptionParser();
189+
foreach ($parser->options() as $name => $option) {
190+
$options[] = "--{$name}";
191+
$short = $option->short();
192+
if ($short) {
193+
$options[] = "-{$short}";
211194
}
212195
}
213196
}
214197
$options = array_unique($options);
215-
$io->out(implode(' ', $options));
198+
$this->io->out(implode(' ', $options));
216199

217200
return static::CODE_SUCCESS;
218201
}

src/Command/CounterCacheCommand.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
*/
1717
namespace Cake\Command;
1818

19-
use Cake\Console\Arguments;
20-
use Cake\Console\ConsoleIoInterface;
2119
use Cake\Console\ConsoleOptionParser;
2220

2321
/**
@@ -47,35 +45,33 @@ public static function getDescription(): string
4745
* Updates the counter cache for the specified model and association based
4846
* on the model's counter cache behavior's configuration.
4947
*
50-
* @param \Cake\Console\Arguments $args The command arguments.
51-
* @param \Cake\Console\ConsoleIoInterface $io The console io
5248
* @return int The exit code or null for success
5349
*/
54-
public function execute(Arguments $args, ConsoleIoInterface $io): int
50+
public function execute(): int
5551
{
56-
$table = $this->fetchTable($args->getArgument('model'));
52+
$table = $this->fetchTable($this->args->getArgument('model'));
5753

5854
if (!$table->hasBehavior('CounterCache')) {
59-
$io->error('The specified model does not have the CounterCache behavior attached.');
55+
$this->io->error('The specified model does not have the CounterCache behavior attached.');
6056

6157
return static::CODE_ERROR;
6258
}
6359

6460
$methodArgs = [];
65-
if ($args->hasOption('assoc')) {
66-
$methodArgs['assocName'] = $args->getOption('assoc');
61+
if ($this->args->hasOption('assoc')) {
62+
$methodArgs['assocName'] = $this->args->getOption('assoc');
6763
}
68-
if ($args->hasOption('limit')) {
69-
$methodArgs['limit'] = (int)$args->getOption('limit');
64+
if ($this->args->hasOption('limit')) {
65+
$methodArgs['limit'] = (int)$this->args->getOption('limit');
7066
}
71-
if ($args->hasOption('page')) {
72-
$methodArgs['page'] = (int)$args->getOption('page');
67+
if ($this->args->hasOption('page')) {
68+
$methodArgs['page'] = (int)$this->args->getOption('page');
7369
}
7470

7571
/** @var \Cake\ORM\Table<array{CounterCache: \Cake\ORM\Behavior\CounterCacheBehavior}> $table */
7672
$table->getBehavior('CounterCache')->updateCounterCache(...$methodArgs);
7773

78-
$io->success('Counter cache updated successfully.');
74+
$this->io->success('Counter cache updated successfully.');
7975

8076
return static::CODE_SUCCESS;
8177
}

0 commit comments

Comments
 (0)