Skip to content

Commit

Permalink
Add sorting to the file iterator to keep consistency in different sit…
Browse files Browse the repository at this point in the history
…uations
  • Loading branch information
donhardman committed Oct 30, 2023
1 parent 3b357ea commit db7e2b9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion APP_VERSION
@@ -1 +1 @@
1.0.11
1.0.13
35 changes: 35 additions & 0 deletions src/Lib/FileSortingIterator.php
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

/*
Copyright (c) 2023, Manticore Software LTD (https://manticoresearch.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 or any later
version. You should have received a copy of the GPL license along with this
program; if you did not, you can find it at http://www.gnu.org/
*/

namespace Manticoresearch\Backup\Lib;

use Iterator;
use SplFileInfo;
use SplHeap;

/** @phpstan-ignore-next-line */
final class FileSortingIterator extends SplHeap {
/**
* @param Iterator<SplFileInfo> $iterator [description]
* @return void
*/
public function __construct(Iterator $iterator) {
foreach ($iterator as $item) {
$this->insert($item);
}
}

public function compare(mixed $b, mixed $a): int {
/** @var SplFileInfo $a */
/** @var SplFileInfo $b */
return strcmp($a->getRealpath(), $b->getRealpath());
}
}
8 changes: 5 additions & 3 deletions src/Lib/FileStorage.php
Expand Up @@ -537,16 +537,18 @@ public function cleanUp(): void {
* @param string $dir
* The directory where we should look into
* @param int $flags
* @return \RecursiveIteratorIterator<\RecursiveDirectoryIterator>
* @return FileSortingIterator<\RecursiveDirectoryIterator>
*/
public static function getFileIterator(
string $dir,
int $flags = \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO
): \RecursiveIteratorIterator {
return new \RecursiveIteratorIterator(
): FileSortingIterator {
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($dir, $flags),
\RecursiveIteratorIterator::CHILD_FIRST
);

return new FileSortingIterator($iterator);
}

/**
Expand Down

0 comments on commit db7e2b9

Please sign in to comment.