Files
nextcloud-server-mirror/lib/private/Console/OutputAdapter.php
Carl Schwan 31672f0d8b feat(search): Add command to display all the search options
Assisted-by: ClaudeCode:claude-opus-5
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
2026-09-23 17:02:09 +02:00

130 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
// SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace OC\Console;
use OCP\Console\IOutput;
use OCP\Console\OutputFormat;
use OCP\Console\Verbosity;
use Override;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class OutputAdapter implements IOutput {
public function __construct(
public readonly OutputInterface $output,
public readonly InputInterface $input,
public readonly SymfonyStyle $symfonyStyle,
public readonly CommandAdapter $commandAdapter,
) {
}
#[Override]
public function write(iterable|string $messages, bool $newline = false, Verbosity $verbosity = Verbosity::Normal): void {
$this->output->write($messages, $newline, $verbosity->value);
}
#[Override]
public function writeln(iterable|string $messages, Verbosity $verbosity = Verbosity::Normal): void {
$this->output->writeln($messages, $verbosity->value);
}
#[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);
}
#[Override]
public function writeTree(iterable $nodes, string $root = ''): void {
$outputFormat = OutputFormat::tryFrom((string)$this->input->getOption('output')) ?? OutputFormat::Plain;
if ($outputFormat === OutputFormat::Plain) {
$this->symfonyStyle->tree($nodes, $root);
return;
}
$data = $this->toArray($nodes);
$this->output->writeln(json_encode(
$root !== '' ? [$root => $data] : $data,
$outputFormat === OutputFormat::JsonPretty ? JSON_PRETTY_PRINT : 0,
));
}
/**
* @return array<string, mixed>
*/
private function toArray(iterable $nodes): array {
$result = [];
foreach ($nodes as $key => $value) {
$result[$key] = is_iterable($value) ? $this->toArray($value) : $value;
}
return $result;
}
#[Override]
public function setVerbosity(Verbosity $level): void {
$this->output->setVerbosity($level->value);
}
#[Override]
public function getVerbosity(): Verbosity {
return Verbosity::from($this->output->getVerbosity());
}
#[Override]
public function progressStart(int $max = 0): void {
$this->symfonyStyle->progressStart($max);
}
#[Override]
public function progressAdvance(int $step = 1): void {
$this->symfonyStyle->progressAdvance($step);
}
#[Override]
public function progressFinish(): void {
$this->symfonyStyle->progressFinish();
}
#[Override]
public function progressIterate(iterable $iterable, ?int $max = null): iterable {
return $this->symfonyStyle->progressIterate($iterable, $max);
}
}