From cca2ebeaed6db1d24734af00e973b2ccb9bdf806 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Wed, 5 Aug 2026 19:02:11 +0200 Subject: [PATCH] feat(api): Extend a bit IInput and IOutput Make sure it is somewhat feature complete with the API used in Base. Signed-off-by: Carl Schwan --- lib/private/Console/CommandAdapter.php | 20 ++++++++- lib/private/Console/InputAdapter.php | 20 +++++++++ lib/private/Console/OutputAdapter.php | 38 ++++++++++++++++ lib/public/Console/Attribute/Argument.php | 1 + lib/public/Console/Attribute/AsCommand.php | 1 + lib/public/Console/Attribute/Option.php | 1 + lib/public/Console/IInput.php | 37 +++++++++++++++- lib/public/Console/IOutput.php | 50 ++++++++++++++++++++++ 8 files changed, 165 insertions(+), 3 deletions(-) diff --git a/lib/private/Console/CommandAdapter.php b/lib/private/Console/CommandAdapter.php index 2cff7aa3328..cbda962d567 100644 --- a/lib/private/Console/CommandAdapter.php +++ b/lib/private/Console/CommandAdapter.php @@ -186,7 +186,7 @@ class CommandAdapter extends Base { $type = $parameter->getType(); if ($type instanceof \ReflectionNamedType && $type->getName() === IOutput::class) { - $parameters[] = new OutputAdapter($output); + $parameters[] = new OutputAdapter($output, $input, $this); continue; } @@ -213,4 +213,22 @@ class CommandAdapter extends Base { return $result instanceof ExitCode ? $result->value : $result; } + + #[Override] + public function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, iterable $items, string $prefix = ' - '): void { + // To make it public + parent::writeArrayInOutputFormat($input, $output, $items, $prefix); + } + + #[Override] + public function writeTableInOutputFormat(InputInterface $input, OutputInterface $output, array $items): void { + // To make it public + parent::writeTableInOutputFormat($input, $output, $items); + } + + #[Override] + public function writeStreamingTableInOutputFormat(InputInterface $input, OutputInterface $output, \Iterator $items, int $tableGroupSize): void { + // To make it public + parent::writeStreamingTableInOutputFormat($input, $output, $items, $tableGroupSize); + } } diff --git a/lib/private/Console/InputAdapter.php b/lib/private/Console/InputAdapter.php index fea53c7ae93..9dc8063ff92 100644 --- a/lib/private/Console/InputAdapter.php +++ b/lib/private/Console/InputAdapter.php @@ -26,4 +26,24 @@ class InputAdapter implements IInput { public function getArgument(string $name): string|bool|int|float|array|null { return $this->input->getArgument($name); } + + #[Override] + public function hasArgument(string $name): bool { + return $this->input->hasArgument($name); + } + + #[Override] + public function getOptions(): array { + return $this->input->getOptions(); + } + + #[Override] + public function getOption(string $name): string|bool|int|float|array|null { + return $this->input->getOption($name); + } + + #[Override] + public function hasOption(string $name): bool { + return $this->input->hasOption($name); + } } diff --git a/lib/private/Console/OutputAdapter.php b/lib/private/Console/OutputAdapter.php index 9fb396350b2..0fe58ee2e5d 100644 --- a/lib/private/Console/OutputAdapter.php +++ b/lib/private/Console/OutputAdapter.php @@ -9,11 +9,14 @@ namespace OC\Console; use OCP\Console\IOutput; use Override; +use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class OutputAdapter implements IOutput { public function __construct( public readonly OutputInterface $output, + public readonly InputInterface $input, + public readonly CommandAdapter $commandAdapter, ) { } @@ -26,4 +29,39 @@ class OutputAdapter implements IOutput { public function writeln(iterable|string $messages): void { $this->output->writeln($messages); } + + #[Override] + public function isQuiet(): bool { + return $this->output->isQuiet(); + } + + #[Override] + public function isVerbose(): bool { + return $this->output->isVerbose(); + } + + #[Override] + public function isVeryVerbose(): bool { + return $this->output->isVeryVerbose(); + } + + #[Override] + public function isDebug(): bool { + return $this->output->isDebug(); + } + + #[Override] + public function writeArrayInOutputFormat(iterable $items, string $prefix = ' - '): void { + $this->commandAdapter->writeArrayInOutputFormat($this->input, $this->output, $items, $prefix); + } + + #[Override] + public function writeTableInOutputFormat(array $items): void { + $this->commandAdapter->writeTableInOutputFormat($this->input, $this->output, $items); + } + + #[Override] + public function writeStreamingTableInOutputFormat(\Iterator $items, int $tableGroupSize): void { + $this->commandAdapter->writeStreamingTableInOutputFormat($this->input, $this->output, $items, $tableGroupSize); + } } diff --git a/lib/public/Console/Attribute/Argument.php b/lib/public/Console/Attribute/Argument.php index 18565e14ee6..f7f590d5f59 100644 --- a/lib/public/Console/Attribute/Argument.php +++ b/lib/public/Console/Attribute/Argument.php @@ -63,6 +63,7 @@ final class Argument { * * @param string $description The description of the argument, displayed with the help page * @param string $name The name of the argument + * @since 35.0.0 */ public function __construct( public string $description = '', diff --git a/lib/public/Console/Attribute/AsCommand.php b/lib/public/Console/Attribute/AsCommand.php index 7075069e0ba..7a1481d5c3c 100644 --- a/lib/public/Console/Attribute/AsCommand.php +++ b/lib/public/Console/Attribute/AsCommand.php @@ -64,6 +64,7 @@ final class AsCommand { * @param bool $hidden If true, the command won't be shown when listing all the available commands, but it can still be run as any other command * @param string|null $help The help content of the command, displayed with the help page * @param string[] $usages The list of usage examples, displayed with the help page + * @since 35.0.0 */ public function __construct( public string $name, diff --git a/lib/public/Console/Attribute/Option.php b/lib/public/Console/Attribute/Option.php index f7b98df3047..ce298e4ae5f 100644 --- a/lib/public/Console/Attribute/Option.php +++ b/lib/public/Console/Attribute/Option.php @@ -64,6 +64,7 @@ final class Option { * @param string $description The description of the option, displayed with the help page * @param string $name The name of the option * @param array|string|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts + * @since 35.0.0 */ public function __construct( public string $description = '', diff --git a/lib/public/Console/IInput.php b/lib/public/Console/IInput.php index fcc6ec65a30..cdb4349a3fb 100644 --- a/lib/public/Console/IInput.php +++ b/lib/public/Console/IInput.php @@ -10,21 +10,54 @@ namespace OCP\Console; use InvalidArgumentException; use OCP\AppFramework\Attribute\Consumable; +/** + * Interface that lets you retrieve the input from a command. + * + * @since 35.0.0 + */ #[Consumable(since: '35.0.0')] interface IInput { /** * Returns all the given arguments merged with the default values. * * @return array + * @since 35.0.0 */ public function getArguments(): array; /** * Returns the argument value for a given argument name. * - * @return string|bool|int|float|array|null - * * @throws InvalidArgumentException When argument given doesn't exist + * @since 35.0.0 */ public function getArgument(string $name): string|bool|int|float|array|null; + + /** + * Returns true if an argument exists by name or position. + * @since 35.0.0 + */ + public function hasArgument(string $name): bool; + + /** + * Returns all the given options merged with the default values. + * + * @return array + * @since 35.0.0 + */ + public function getOptions(): array; + + /** + * Returns the option value for a given option name. + * + * @throws InvalidArgumentException When option given doesn't exist + * @since 35.0.0 + */ + public function getOption(string $name): string|bool|int|float|array|null; + + /** + * Returns true if an option exists by name. + * @since 35.0.0 + */ + public function hasOption(string $name): bool; } diff --git a/lib/public/Console/IOutput.php b/lib/public/Console/IOutput.php index 1a63f59bd37..489394ea7f8 100644 --- a/lib/public/Console/IOutput.php +++ b/lib/public/Console/IOutput.php @@ -10,6 +10,8 @@ namespace OCP\Console; use OCP\AppFramework\Attribute\Consumable; /** + * Interface that lets you output content from a command. + * * @since 35.0.0 */ #[Consumable(since: '35.0.0')] @@ -27,4 +29,52 @@ interface IOutput { * @since 35.0.0 */ public function writeln(string|iterable $messages): void; + + /** + * Returns whether verbosity is quiet (-q). + * @since 35.0.0 + */ + public function isQuiet() : bool; + + /** + * Returns whether verbosity is verbose (-v). + * @since 35.0.0 + */ + public function isVerbose() : bool; + + /** + * Returns whether verbosity is very verbose (-vv). + * @since 35.0.0 + */ + public function isVeryVerbose() : bool; + + /** + * Returns whether verbosity is debug (-vvv). + * @since 35.0.0 + */ + public function isDebug() : bool; + + /** + * Write a list of items in the format specified with --output + * + * @param list $items + * @since 35.0.0 + */ + public function writeArrayInOutputFormat(iterable $items, string $prefix = ' - '): void; + + /** + * Write a multidimensional array of items in the format specified with --output + * + * @param list> $items + * @since 35.0.0 + */ + public function writeTableInOutputFormat(array $items): void; + + /** + * Write a multidimensional iterator of items in the format specified with --output + * + * @param \Iterator> $items + * @since 35.0.0 + */ + public function writeStreamingTableInOutputFormat(\Iterator $items, int $tableGroupSize): void; }