Skip to content
Merged
23 changes: 22 additions & 1 deletion src/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace KevinGH\Box;

use Amp\Parallel\Worker\TaskFailureThrowable;
use ArrayIterator;
use BadMethodCallException;
use Countable;
use DateTimeImmutable;
Expand All @@ -35,6 +36,7 @@
use RuntimeException;
use Seld\PharUtils\Timestamps;
use SplFileInfo;
use Symfony\Component\Finder\Finder;
use Webmozart\Assert\Assert;
use function array_map;
use function array_unshift;
Expand Down Expand Up @@ -137,7 +139,11 @@ public function endBuffering(?callable $dumpAutoload): void
}

try {
$files = [];

foreach ($this->bufferedFiles as $file) {
$files[$file->getPath()] = $tmp.DIRECTORY_SEPARATOR.$file->getPath();

FS::dumpFile(
$file->getPath(),
$file->getContents(),
Expand All @@ -154,7 +160,22 @@ public function endBuffering(?callable $dumpAutoload): void

chdir($cwd);

$this->phar->buildFromDirectory($tmp);
$unknownFiles = Finder::create()
->files()
->in($tmp)
->notPath(array_keys($files))
->sortByName();

$files = [...$files, ...$unknownFiles];

uasort($files, static function (SplFileInfo|string $a, SplFileInfo|string $b) {
$a = is_string($a) ? $a : $a->getPath();
$b = is_string($b) ? $b : $b->getPath();

return strcmp($a, $b);
});

$this->phar->buildFromIterator(new ArrayIterator($files), $tmp);
} finally {
FS::remove($tmp);
}
Expand Down
Loading