[Finder] Added sortBy options based on accessed, changed and modified times
This commit is contained in:
parent
0c7b2911bc
commit
dcf82d732b
@ -336,6 +336,68 @@ class Finder implements \IteratorAggregate
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts files and directories by the last accessed time.
|
||||
*
|
||||
* This is the time that the file was last accessed, read or written to.
|
||||
*
|
||||
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
||||
*
|
||||
* @return Finder The current Finder instance
|
||||
*
|
||||
* @see Symfony\Component\Finder\Iterator\SortableIterator
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function sortByAccessedTime()
|
||||
{
|
||||
$this->sort = Iterator\SortableIterator::SORT_BY_ACCESSED_TIME;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts files and directories by the last inode changed time.
|
||||
*
|
||||
* This is the time that the inode information was last modified (permissions, owner, group or other metadata).
|
||||
*
|
||||
* On Windows, since inode is not available, changed time is actually the file creation time.
|
||||
*
|
||||
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
||||
*
|
||||
* @return Finder The current Finder instance
|
||||
*
|
||||
* @see Symfony\Component\Finder\Iterator\SortableIterator
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function sortByChangedTime()
|
||||
{
|
||||
$this->sort = Iterator\SortableIterator::SORT_BY_CHANGED_TIME;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts files and directories by the last modified time.
|
||||
*
|
||||
* This is the last time the actual contents of the file were last modified.
|
||||
*
|
||||
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
||||
*
|
||||
* @return Finder The current Finder instance
|
||||
*
|
||||
* @see Symfony\Component\Finder\Iterator\SortableIterator
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function sortByModifiedTime()
|
||||
{
|
||||
$this->sort = Iterator\SortableIterator::SORT_BY_MODIFIED_TIME;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the iterator with an anonymous function.
|
||||
*
|
||||
|
@ -20,6 +20,9 @@ class SortableIterator implements \IteratorAggregate
|
||||
{
|
||||
const SORT_BY_NAME = 1;
|
||||
const SORT_BY_TYPE = 2;
|
||||
const SORT_BY_ACCESSED_TIME = 3;
|
||||
const SORT_BY_CHANGED_TIME = 4;
|
||||
const SORT_BY_MODIFIED_TIME = 5;
|
||||
|
||||
private $iterator;
|
||||
private $sort;
|
||||
@ -48,6 +51,18 @@ class SortableIterator implements \IteratorAggregate
|
||||
|
||||
return strcmp($a->getRealpath(), $b->getRealpath());
|
||||
};
|
||||
} elseif (self::SORT_BY_ACCESSED_TIME === $sort) {
|
||||
$this->sort = function ($a, $b) {
|
||||
return ($a->getATime() > $b->getATime());
|
||||
};
|
||||
} elseif (self::SORT_BY_CHANGED_TIME === $sort) {
|
||||
$this->sort = function ($a, $b) {
|
||||
return ($a->getCTime() > $b->getCTime());
|
||||
};
|
||||
} elseif (self::SORT_BY_MODIFIED_TIME === $sort) {
|
||||
$this->sort = function ($a, $b) {
|
||||
return ($a->getMTime() > $b->getMTime());
|
||||
};
|
||||
} elseif (is_callable($sort)) {
|
||||
$this->sort = $sort;
|
||||
} else {
|
||||
|
@ -162,6 +162,28 @@ class FinderTest extends Iterator\RealIteratorTestCase
|
||||
$this->assertIterator($this->toAbsolute(array('foo', 'toto', 'foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
|
||||
}
|
||||
|
||||
|
||||
public function testSortByAccessedTime()
|
||||
{
|
||||
$finder = new Finder();
|
||||
$this->assertSame($finder, $finder->sortByAccessedTime());
|
||||
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'toto', 'test.py', 'foo')), $finder->in(self::$tmpDir)->getIterator());
|
||||
}
|
||||
|
||||
public function testSortByChangedTime()
|
||||
{
|
||||
$finder = new Finder();
|
||||
$this->assertSame($finder, $finder->sortByChangedTime());
|
||||
$this->assertIterator($this->toAbsolute(array('toto', 'test.py', 'test.php', 'foo/bar.tmp', 'foo')), $finder->in(self::$tmpDir)->getIterator());
|
||||
}
|
||||
|
||||
public function testSortByModifiedTime()
|
||||
{
|
||||
$finder = new Finder();
|
||||
$this->assertSame($finder, $finder->sortByModifiedTime());
|
||||
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'toto', 'test.py', 'foo')), $finder->in(self::$tmpDir)->getIterator());
|
||||
}
|
||||
|
||||
public function testSort()
|
||||
{
|
||||
$finder = new Finder();
|
||||
|
Reference in New Issue
Block a user