Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bc816da

Browse filesBrowse files
committed
feature #27967 [Finder] Added a way to inverse a previous sorting (lyrixx)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Finder] Added a way to inverse a previous sorting | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | --- Sometimes, it's useful to inverse the previous sorting. For exemple when you want to display the most recent uploaded files Commits ------- 3cd0dca [Finder] Added a way to inverse a previous sorting
2 parents 331a24e + 3cd0dca commit bc816da
Copy full SHA for bc816da

File tree

Expand file treeCollapse file tree

5 files changed

+107
-0
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+107
-0
lines changed

‎src/Symfony/Component/Finder/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* added $useNaturalSort option to Finder::sortByName() method
88
* the `Finder::sortByName()` method will have a new `$useNaturalSort`
99
argument in version 5.0, not defining it is deprecated
10+
* added `Finder::reverseSorting` to reverse the sorting
1011

1112
4.0.0
1213
-----

‎src/Symfony/Component/Finder/Finder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/Finder.php
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Finder implements \IteratorAggregate, \Countable
4848
private $depths = array();
4949
private $sizes = array();
5050
private $followLinks = false;
51+
private $reverseSorting = false;
5152
private $sort = false;
5253
private $ignore = 0;
5354
private $dirs = array();
@@ -463,6 +464,18 @@ public function sortByAccessedTime()
463464
return $this;
464465
}
465466

467+
/**
468+
* Reverses the sorting.
469+
*
470+
* @return $this
471+
*/
472+
public function reverseSorting()
473+
{
474+
$this->reverseSorting = true;
475+
476+
return $this;
477+
}
478+
466479
/**
467480
* Sorts files and directories by the last inode changed time.
468481
*
@@ -742,6 +755,11 @@ private function searchInDirectory(string $dir): \Iterator
742755
$iterator = $iteratorAggregate->getIterator();
743756
}
744757

758+
if ($this->reverseSorting) {
759+
$iteratorAggregate = new Iterator\ReverseSortingIterator($iterator);
760+
$iterator = $iteratorAggregate->getIterator();
761+
}
762+
745763
return $iterator;
746764
}
747765

+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Finder\Iterator;
13+
14+
/**
15+
* Reverse the order of a previous iterator.
16+
*
17+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
18+
*/
19+
class ReverseSortingIterator implements \IteratorAggregate
20+
{
21+
private $iterator;
22+
23+
public function __construct(\Traversable $iterator)
24+
{
25+
$this->iterator = $iterator;
26+
}
27+
28+
public function getIterator()
29+
{
30+
return new \ArrayIterator(array_reverse(iterator_to_array($this->iterator, true)));
31+
}
32+
}

‎src/Symfony/Component/Finder/Tests/FinderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/Tests/FinderTest.php
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,30 @@ public function testSortByModifiedTime()
611611
)), $finder->in(self::$tmpDir)->getIterator());
612612
}
613613

614+
public function testReverseSorting()
615+
{
616+
$finder = $this->buildFinder();
617+
$this->assertSame($finder, $finder->sortByName());
618+
$this->assertSame($finder, $finder->reverseSorting());
619+
$this->assertOrderedIteratorInForeach($this->toAbsolute(array(
620+
'toto',
621+
'test.py',
622+
'test.php',
623+
'qux_2_0.php',
624+
'qux_12_0.php',
625+
'qux_10_2.php',
626+
'qux_1002_0.php',
627+
'qux_1000_1.php',
628+
'qux_0_1.php',
629+
'qux/baz_1_2.py',
630+
'qux/baz_100_1.py',
631+
'qux',
632+
'foo/bar.tmp',
633+
'foo bar',
634+
'foo',
635+
)), $finder->in(self::$tmpDir)->getIterator());
636+
}
637+
614638
public function testSortByNameNatural()
615639
{
616640
$finder = $this->buildFinder();
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Finder\Tests\Iterator;
13+
14+
use Symfony\Component\Finder\Iterator\ReverseSortingIterator;
15+
16+
class ReverseSortingIteratorTest extends IteratorTestCase
17+
{
18+
public function test()
19+
{
20+
$iterator = new ReverseSortingIterator(new MockFileListIterator(array(
21+
'a.txt',
22+
'b.yaml',
23+
'c.php',
24+
)));
25+
26+
$result = iterator_to_array($iterator);
27+
$this->assertCount(3, $iterator);
28+
$this->assertSame('c.php', $result[0]->getFilename());
29+
$this->assertSame('b.yaml', $result[1]->getFilename());
30+
$this->assertSame('a.txt', $result[2]->getFilename());
31+
}
32+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.