Advertisement
rfv123

Q31470421-sorted-directory-class

Jul 19th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.24 KB | None | 0 0
  1. <?php // http://stackoverflow.com/questions/31470421/recursivedirectoryiterator-indent-subfolders
  2.  
  3.  
  4. class SortedDirectory implements IteratorAggregate, Countable
  5. {
  6.     /**
  7.      * Sorted directory list
  8.      *
  9.      * @var array
  10.      */
  11.     private $directories = array();
  12.  
  13.     /**
  14.      * sorted file list
  15.      *
  16.      * @var array
  17.      */
  18.     private $files = array();
  19.  
  20.     private $allFiles = null;
  21.  
  22.     /**
  23.      * Read the supplied directory into the two lists and sort them
  24.      *
  25.      * Ignore the '.' and '..' entries
  26.      *
  27.      * @param SplFileInfo
  28.      */
  29.     public function __construct(\SplFileInfo $directory)
  30.     {
  31.        foreach (new DirectoryIterator($directory->getRealPath()) as $entry) {
  32.            if ($entry->isDir()) {
  33.                $fname = $entry->getBasename();
  34.                if (substr($fname, 0, 1) === '.') {
  35.                    if ($fname === '.' || $fname === '..') {
  36.                        continue;
  37.                    }
  38.                 }
  39.                 $this->directories[] = clone $entry;
  40.            }
  41.            else {
  42.                $this->files[] = clone $entry;
  43.            }
  44.        }
  45.        $this->sortLists();
  46.     }
  47.  
  48.  
  49.     /**
  50.      * Iterator of the sorted directory list
  51.      *
  52.      * @return \ArrayIterator
  53.      */
  54.     public function getDirIterator()
  55.     {
  56.        return new ArrayIterator($this->directories);
  57.     }
  58.  
  59.     /**
  60.      * Iterator of the sorted files
  61.      *
  62.      * @return \ArrayIterator
  63.      */
  64.     public function getFileIterator()
  65.     {
  66.        return new ArrayIterator($this->files);
  67.     }
  68.  
  69.     /**
  70.      * Iterator of all the directory entries
  71.      *
  72.      * @return \ArrayIterator All the files with directories before files
  73.      */
  74.     public function getIterator()
  75.     {
  76.        return new ArrayIterator($this->allFiles);
  77.     }
  78.  
  79.     public function __get($property)
  80.     {
  81.        return $this->$property;
  82.     }
  83.  
  84.     /*
  85.      * Implement Iterator
  86.      *
  87.      */
  88.  
  89.     /**
  90.      * @return \SplFileInfo
  91.      */
  92.     public function current()
  93.     {
  94.         return current($this->allFiles);
  95.     }
  96.  
  97.     /**
  98.      * @return int
  99.      */
  100.     public function key()
  101.     {
  102.         return key($this->allFiles);
  103.     }
  104.  
  105.     public function next()
  106.     {
  107.         return next($this->allFiles);
  108.     }
  109.  
  110.     public function reset()
  111.     {
  112.         return reset($this->allFiles);
  113.     }
  114.  
  115.     public function rewind()
  116.     {
  117.         return reset($this->allFiles);
  118.     }
  119.  
  120.     public function valid()
  121.     {
  122.         return current($this->allFiles) !== false;
  123.     }
  124.  
  125.     /**
  126.      * @return integer
  127.      */
  128.     public function count()
  129.     {
  130.         return count($this->allFiles);
  131.     }
  132.  
  133.     /**
  134.      * Sort lists using 'natural' compare
  135.      */
  136.     private function sortLists()
  137.     {
  138.        if (count($this->directories) >= 2) {
  139.             usort($this->directories, array($this, 'cmpEntry'));
  140.        }
  141.        if (count($this->files) >= 2) {
  142.             usort($this->files,  array($this, 'cmpEntry'));
  143.        }
  144.  
  145.        $this->allFiles = array_merge($this->directories, $this->files);
  146.     }
  147.  
  148.     private function cmpEntry(SplFileInfo $entry1, SplFileInfo $entry2)
  149.     {
  150.        return strnatcasecmp($entry1->getFilename(), $entry2->getFilename());
  151.     }
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement