-
-
Save mablae/f4c67f5b5bdf1b8a7c41524148a16b7a 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