[ Index ]
 

Code source de Flux CMS 1.5

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/inc/ -> DirectoryIterator.php (source)

   1  <?php
   2  // +----------------------------------------------------------------------+
   3  // | DirectoryIterator.php                                                |                                                                    |
   4  // +----------------------------------------------------------------------+
   5  // | Copyright (c) 2005 Mediagonal Ag                                     |
   6  // +----------------------------------------------------------------------+
   7  // | This program is free software; you can redistribute it and/or        |
   8  // | modify it under the terms of the GNU General Public License (GPL)    |
   9  // | as published by the Free Software Foundation; either version 2       |
  10  // | of the License, or (at your option) any later version.               |
  11  // | The GPL can be found at http://www.gnu.org/licenses/gpl.html         |
  12  // +----------------------------------------------------------------------+
  13  // | Author: Hannes Gassert <hannes.gassert@mediagonal.ch>                |
  14  // +----------------------------------------------------------------------+
  15  
  16  
  17  /**
  18  * A lousy and incomplete port of DirectoryIterator of ext/spl.
  19  * 
  20  * @author Hannes Gassert <hannes@mediagonal.ch>
  21  * @example $d = new DirectoryIterator('.'); foreach ($d as $entry) { echo $entry , "\n<br>"; }
  22  */
  23  class DirectoryIterator {
  24  
  25      /**
  26      * @var array
  27      */    
  28      protected $_children = array();
  29  
  30      /**
  31      * @var string
  32      */
  33      protected $_path;
  34  
  35      /**
  36      * @var int
  37      */
  38      protected $_curIdx = 0;
  39  
  40      /**
  41      * @var boolean
  42      */
  43      protected $_isDot = false;
  44  
  45      /**
  46      * @param string Path to directory
  47      * @param boolean isDot - for internal use only
  48      */
  49      public function __construct($path, $isDot = false) {
  50  
  51          $this->_path = realpath($path);
  52          
  53          if (is_dir($this->_path)) {
  54              $dirArray = scandir($this->_path);
  55          } else {
  56              $dirArray = null;
  57          }
  58  
  59          if (is_array($dirArray)) {
  60  
  61              foreach ($dirArray as $i => $entry) {
  62  
  63                  $newPath = $this->_path .'/'. $entry;
  64  
  65                  $newPathPropName = 'dir_' . md5($newPath);
  66  
  67                  // "dot files" -> don't go into recursion.
  68                  if($entry == '.' || $entry == '..' || realpath($newPath) == $this->_path){
  69                      $newNode = clone $this;
  70                      $newNode->_isDot = true;
  71                      $newNode->_path = realpath($newPath);
  72                  }
  73                  else{
  74                      $thisClass = get_class($this); //necesseray in order to work with classes inheriting from DirectoryIterator..
  75                      $newNode = new $thisClass($newPath);
  76                  }
  77  
  78                  $this->_children[] = $newPathPropName;
  79  
  80                  // enable foreach access.. rather hackish.
  81                  $this->{$newPathPropName} = $newNode;
  82  
  83              }
  84          }
  85      }
  86  
  87      /**
  88      * @return string
  89      */
  90      public function __toString() {
  91          return $this->getFileName();
  92      }
  93  
  94      /**
  95      * @return string
  96      */
  97      public function getFileName() {
  98          return basename($this->_path);
  99      }
 100  
 101      /**
 102      * @return string
 103      */
 104      public function getPathName() {
 105          return $this->_path;
 106      }
 107  
 108      /**
 109      * @return boolean
 110      */
 111      public function isDir() {
 112          return is_dir($this->_path);
 113      }
 114  
 115      /**
 116      * @return boolean
 117      */
 118      public function isFile() {
 119          return is_file($this->_path);
 120      }
 121  
 122      /**
 123      * @return boolean
 124      */
 125      public function isLink() {
 126          return is_link($this->_path);
 127      }
 128  
 129      /**
 130      * @return boolean
 131      */
 132      public function isExecutable() {
 133          return is_executable($this->_path);
 134      }
 135  
 136      /**
 137      * @return boolean
 138      */
 139      public function isWriteable() {
 140          return is_writeable($this->_path);
 141      }
 142  
 143      /**
 144      * @return string
 145      */
 146      public function getPerms() {
 147          return fileperms($this->_path);
 148      }
 149  
 150      /**
 151      * @return int
 152      */
 153      public function getAtime() {
 154          return fileatime($this->_path);
 155      }
 156  
 157      /**
 158      * @return int
 159      */
 160      public function getCtime() {
 161          return filectime($this->_path);
 162      }
 163  
 164      /**
 165      * @return int
 166      */
 167      public function getMtime() {
 168          return filemtime($this->_path);
 169      }
 170  
 171      /**
 172      * @return int
 173      */
 174      public function getInode() {
 175          return fileinode($this->_path);
 176      }
 177  
 178      /**
 179      * @return int
 180      */
 181      public function getSize() {
 182          return filesize($this->_path);
 183      }
 184  
 185      /**
 186      * @return string
 187      */    
 188      public function getOwner() {
 189          return fileowner($this->_path);
 190      }
 191  
 192  
 193      /**
 194      * @return string
 195      */
 196      public function getGroup() {
 197          return filegroup($this->_path);
 198      }
 199  
 200      /**
 201      * @return boolean
 202      */
 203      public function isDot(){
 204          return $this->_isDot;
 205      }
 206  
 207      /**
 208      * @return boolean
 209      */
 210      public function isReadable(){
 211          return is_readable($this->_path);
 212      }
 213  
 214      /**
 215      * @return int
 216      */
 217      public function key() {
 218          return $this->_curIdx;
 219      }
 220  
 221      /**
 222      * @return object DirectoryIterator
 223      */
 224      public function getChildren() {
 225          if (empty($this->_children)) {
 226              return false;
 227          }
 228          return $this; //ähh.. correct?
 229      }
 230  
 231      /**
 232      * @return boolean
 233      */
 234      public function valid() {
 235          return isset($this->_children[$this->_curIdx + 1]);
 236      }
 237  
 238      public function rewind() {
 239          $this->_curIdx = 0;
 240      }
 241  
 242      public function next() {
 243          $this->_curIdx++;
 244      }
 245  
 246      /**
 247      * @return object DirectoryIterator
 248      */
 249      public function current() {
 250          if (isset($this->_children[$this->_curIdx])) {
 251              return $this->{$this->_children[$this->_curIdx]};
 252          }
 253          else {
 254              return false;
 255          }
 256      }
 257  
 258  }
 259  
 260  /*
 261  $d = new DirectoryIterator('.'); foreach ($d as $entry) { echo $entry , "\n<br>"; }
 262  */
 263  
 264  ?>


Généré le : Wed Nov 21 13:08:55 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics