88
99namespace OCA \Files \Command \Object ;
1010
11- use OC \Core \Command \Base ;
1211use OCP \DB \QueryBuilder \IQueryBuilder ;
1312use OCP \Files \ObjectStore \IObjectStore ;
1413use OCP \IConfig ;
1514use OCP \IDBConnection ;
1615use OCP \Util ;
17- use Symfony \Component \Console \Input \InputInterface ;
1816use Symfony \Component \Console \Output \OutputInterface ;
1917
20- class ObjectUtil extends Base {
18+ class ObjectUtil {
2119 public function __construct (
2220 private IConfig $ config ,
2321 private IDBConnection $ connection ,
@@ -95,36 +93,13 @@ public function objectExistsInDb(string $object): int|false {
9593 return $ fileId ;
9694 }
9795
98- public function writeIteratorToOutput (InputInterface $ input , OutputInterface $ output , \Iterator $ objects , int $ chunkSize ): void {
99- $ outputType = $ input ->getOption ('output ' );
100- $ humanOutput = $ outputType === Base::OUTPUT_FORMAT_PLAIN ;
101-
102- if ($ humanOutput ) {
103- // we can't write tables in a streaming way, so we print them in chunks instead
104- foreach ($ this ->chunkIterator ($ objects , $ chunkSize ) as $ chunk ) {
105- $ this ->outputChunkHuman ($ input , $ output , $ chunk );
106- }
107- } else {
108- $ first = true ;
109-
110- $ output ->writeln ('[ ' );
111- foreach ($ objects as $ object ) {
112- if (!$ first ) {
113- $ output ->writeln (', ' );
114- }
115- $ row = $ this ->formatObject ($ object , false );
116- if ($ outputType === self ::OUTPUT_FORMAT_JSON_PRETTY ) {
117- $ output ->write (json_encode ($ row , JSON_PRETTY_PRINT ));
118- } else {
119- $ output ->write (json_encode ($ row ));
120- }
121- $ first = false ;
122- }
123- $ output ->writeln ("\n] " );
96+ public function formatObjects (\Iterator $ objects , bool $ humanOutput ): \Iterator {
97+ foreach ($ objects as $ object ) {
98+ yield $ this ->formatObject ($ object , $ humanOutput );
12499 }
125100 }
126101
127- private function formatObject (array $ object , bool $ humanOutput ): array {
102+ public function formatObject (array $ object , bool $ humanOutput ): array {
128103 $ row = array_merge ([
129104 'urn ' => $ object ['urn ' ],
130105 ], ($ object ['metadata ' ] ?? []));
@@ -137,31 +112,4 @@ private function formatObject(array $object, bool $humanOutput): array {
137112 }
138113 return $ row ;
139114 }
140-
141- private function outputChunkHuman (InputInterface $ input , OutputInterface $ output , iterable $ chunk ): void {
142- $ result = [];
143- foreach ($ chunk as $ object ) {
144- $ result [] = $ this ->formatObject ($ object , true );
145- }
146- $ this ->writeTableInOutputFormat ($ input , $ output , $ result );
147- }
148-
149- public function chunkIterator (\Iterator $ iterator , int $ count ): \Iterator {
150- $ chunk = [];
151-
152- for ($ i = 0 ; $ iterator ->valid (); $ i ++) {
153- $ chunk [] = $ iterator ->current ();
154- $ iterator ->next ();
155- if (count ($ chunk ) == $ count ) {
156- // Got a full chunk, yield and start a new one
157- yield $ chunk ;
158- $ chunk = [];
159- }
160- }
161-
162- if (count ($ chunk )) {
163- // Yield the last chunk even if incomplete
164- yield $ chunk ;
165- }
166- }
167115}
0 commit comments