[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/fs/src/ -> lmbFsRecursiveIterator.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/fs/src/exception/lmbFsException.class.php');
  10  
  11  /**

  12   * class lmbFsRecursiveIterator.

  13   *

  14   * @package fs

  15   * @version $Id$

  16   */
  17  class lmbFsRecursiveIterator
  18  {
  19    protected $start_dir;
  20    protected $open_dirs = array();
  21    protected $look_ahead_buffer;
  22    protected $valid;
  23    protected $dir;
  24    protected $item;
  25  
  26    function __construct($dir)
  27    {
  28      $this->start_dir = $dir;
  29    }
  30  
  31    function rewind()
  32    {
  33      $this->open_dirs = array();
  34      $this->look_ahead_buffer = null;
  35      $this->valid = true;
  36      $this->_openDir($this->start_dir);
  37  
  38      $this->next();
  39    }
  40  
  41    protected function _openDir($dir)
  42    {
  43      $dir = rtrim($dir, '/') . '/';
  44      if(($h = @opendir($dir)) === false)
  45        throw new lmbFsException('can not open directory for scanning', array('dir' => $dir));
  46  
  47      $this->open_dirs[] = array('handle' => $h, 'dir' => $dir);
  48    }
  49  
  50    protected function _openDirIfNeccessary()
  51    {
  52      if(!$this->isDot() && $this->isDir())
  53        $this->_openDir($this->getPathName());
  54    }
  55  
  56    protected function _closeTerminalDirs()
  57    {
  58      if(!$this->_readItem() && $this->_hasOpenDirs())
  59      {
  60        $h = $this->_getLastOpenDirHandle();
  61        closedir($h);
  62        array_pop($this->open_dirs);
  63        $this->_closeTerminalDirs();
  64      }
  65    }
  66  
  67    protected function _hasOpenDirs()
  68    {
  69      return sizeof($this->open_dirs) > 0;
  70    }
  71  
  72    protected function _getLastOpenDirHandle()
  73    {
  74      $index = sizeof($this->open_dirs) - 1;
  75      if(isset($this->open_dirs[$index]))
  76        return $this->open_dirs[$index]['handle'];
  77    }
  78  
  79    protected function _getLastOpenDir()
  80    {
  81      $index = sizeof($this->open_dirs) - 1;
  82      if(isset($this->open_dirs[$index]))
  83        return $this->open_dirs[$index]['dir'];
  84    }
  85  
  86    protected function _readItem()
  87    {
  88      if(!$handle = $this->_getLastOpenDirHandle())
  89        return false;
  90  
  91      $this->dir = $this->_getLastOpenDir();
  92  
  93      $this->item = readdir($handle);
  94  
  95      if($this->item !== false)
  96        $this->_openDirIfNeccessary();
  97  
  98      return $this->item;
  99    }
 100  
 101    function current()
 102    {
 103      return $this;//???

 104    }
 105  
 106    function valid()
 107    {
 108      return $this->valid;
 109    }
 110  
 111    function next()
 112    {
 113      $this->valid = true;
 114  
 115      if($this->_readItem() === false)
 116      {
 117        $this->_closeTerminalDirs();
 118  
 119        if(!$this->_hasOpenDirs())
 120          $this->valid = false;
 121      }
 122    }
 123  
 124    function isDot()
 125    {
 126      return ($this->item == '.' || $this->item == '..');
 127    }
 128  
 129    function isDir()
 130    {
 131      return is_dir($this->getPathName());
 132    }
 133  
 134    function isFile()
 135    {
 136      return is_file($this->getPathName()) && !is_dir($this->getPathName());
 137    }
 138  
 139    function getPathName()
 140    {
 141      return $this->dir . $this->item;
 142    }
 143  
 144    function getCurrentDirectoryName()
 145    {
 146      return $this->dir;
 147    }
 148  
 149    function getCurrentFileName()
 150    {
 151      return $this->item;
 152    }
 153  }
 154  
 155  ?>


Generated: Sat Nov 22 03:48:54 2008 Cross-referenced by PHPXref 0.7