Skip to content

Commit 73b4e60

Browse files
committed
migrate some built-in commands
1 parent 8b43341 commit 73b4e60

9 files changed

Lines changed: 491 additions & 401 deletions

File tree

system/Commands/Cache/ClearCache.php

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,75 +13,47 @@
1313

1414
namespace CodeIgniter\Commands\Cache;
1515

16-
use CodeIgniter\CLI\BaseCommand;
16+
use CodeIgniter\CLI\AbstractCommand;
17+
use CodeIgniter\CLI\Attributes\Command;
1718
use CodeIgniter\CLI\CLI;
19+
use CodeIgniter\CLI\Input\Argument;
1820
use Config\Cache;
1921

2022
/**
21-
* Clears current cache.
23+
* Clears the current system caches.
2224
*/
23-
class ClearCache extends BaseCommand
25+
#[Command(name: 'cache:clear', description: 'Clears the current system caches.', group: 'Cache')]
26+
class ClearCache extends AbstractCommand
2427
{
25-
/**
26-
* Command grouping.
27-
*
28-
* @var string
29-
*/
30-
protected $group = 'Cache';
31-
32-
/**
33-
* The Command's name
34-
*
35-
* @var string
36-
*/
37-
protected $name = 'cache:clear';
38-
39-
/**
40-
* the Command's short description
41-
*
42-
* @var string
43-
*/
44-
protected $description = 'Clears the current system caches.';
45-
46-
/**
47-
* the Command's usage
48-
*
49-
* @var string
50-
*/
51-
protected $usage = 'cache:clear [<driver>]';
52-
53-
/**
54-
* the Command's Arguments
55-
*
56-
* @var array<string, string>
57-
*/
58-
protected $arguments = [
59-
'driver' => 'The cache driver to use',
60-
];
28+
protected function configure(): void
29+
{
30+
$this->addArgument(new Argument(
31+
name: 'driver',
32+
description: 'The cache driver to use.',
33+
default: config(Cache::class)->handler,
34+
));
35+
}
6136

62-
/**
63-
* Clears the cache
64-
*/
65-
public function run(array $params)
37+
protected function execute(array $arguments, array $options): int
6638
{
67-
$config = config(Cache::class);
68-
$handler = $params[0] ?? $config->handler;
39+
$driver = $arguments['driver'];
40+
$config = config(Cache::class);
6941

70-
if (! array_key_exists($handler, $config->validHandlers)) {
71-
CLI::error(lang('Cache.invalidHandler', [$handler]));
42+
if (! array_key_exists($driver, $config->validHandlers)) {
43+
CLI::error(lang('Cache.invalidHandler', [$driver]));
7244

7345
return EXIT_ERROR;
7446
}
7547

76-
$config->handler = $handler;
48+
$config->handler = $driver;
7749

7850
if (! service('cache', $config)->clean()) {
79-
CLI::error('Error while clearing the cache.');
51+
CLI::error('Error occurred while clearing the cache.');
8052

8153
return EXIT_ERROR;
8254
}
8355

84-
CLI::write(CLI::color('Cache cleared.', 'green'));
56+
CLI::write('Cache cleared.', 'green');
8557

8658
return EXIT_SUCCESS;
8759
}

system/Commands/Help.php

Lines changed: 137 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,77 +13,157 @@
1313

1414
namespace CodeIgniter\Commands;
1515

16-
use CodeIgniter\CLI\BaseCommand;
16+
use CodeIgniter\CLI\AbstractCommand;
17+
use CodeIgniter\CLI\Attributes\Command;
18+
use CodeIgniter\CLI\CLI;
19+
use CodeIgniter\CLI\Input\Argument;
1720

1821
/**
19-
* CI Help command for the spark script.
20-
*
21-
* Lists the basic usage information for the spark script,
22-
* and provides a way to list help for other commands.
22+
* Displays the basic usage information for a given command.
2323
*/
24-
class Help extends BaseCommand
24+
#[Command(name: 'help', description: 'Displays basic usage information.', group: 'CodeIgniter')]
25+
class Help extends AbstractCommand
2526
{
26-
/**
27-
* The group the command is lumped under
28-
* when listing commands.
29-
*
30-
* @var string
31-
*/
32-
protected $group = 'CodeIgniter';
27+
protected function configure(): void
28+
{
29+
$this->addArgument(new Argument(
30+
name: 'command_name',
31+
description: 'The command name.',
32+
default: $this->getName(),
33+
));
34+
}
3335

34-
/**
35-
* The Command's name
36-
*
37-
* @var string
38-
*/
39-
protected $name = 'help';
36+
protected function execute(array $arguments, array $options): int
37+
{
38+
$command = $arguments['command_name'];
39+
assert(is_string($command));
4040

41-
/**
42-
* the Command's short description
43-
*
44-
* @var string
45-
*/
46-
protected $description = 'Displays basic usage information.';
41+
$commands = $this->getCommandRunner();
4742

48-
/**
49-
* the Command's usage
50-
*
51-
* @var string
52-
*/
53-
protected $usage = 'help [<command_name>]';
43+
if (array_key_exists($command, $commands->getCommands())) {
44+
$commands->getCommand($command)->showHelp();
5445

55-
/**
56-
* the Command's Arguments
57-
*
58-
* @var array<string, string>
59-
*/
60-
protected $arguments = [
61-
'command_name' => 'The command name [default: "help"]',
62-
];
46+
return EXIT_SUCCESS;
47+
}
6348

64-
/**
65-
* the Command's Options
66-
*
67-
* @var array<string, string>
68-
*/
69-
protected $options = [];
49+
if (! $commands->verifyCommand($command, legacy: false)) {
50+
return EXIT_ERROR;
51+
}
7052

71-
/**
72-
* Displays the help for spark commands.
73-
*/
74-
public function run(array $params)
53+
$this->describeHelp($commands->getCommand($command, legacy: false));
54+
55+
return EXIT_SUCCESS;
56+
}
57+
58+
private function describeHelp(AbstractCommand $command): void
7559
{
76-
$command = array_shift($params);
77-
$command ??= 'help';
78-
$commands = $this->commands->getCommands();
60+
CLI::write(lang('CLI.helpUsage'), 'yellow');
7961

80-
if (! $this->commands->verifyCommand($command, $commands)) {
81-
return EXIT_ERROR;
62+
foreach ($command->getUsages() as $usage) {
63+
CLI::write($this->addPadding($usage));
8264
}
8365

84-
$class = new $commands[$command]['class']($this->logger, $this->commands);
85-
$class->showHelp();
66+
if ($command->getDescription() !== '') {
67+
CLI::newLine();
68+
CLI::write(lang('CLI.helpDescription'), 'yellow');
69+
CLI::write($this->addPadding($command->getDescription()));
70+
}
8671

87-
return EXIT_SUCCESS;
72+
$maxPadding = $this->getMaxPadding($command);
73+
74+
if ($command->getArgumentsDefinition() !== []) {
75+
CLI::newLine();
76+
CLI::write(lang('CLI.helpArguments'), 'yellow');
77+
78+
foreach ($command->getArgumentsDefinition() as $argument => $definition) {
79+
$default = '';
80+
81+
if (! $definition->required) {
82+
$default = sprintf(' [default: %s]', $this->formatDefaultValue($definition->default));
83+
}
84+
85+
CLI::write(sprintf(
86+
'%s%s%s',
87+
CLI::color($this->addPadding($argument, 2, $maxPadding), 'green'),
88+
$definition->description,
89+
CLI::color($default, 'yellow'),
90+
));
91+
}
92+
}
93+
94+
if ($command->getOptionsDefinition() !== []) {
95+
CLI::newLine();
96+
CLI::write(lang('CLI.helpOptions'), 'yellow');
97+
98+
$hasShortcuts = $command->getShortcuts() !== [];
99+
100+
foreach ($command->getOptionsDefinition() as $option => $definition) {
101+
$value = '';
102+
103+
if ($definition->acceptsValue) {
104+
$value = sprintf('=%s', strtoupper($definition->valueLabel ?? ''));
105+
106+
if (! $definition->requiresValue) {
107+
$value = sprintf('[%s]', $value);
108+
}
109+
}
110+
111+
$optionString = sprintf(
112+
'%s--%s%s%s',
113+
$definition->shortcut !== null
114+
? sprintf('-%s, ', $definition->shortcut)
115+
: ($hasShortcuts ? ' ' : ''),
116+
$option,
117+
$value,
118+
$definition->negation !== null ? sprintf('|--%s', $definition->negation) : '',
119+
);
120+
121+
CLI::write(sprintf(
122+
'%s%s%s',
123+
CLI::color($this->addPadding($optionString, 2, $maxPadding), 'green'),
124+
$definition->description,
125+
$definition->isArray ? CLI::color(' (multiple values allowed)', 'yellow') : '',
126+
));
127+
}
128+
}
129+
}
130+
131+
private function addPadding(string $item, int $before = 2, ?int $max = null): string
132+
{
133+
return str_pad(str_repeat(' ', $before) . $item, $max ?? (strlen($item) + $before));
134+
}
135+
136+
private function getMaxPadding(AbstractCommand $command): int
137+
{
138+
$max = 0;
139+
140+
foreach (array_keys($command->getArgumentsDefinition()) as $argument) {
141+
$max = max($max, strlen($argument));
142+
}
143+
144+
$hasShortcuts = $command->getShortcuts() !== [];
145+
146+
foreach ($command->getOptionsDefinition() as $option => $definition) {
147+
$optionLength = strlen($option) + 2 // Account for the "--" prefix on options.
148+
+ ($definition->acceptsValue ? strlen($definition->valueLabel ?? '') + ($definition->requiresValue ? 1 : 3) : 0) // Account for the "=%s" value notation if the option accepts a value.
149+
+ ($hasShortcuts ? 4 : 0) // Account for the "-%s, " shortcut notation if shortcuts are present.
150+
+ ($definition->negation !== null ? 3 + strlen($definition->negation) : 0); // Account for the "|--no-%s" negation notation if a negation exists for this option.
151+
152+
$max = max($max, $optionLength);
153+
}
154+
155+
return $max + 4; // Account for the extra padding around the option/argument.
156+
}
157+
158+
/**
159+
* @param list<string>|string $value
160+
*/
161+
private function formatDefaultValue(array|string $value): string
162+
{
163+
if (is_array($value)) {
164+
return sprintf('[%s]', implode(', ', array_map($this->formatDefaultValue(...), $value)));
165+
}
166+
167+
return sprintf('"%s"', $value);
88168
}
89169
}

system/Commands/Housekeeping/ClearDebugbar.php

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,21 @@
1313

1414
namespace CodeIgniter\Commands\Housekeeping;
1515

16-
use CodeIgniter\CLI\BaseCommand;
16+
use CodeIgniter\CLI\AbstractCommand;
17+
use CodeIgniter\CLI\Attributes\Command;
1718
use CodeIgniter\CLI\CLI;
1819

1920
/**
20-
* ClearDebugbar Command
21+
* Clears all debugbar JSON files.
2122
*/
22-
class ClearDebugbar extends BaseCommand
23+
#[Command(name: 'debugbar:clear', description: 'Clears all debugbar JSON files.', group: 'Housekeeping')]
24+
class ClearDebugbar extends AbstractCommand
2325
{
24-
/**
25-
* The group the command is lumped under
26-
* when listing commands.
27-
*
28-
* @var string
29-
*/
30-
protected $group = 'Housekeeping';
31-
32-
/**
33-
* The Command's name
34-
*
35-
* @var string
36-
*/
37-
protected $name = 'debugbar:clear';
38-
39-
/**
40-
* The Command's usage
41-
*
42-
* @var string
43-
*/
44-
protected $usage = 'debugbar:clear';
45-
46-
/**
47-
* The Command's short description.
48-
*
49-
* @var string
50-
*/
51-
protected $description = 'Clears all debugbar JSON files.';
52-
53-
/**
54-
* Actually runs the command.
55-
*/
56-
public function run(array $params)
26+
protected function execute(array $arguments, array $options): int
5727
{
5828
helper('filesystem');
5929

60-
if (! delete_files(WRITEPATH . 'debugbar', false, true)) {
30+
if (! delete_files(WRITEPATH . 'debugbar', htdocs: true)) {
6131
CLI::error('Error deleting the debugbar JSON files.');
6232

6333
return EXIT_ERROR;

0 commit comments

Comments
 (0)