Created
June 22, 2017 21:23
-
-
Save DragonBe/63fb1a8d4d28e0b25a3234fe56093615 to your computer and use it in GitHub Desktop.
Sortable DirectoryIterator based on last modification time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SortableDirectoryIterator extends RecursiveDirectoryIterator | |
{ | |
/** | |
* \ArrayObject | |
*/ | |
private $dirArray; | |
public function __construct(string $path) | |
{ | |
parent::__construct($path); | |
$this->dirArray = new \ArrayObject(); | |
foreach($this as $item) { | |
$this->dirArray->append( $item ); | |
} | |
$this->dirArray->uasort( function ($fileObj1, $fileObj2) { | |
if ($fileObj1->getMTime() == $fileObj2->getMTime()) { | |
return 0; | |
} | |
return ($fileObj1->getMTime() < $fileObj2->getMTime()) ? -1 : 1; | |
} ); | |
} | |
public function getIterator() | |
{ | |
return $this->dirArray->getIterator(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$list
should now be iterable, contain all files recursively within$dir
sorted by their mtime…There's also the possibility to filter or sort the files by different parameters. More info in the README