aboutsummaryrefslogtreecommitdiffstats
path: root/core/Command/Base.php
diff options
context:
space:
mode:
authorRobin Appelman <robin@icewind.nl>2025-03-28 15:23:13 +0100
committerRobin Appelman <robin@icewind.nl>2025-03-28 15:23:13 +0100
commita91f313a1c5796429cfb39b0e2e507e221532282 (patch)
tree42160752c77f18a908517aa10076e0eff01f61b7 /core/Command/Base.php
parenta157ba729ef804f50f9746998deba07137b0190b (diff)
downloadnextcloud-server-object-store-orphan.tar.gz
nextcloud-server-object-store-orphan.zip
feat: move streaming output helps to command base classobject-store-orphan
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'core/Command/Base.php')
-rw-r--r--core/Command/Base.php52
1 files changed, 52 insertions, 0 deletions
diff --git a/core/Command/Base.php b/core/Command/Base.php
index b915ae2ae4a..c9b6337b64a 100644
--- a/core/Command/Base.php
+++ b/core/Command/Base.php
@@ -88,6 +88,58 @@ class Base extends Command implements CompletionAwareInterface {
}
}
+ protected function writeStreamingTableInOutputFormat(InputInterface $input, OutputInterface $output, \Iterator $items, int $tableGroupSize): void {
+ switch ($input->getOption('output')) {
+ case self::OUTPUT_FORMAT_JSON:
+ case self::OUTPUT_FORMAT_JSON_PRETTY:
+ $this->writeStreamingJsonArray($input, $output, $items);
+ break;
+ default:
+ foreach ($this->chunkIterator($items, $tableGroupSize) as $chunk) {
+ $this->writeTableInOutputFormat($input, $output, $chunk);
+ }
+ break;
+ }
+ }
+
+ protected function writeStreamingJsonArray(InputInterface $input, OutputInterface $output, \Iterator $items): void {
+ $first = true;
+ $outputType = $input->getOption('output');
+
+ $output->writeln('[');
+ foreach ($items as $item) {
+ if (!$first) {
+ $output->writeln(',');
+ }
+ if ($outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
+ $output->write(json_encode($item, JSON_PRETTY_PRINT));
+ } else {
+ $output->write(json_encode($item));
+ }
+ $first = false;
+ }
+ $output->writeln("\n]");
+ }
+
+ public function chunkIterator(\Iterator $iterator, int $count): \Iterator {
+ $chunk = [];
+
+ for ($i = 0; $iterator->valid(); $i++) {
+ $chunk[] = $iterator->current();
+ $iterator->next();
+ if (count($chunk) == $count) {
+ // Got a full chunk, yield and start a new one
+ yield $chunk;
+ $chunk = [];
+ }
+ }
+
+ if (count($chunk)) {
+ // Yield the last chunk even if incomplete
+ yield $chunk;
+ }
+ }
+
/**
* @param mixed $item