|
38 | 38 | use Seld\PharUtils\Timestamps; |
39 | 39 | use Symfony\Component\Finder\Finder; |
40 | 40 | use Symfony\Component\Finder\SplFileInfo; |
| 41 | +use Traversable; |
41 | 42 | use Webmozart\Assert\Assert; |
| 43 | +use function array_keys; |
42 | 44 | use function array_map; |
43 | 45 | use function array_unshift; |
44 | 46 | use function chdir; |
@@ -144,39 +146,18 @@ public function endBuffering(?callable $dumpAutoload): void |
144 | 146 | } |
145 | 147 |
|
146 | 148 | try { |
147 | | - $files = []; |
148 | | - $bufferedFiles = $this->bufferedFiles; |
149 | | - $this->bufferedFiles = []; |
150 | | - |
151 | | - foreach ($bufferedFiles as $file) { |
152 | | - $files[$file->getPath()] = $tmp.DIRECTORY_SEPARATOR.$file->getPath(); |
153 | | - |
154 | | - FS::dumpFile( |
155 | | - $file->getPath(), |
156 | | - $file->getContents(), |
157 | | - ); |
158 | | - } |
| 149 | + $bufferedFiles = $this->dumpBufferedFiles($tmp); |
159 | 150 |
|
160 | 151 | $completeDumpAutoload(); |
161 | 152 |
|
162 | | - $unknownFiles = fromPairs( |
163 | | - map( |
164 | | - static fn (SplFileInfo $fileInfo) => [ |
165 | | - $fileInfo->getRelativePathname(), |
166 | | - $fileInfo->getPathname(), |
167 | | - ], |
168 | | - Finder::create() |
169 | | - ->files() |
170 | | - ->in($tmp) |
171 | | - ->notPath(array_keys($files)), |
172 | | - ), |
| 153 | + $remainingFiles = self::collectRemainingFiles( |
| 154 | + $tmp, |
| 155 | + array_keys($bufferedFiles), |
173 | 156 | ); |
174 | 157 |
|
175 | | - $files = [...$files, ...$unknownFiles]; |
176 | | - |
177 | | - uksort($files, strcmp(...)); |
| 158 | + $iterator = self::createFileSortedIterator($bufferedFiles, $remainingFiles); |
178 | 159 |
|
179 | | - $this->phar->buildFromIterator(new ArrayIterator($files), $tmp); |
| 160 | + $this->phar->buildFromIterator($iterator, $tmp); |
180 | 161 | } finally { |
181 | 162 | FS::remove($tmp); |
182 | 163 | // Must happen _after_ the remove as the latter has higher priority. |
@@ -204,6 +185,66 @@ private function createDumpAutoload(?callable $dumpAutoload): Closure |
204 | 185 | ); |
205 | 186 | } |
206 | 187 |
|
| 188 | + /** |
| 189 | + * @return array<string, string> |
| 190 | + */ |
| 191 | + private function dumpBufferedFiles(string $tmp): array |
| 192 | + { |
| 193 | + $files = []; |
| 194 | + $bufferedFiles = $this->bufferedFiles; |
| 195 | + $this->bufferedFiles = []; |
| 196 | + |
| 197 | + foreach ($bufferedFiles as $file) { |
| 198 | + $files[$file->getPath()] = $tmp.DIRECTORY_SEPARATOR.$file->getPath(); |
| 199 | + |
| 200 | + FS::dumpFile( |
| 201 | + $file->getPath(), |
| 202 | + $file->getContents(), |
| 203 | + ); |
| 204 | + } |
| 205 | + |
| 206 | + return $files; |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * @param list<string> $bufferedFileNames |
| 211 | + * |
| 212 | + * @return iterable<string, string> |
| 213 | + */ |
| 214 | + private static function collectRemainingFiles( |
| 215 | + string $tmp, |
| 216 | + array $bufferedFileNames, |
| 217 | + ): iterable { |
| 218 | + return fromPairs( |
| 219 | + map( |
| 220 | + static fn (SplFileInfo $fileInfo) => [ |
| 221 | + $fileInfo->getRelativePathname(), |
| 222 | + $fileInfo->getPathname(), |
| 223 | + ], |
| 224 | + Finder::create() |
| 225 | + ->files() |
| 226 | + ->in($tmp) |
| 227 | + ->notPath($bufferedFileNames), |
| 228 | + ), |
| 229 | + ); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * @param array<string, string> $bufferedFiles |
| 234 | + * @param iterable<string, string> $remainingFiles |
| 235 | + * |
| 236 | + * @return Traversable<string, string> |
| 237 | + */ |
| 238 | + private static function createFileSortedIterator( |
| 239 | + array $bufferedFiles, |
| 240 | + iterable $remainingFiles |
| 241 | + ): Traversable { |
| 242 | + $files = [...$bufferedFiles, ...$remainingFiles]; |
| 243 | + uksort($files, strcmp(...)); |
| 244 | + |
| 245 | + return new ArrayIterator($files); |
| 246 | + } |
| 247 | + |
207 | 248 | /** |
208 | 249 | * @param non-empty-string $normalizedVendorDir Normalized path ("/" path separator and no trailing "/") to the Composer vendor directory |
209 | 250 | */ |
|
0 commit comments