Quellcode durchsuchen

[Finder] refactored SortableIterator

Fabien Potencier vor 14 Jahren
Ursprung
Commit
e1c52764ce

+ 21 - 12
src/Symfony/Component/Finder/Iterator/SortableIterator.php

@@ -16,27 +16,30 @@ namespace Symfony\Component\Finder\Iterator;
  *
  * @author Fabien Potencier <fabien@symfony.com>
  */
-class SortableIterator extends \ArrayIterator
+class SortableIterator implements \IteratorAggregate
 {
     const SORT_BY_NAME = 1;
     const SORT_BY_TYPE = 2;
 
+    private $iterator;
+    private $sort;
+
     /**
      * Constructor.
      *
-     * @param \Iterator        $iterator The Iterator to filter
-     * @param integer|\Closure $sort     The sort type (SORT_BY_NAME, SORT_BY_TYPE, or a \Closure instance)
+     * @param \Traversable     $iterator The Iterator to filter
+     * @param integer|callback $sort     The sort type (SORT_BY_NAME, SORT_BY_TYPE, or a PHP callback)
      */
-    public function __construct(\Iterator $iterator, $sort)
+    public function __construct(\Traversable $iterator, $sort)
     {
+        $this->iterator = $iterator;
+
         if (self::SORT_BY_NAME === $sort) {
-            $sort = function ($a, $b)
-            {
+            $this->sort = function ($a, $b) {
                 return strcmp($a->getRealpath(), $b->getRealpath());
             };
         } elseif (self::SORT_BY_TYPE === $sort) {
-            $sort = function ($a, $b)
-            {
+            $this->sort = function ($a, $b) {
                 if ($a->isDir() && $b->isFile()) {
                     return -1;
                 } elseif ($a->isFile() && $b->isDir()) {
@@ -45,12 +48,18 @@ class SortableIterator extends \ArrayIterator
 
                 return strcmp($a->getRealpath(), $b->getRealpath());
             };
-        } elseif (!$sort instanceof \Closure) {
-            throw new \InvalidArgumentException(sprintf('The SortableIterator takes a \Closure or a valid built-in sort algorithm as an argument (%s given).', $sort));
+        } elseif (is_callable($sort)) {
+            $this->sort = $sort;
+        } else {
+            throw new \InvalidArgumentException('The SortableIterator takes a PHP callback or a valid built-in sort algorithm as an argument.');
         }
+    }
 
-        parent::__construct(iterator_to_array($iterator));
+    public function getIterator()
+    {
+        $array = iterator_to_array($this->iterator, true);
+        uasort($array, $this->sort);
 
-        $this->uasort($sort);
+        return new \ArrayIterator($array);
     }
 }

+ 2 - 2
tests/Symfony/Tests/Component/Finder/Iterator/IteratorTestCase.php

@@ -15,7 +15,7 @@ require_once __DIR__.'/Iterator.php';
 
 abstract class IteratorTestCase extends \PHPUnit_Framework_TestCase
 {
-    protected function assertIterator($expected, \Iterator $iterator)
+    protected function assertIterator($expected, \Traversable $iterator)
     {
         $values = array_map(function (\SplFileInfo $fileinfo) { return $fileinfo->getPathname(); }, iterator_to_array($iterator));
 
@@ -25,7 +25,7 @@ abstract class IteratorTestCase extends \PHPUnit_Framework_TestCase
         $this->assertEquals($expected, array_values($values));
     }
 
-    protected function assertOrderedIterator($expected, \Iterator $iterator)
+    protected function assertOrderedIterator($expected, \Traversable $iterator)
     {
         $values = array_map(function (\SplFileInfo $fileinfo) { return $fileinfo->getPathname(); }, iterator_to_array($iterator));