8
8
9
9
namespace OCA \Files \Command \Object ;
10
10
11
- use OC \Core \Command \Base ;
12
11
use OCP \DB \QueryBuilder \IQueryBuilder ;
13
12
use OCP \Files \ObjectStore \IObjectStore ;
14
13
use OCP \IConfig ;
15
14
use OCP \IDBConnection ;
16
15
use OCP \Util ;
17
- use Symfony \Component \Console \Input \InputInterface ;
18
16
use Symfony \Component \Console \Output \OutputInterface ;
19
17
20
- class ObjectUtil extends Base {
18
+ class ObjectUtil {
21
19
public function __construct (
22
20
private IConfig $ config ,
23
21
private IDBConnection $ connection ,
@@ -95,36 +93,13 @@ public function objectExistsInDb(string $object): int|false {
95
93
return $ fileId ;
96
94
}
97
95
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 );
124
99
}
125
100
}
126
101
127
- private function formatObject (array $ object , bool $ humanOutput ): array {
102
+ public function formatObject (array $ object , bool $ humanOutput ): array {
128
103
$ row = array_merge ([
129
104
'urn ' => $ object ['urn ' ],
130
105
], ($ object ['metadata ' ] ?? []));
@@ -137,31 +112,4 @@ private function formatObject(array $object, bool $humanOutput): array {
137
112
}
138
113
return $ row ;
139
114
}
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
- }
167
115
}
0 commit comments