Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/Command/CacheClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
use Cake\Cache\Cache;
use Cake\Cache\Engine\ApcuEngine;
use Cake\Cache\Exception\InvalidArgumentException;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIoInterface;
use Cake\Console\ConsoleOptionParser;

/**
Expand Down Expand Up @@ -69,26 +67,24 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
/**
* Implement this method with your command's logic.
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIoInterface $io The console io
* @return int|null The exit code or null for success
*/
public function execute(Arguments $args, ConsoleIoInterface $io): ?int
public function execute(): ?int
{
$name = (string)$args->getArgument('engine');
$name = (string)$this->args->getArgument('engine');
try {
$io->out("Clearing {$name}");
$this->io->out("Clearing {$name}");

$engine = Cache::pool($name);
Cache::clear($name);
if ($engine instanceof ApcuEngine) {
$io->warning("ApcuEngine detected: Cleared {$name} CLI cache successfully " .
$this->io->warning("ApcuEngine detected: Cleared {$name} CLI cache successfully " .
"but {$name} web cache must be cleared separately.");
} else {
$io->out("<success>Cleared {$name} cache</success>");
$this->io->out("<success>Cleared {$name} cache</success>");
}
} catch (InvalidArgumentException $e) {
$io->error($e->getMessage());
$this->io->error($e->getMessage());
$this->abort();
}

Expand Down
18 changes: 7 additions & 11 deletions src/Command/CacheClearGroupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

use Cake\Cache\Cache;
use Cake\Cache\Exception\InvalidArgumentException;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIoInterface;
use Cake\Console\ConsoleOptionParser;

/**
Expand Down Expand Up @@ -71,24 +69,22 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
/**
* Clears the cache group
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIoInterface $io The console io
* @return int|null The exit code or null for success
*/
public function execute(Arguments $args, ConsoleIoInterface $io): ?int
public function execute(): ?int
Comment on lines -78 to +74
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a change that we can make with rector? Converting all the command code could get tricky. 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try to make a custom rector for that, yes.
Removing the args here wouldn't be that hard, but making all the appearances inside that method (and sub methods) use $this->args and $this->io instead of those vars could be a bit tricky.

But then again this is also a pretty easy search-replace operation to be honest.

{
$group = (string)$args->getArgument('group');
$group = (string)$this->args->getArgument('group');
try {
$groupConfigs = Cache::groupConfigs($group);
} catch (InvalidArgumentException) {
$io->error(sprintf('Cache group "%s" not found', $group));
$this->io->error(sprintf('Cache group "%s" not found', $group));

return static::CODE_ERROR;
}

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

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

if (!Cache::clearGroup($group, $groupConfig)) {
$io->error(sprintf(
$this->io->error(sprintf(
'Error encountered clearing group "%s". Was unable to clear entries for "%s".',
$group,
$groupConfig,
));
$this->abort();
} else {
$io->success(sprintf('Cache "%s" was cleared.', $groupConfig));
$this->io->success(sprintf('Cache "%s" was cleared.', $groupConfig));
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/Command/CacheClearallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
namespace Cake\Command;

use Cake\Cache\Cache;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIoInterface;
use Cake\Console\ConsoleOptionParser;

/**
Expand Down Expand Up @@ -62,14 +60,12 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
/**
* Implement this method with your command's logic.
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIoInterface $io The console io
* @return int|null The exit code or null for success
*/
public function execute(Arguments $args, ConsoleIoInterface $io): ?int
public function execute(): ?int
{
foreach (Cache::configured() as $engine) {
$this->executeCommand(CacheClearCommand::class, [$engine], $io);
$this->executeCommand(CacheClearCommand::class, [$engine]);
}

return static::CODE_SUCCESS;
Expand Down
8 changes: 2 additions & 6 deletions src/Command/CacheListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
namespace Cake\Command;

use Cake\Cache\Cache;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIoInterface;
use Cake\Console\ConsoleOptionParser;

/**
Expand Down Expand Up @@ -60,14 +58,12 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
/**
* Get the list of cache prefixes
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIoInterface $io The console io
* @return int|null The exit code or null for success
*/
public function execute(Arguments $args, ConsoleIoInterface $io): ?int
public function execute(): ?int
{
foreach (Cache::configured() as $engine) {
$io->out("- {$engine}");
$this->io->out("- {$engine}");
}

return static::CODE_SUCCESS;
Expand Down
6 changes: 1 addition & 5 deletions src/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
*/
namespace Cake\Command;

use Cake\Console\Arguments;
use Cake\Console\BaseCommand;
use Cake\Console\ConsoleIoInterface;
use Cake\Log\LogTrait;
use Cake\ORM\Locator\LocatorAwareTrait;

Expand All @@ -37,11 +35,9 @@ class Command extends BaseCommand
/**
* Implement this method with your command's logic.
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIoInterface $io The console io
* @return int|null|void The exit code or null for success
*/
public function execute(Arguments $args, ConsoleIoInterface $io)
public function execute()
{
}
}
63 changes: 23 additions & 40 deletions src/Command/CompletionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
*/
namespace Cake\Command;

use Cake\Console\Arguments;
use Cake\Console\BaseCommand;
use Cake\Console\CommandCollection;
use Cake\Console\CommandCollectionAwareInterface;
use Cake\Console\ConsoleIoInterface;
use Cake\Console\ConsoleOptionParser;
use ReflectionClass;

Expand Down Expand Up @@ -99,50 +97,44 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
/**
* Main function Prints out the list of commands.
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIoInterface $io The console io
* @return int|null
*/
public function execute(Arguments $args, ConsoleIoInterface $io): ?int
public function execute(): ?int
{
return match ($args->getArgument('mode')) {
'commands' => $this->getCommands($args, $io),
'subcommands' => $this->getSubcommands($args, $io),
'options' => $this->getOptions($args, $io),
return match ($this->args->getArgument('mode')) {
'commands' => $this->getCommands(),
'subcommands' => $this->getSubcommands(),
'options' => $this->getOptions(),
default => static::CODE_ERROR,
};
}

/**
* Get the list of defined commands.
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIoInterface $io The console io
* @return int
*/
protected function getCommands(Arguments $args, ConsoleIoInterface $io): int
protected function getCommands(): int
{
$options = [];
foreach ($this->commands as $key => $value) {
$parts = explode(' ', $key);
$options[] = $parts[0];
}
$options = array_unique($options);
$io->out(implode(' ', $options));
$this->io->out(implode(' ', $options));

return static::CODE_SUCCESS;
}

/**
* Get the list of defined sub-commands.
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIoInterface $io The console io
* @return int
*/
protected function getSubcommands(Arguments $args, ConsoleIoInterface $io): int
protected function getSubcommands(): int
{
$name = $args->getArgument('command');
$name = $this->args->getArgument('command');
if ($name === null || $name === '') {
return static::CODE_SUCCESS;
}
Expand All @@ -161,22 +153,20 @@ protected function getSubcommands(Arguments $args, ConsoleIoInterface $io): int
}
}
$options = array_unique($options);
$io->out(implode(' ', $options));
$this->io->out(implode(' ', $options));

return static::CODE_SUCCESS;
}

/**
* Get the options for a command or subcommand
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIoInterface $io The console io
* @return int|null
*/
protected function getOptions(Arguments $args, ConsoleIoInterface $io): ?int
protected function getOptions(): ?int
{
$name = $args->getArgument('command');
$subcommand = $args->getArgument('subcommand');
$name = $this->args->getArgument('command');
$subcommand = $this->args->getArgument('subcommand');

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

// Handle class strings
if (is_string($value)) {
$reflection = new ReflectionClass($value);
$value = $reflection->newInstance();
assert($value instanceof BaseCommand);
}

if (method_exists($value, 'getOptionParser')) {
/** @var \Cake\Console\ConsoleOptionParser $parser */
$parser = $value->getOptionParser();
$reflection = new ReflectionClass($value);
$value = $reflection->newInstance();
assert($value instanceof BaseCommand);

foreach ($parser->options() as $name => $option) {
$options[] = "--{$name}";
$short = $option->short();
if ($short) {
$options[] = "-{$short}";
}
$parser = $value->getOptionParser();
foreach ($parser->options() as $name => $option) {
$options[] = "--{$name}";
$short = $option->short();
if ($short) {
$options[] = "-{$short}";
}
}
}
$options = array_unique($options);
$io->out(implode(' ', $options));
$this->io->out(implode(' ', $options));

return static::CODE_SUCCESS;
}
Expand Down
24 changes: 10 additions & 14 deletions src/Command/CounterCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
namespace Cake\Command;

use Cake\Console\Arguments;
use Cake\Console\ConsoleIoInterface;
use Cake\Console\ConsoleOptionParser;

/**
Expand Down Expand Up @@ -47,35 +45,33 @@ public static function getDescription(): string
* Updates the counter cache for the specified model and association based
* on the model's counter cache behavior's configuration.
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIoInterface $io The console io
* @return int The exit code or null for success
*/
public function execute(Arguments $args, ConsoleIoInterface $io): int
public function execute(): int
{
$table = $this->fetchTable($args->getArgument('model'));
$table = $this->fetchTable($this->args->getArgument('model'));

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

return static::CODE_ERROR;
}

$methodArgs = [];
if ($args->hasOption('assoc')) {
$methodArgs['assocName'] = $args->getOption('assoc');
if ($this->args->hasOption('assoc')) {
$methodArgs['assocName'] = $this->args->getOption('assoc');
}
if ($args->hasOption('limit')) {
$methodArgs['limit'] = (int)$args->getOption('limit');
if ($this->args->hasOption('limit')) {
$methodArgs['limit'] = (int)$this->args->getOption('limit');
}
if ($args->hasOption('page')) {
$methodArgs['page'] = (int)$args->getOption('page');
if ($this->args->hasOption('page')) {
$methodArgs['page'] = (int)$this->args->getOption('page');
}

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

$io->success('Counter cache updated successfully.');
$this->io->success('Counter cache updated successfully.');

return static::CODE_SUCCESS;
}
Expand Down
Loading
Loading