00001 <?php 00024 class FrameworkDirectoryPointer extends BaseFrameworkSystem { 00028 private $pathName = ""; 00029 00033 private $dirPointer = null; 00034 00038 protected function __construct () { 00039 // Call parent constructor 00040 parent::__construct(__CLASS__); 00041 00042 // Clean-up a little 00043 $this->removeNumberFormaters(); 00044 $this->removeSystemArray(); 00045 } 00046 00050 public function __destruct() { 00051 // Is there a resource pointer? Then we have to close the directory here! 00052 if (is_resource($this->getPointer())) { 00053 // Try to close a directory 00054 $this->closeDirectory(); 00055 } 00056 00057 // Call the parent destructor 00058 parent::__destruct(); 00059 } 00060 00082 public final static function createFrameworkDirectoryPointer ($pathName, $inConstructor = false) { 00083 // Some pre-sanity checks... 00084 if (is_null($pathName)) { 00085 // No pathname given 00086 if ($inConstructor) { 00087 return null; 00088 } else { 00089 throw new PathIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING); 00090 } 00091 } elseif (!is_string($pathName)) { 00092 // Is not a string 00093 if ($inConstructor) { 00094 return null; 00095 } else { 00096 throw new InvalidPathStringException(null, self::EXCEPTION_INVALID_STRING); 00097 } 00098 } elseif (!is_dir($pathName)) { 00099 // Not a directory 00100 if ($inConstructor) { 00101 return null; 00102 } else { 00103 throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME); 00104 } 00105 } elseif (!is_readable($pathName)) { 00106 // Not readable 00107 if ($inConstructor) { 00108 return null; 00109 } else { 00110 throw new PathReadProtectedException($pathName, self::EXCEPTION_READ_PROTECED_PATH); 00111 } 00112 } 00113 00114 // Try to open a handler 00115 $dirPointer = @opendir($pathName); 00116 if (!is_resource($dirPointer)) { 00117 // Something bad happend 00118 if ($inConstructor) { 00119 return null; 00120 } else { 00121 throw new DirPointerNotOpenedException($pathName, self::EXCEPTION_DIR_POINTER_INVALID); 00122 } 00123 } 00124 00125 // Create new instance 00126 $pointerInstance = new FrameworkDirectoryPointer(); 00127 00128 // Set directory pointer and path name 00129 $pointerInstance->setPointer($dirPointer); 00130 $pointerInstance->setPathName($pathName); 00131 00132 // Return the instance 00133 return $pointerInstance; 00134 } 00135 00142 public function readRawDirectory () { 00143 // Read data from the directory pointer and return it 00144 return readdir($this->getPointer()); 00145 } 00146 00156 public function readDirectoryExcept ($except = "") { 00157 if ((empty($except)) || (!is_array($except)) || (count($except) == 0)) { 00158 // No exception given, so read all data 00159 return $this->readRawDirectory(); 00160 } 00161 00162 // Read a raw line... 00163 $rawLine = $this->readRawDirectory(); 00164 00165 // Shall we exclude directories? 00166 if ((!is_null($rawLine)) && ($rawLine !== false) && (in_array($rawLine, $except))) { 00167 // Exclude this part 00168 return $this->readDirectoryExcept($except); 00169 } elseif ((!is_null($rawLine)) && ($rawLine !== false)) { 00170 // Return read data 00171 return $rawLine; 00172 } 00173 00174 // End pointer reached 00175 return null; 00176 } 00177 00184 public function closeDirectory () { 00185 // Close the directory pointer and reset the instance variable 00186 @closedir($this->getPointer()); 00187 $this->setPointer(null); 00188 $this->setPathName(""); 00189 } 00190 00197 public final function setPointer ($dirPointer) { 00198 // Sanity-check if pointer is a valid directory resource 00199 if (is_resource($dirPointer) || is_null($dirPointer)) { 00200 // Is a valid resource 00201 $this->dirPointer = $dirPointer; 00202 } else { 00203 // Throw exception 00204 throw new InvalidDirectoryResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER); 00205 } 00206 } 00207 00214 public final function getPointer () { 00215 return $this->dirPointer; 00216 } 00217 00224 public final function setPathName ($pathName) { 00225 $pathName = (string) $pathName; 00226 $this->pathName = $pathName; 00227 } 00228 00234 public final function getPathName () { 00235 return $this->pathName; 00236 } 00237 } 00238 00239 // [EOF] 00240 ?>
1.5.6