X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fio%2Fclass_FrameworkDirectoryPointer.php;h=26f2d854e4acaebdbdc386dc47ad770a1bab8824;hp=a8bf835895508a30a336ec476f6498265b22bd50;hb=84daf7f99e1af16ef9496e14f55ddf35f8657dc6;hpb=87dacf0cd1569f9ef85ce1f61003d3169feec6d2 diff --git a/inc/classes/main/io/class_FrameworkDirectoryPointer.php b/inc/classes/main/io/class_FrameworkDirectoryPointer.php index a8bf8358..26f2d854 100644 --- a/inc/classes/main/io/class_FrameworkDirectoryPointer.php +++ b/inc/classes/main/io/class_FrameworkDirectoryPointer.php @@ -1,6 +1,6 @@ * @version 0.0.0 @@ -28,9 +28,9 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem { private $pathName = ''; /** - * The directory pointer + * The directory iterator instance */ - private $dirPointer = NULL; + private $directoryInstance = NULL; /** * Protected constructor @@ -45,10 +45,10 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem { */ public function __destruct() { // Is there a resource pointer? Then we have to close the directory here! - if (is_resource($this->getPointer())) { + if ($this->getDirectoryInstance() instanceof DirectoryIterator) { // Try to close a directory $this->closeDirectory(); - } + } // END - if // Call the parent destructor parent::__destruct(); @@ -65,7 +65,7 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem { * @throws InvalidPathStringException If the provided path name is not a string * @throws PathIsNoDirectoryException If the provided path name is not valid * @throws PathReadProtectedException If the provided path name is read-protected - * @throws DirPointerNotOpenedException If opendir() returns not a directory resource + * @todo Get rid of inConstructor, could be old-lost code. */ public static final function createFrameworkDirectoryPointer ($pathName, $inConstructor = FALSE) { // Some pre-sanity checks... @@ -99,22 +99,14 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem { } } - // Try to open a handler - $dirPointer = @opendir($pathName); - if (!is_resource($dirPointer)) { - // Something bad happend - if ($inConstructor) { - return NULL; - } else { - throw new DirPointerNotOpenedException($pathName, self::EXCEPTION_DIR_POINTER_INVALID); - } - } + // Get an iterator for the directory + $directoryInstance = new DirectoryIterator($pathName); // Create new instance $pointerInstance = new FrameworkDirectoryPointer(); // Set directory pointer and path name - $pointerInstance->setPointer($dirPointer); + $pointerInstance->setDirectoryInstance($directoryInstance); $pointerInstance->setPathName($pathName); // Return the instance @@ -124,11 +116,26 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem { /** * Read raw lines of data from a directory pointer and return the data * - * @return string Directory and/or file names read from the current directory pointer + * @return $current Current entry from encapsulated iterator */ public function readRawDirectory () { - // Read data from the directory pointer and return it - return readdir($this->getPointer()); + // Can the next entry be read? + assert($this->getDirectoryInstance()->valid()); + + // Is it a dot directory? + if ($this->getDirectoryInstance()->isDot()) { + // Then call this method recursive + $current = $this->readRawDirectory(); + } else { + // Read data from the directory pointer and return it + $current = $this->getDirectoryInstance()->current(); + } + + // Advance to next entry + $this->getDirectoryInstance()->next(); + + // Return found entry + return $current; } /** @@ -137,7 +144,8 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem { * @param $except Some parts of a directory we want to ignore. Valid: directory and file names, other values will be silently ignored * @return string Directory and/or file names read from the current directory pointer */ - public function readDirectoryExcept (array $except = array('.', '..')) { + public function readDirectoryExcept (array $except = array()) { + // No exceptions given? if (count($except) == 0) { // No exception given, so read all data self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: No exceptions given, please use readRawDirectory() instead!'); @@ -146,20 +154,21 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem { // Read a raw line... $rawLine = $this->readRawDirectory(); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: rawLine[' . gettype($rawLine) . ']=' . $rawLine); // Shall we exclude directories? if ((!is_null($rawLine)) && ($rawLine !== FALSE) && (!in_array($rawLine, $except))) { // Return read data - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: rawLine[' . gettype($rawLine) . ']=' . $rawLine); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: rawLine[' . gettype($rawLine) . ']=' . $rawLine); return $rawLine; } elseif ((!is_null($rawLine)) && ($rawLine !== FALSE)) { // Exclude this part - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: rawline[' . gettype($rawLine) . ']=' . $rawLine . ' - Recursive call!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: rawline[' . gettype($rawLine) . ']=' . $rawLine . ' - Recursive call!'); return $this->readDirectoryExcept($except); } // End pointer reached - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: Returning NULL!'); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: Returning NULL!'); return NULL; } @@ -170,36 +179,29 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem { * @return void */ public function closeDirectory () { - // Close the directory pointer and reset the instance variable - @closedir($this->getPointer()); - $this->setPointer(NULL); + // Close the directory by unsetting it + $this->setDirectoryInstance(NULL); $this->setPathName(''); } /** * Setter for the directory pointer * - * @param $dirPointer The directory resource + * @param $directoryInstance An instanceof a DirectoryIterator class or NULL to unset ("close") it. * @return void */ - public final function setPointer ($dirPointer) { - // Sanity-check if pointer is a valid directory resource - if (is_resource($dirPointer) || is_null($dirPointer)) { - // Is a valid resource - $this->dirPointer = $dirPointer; - } else { - // Throw exception - throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE); - } + protected final function setDirectoryInstance (DirectoryIterator $directoryInstance = NULL) { + // Set instance (or NULL) + $this->directoryInstance = $directoryInstance; } /** * Getter for the directory pointer * - * @return $dirPointer The directory pointer which shall be a valid directory resource + * @return $directoryInstance The directory pointer which shall be a valid directory resource */ - public final function getPointer () { - return $this->dirPointer; + public final function getDirectoryInstance () { + return $this->directoryInstance; } /**