Improved directory (non-recursive) reading:
authorRoland Haeder <roland@mxchange.org>
Mon, 12 May 2014 20:48:19 +0000 (22:48 +0200)
committerRoland Haeder <roland@mxchange.org>
Mon, 12 May 2014 20:48:19 +0000 (22:48 +0200)
- Refactured FrameworkDirectoryPointer to use DirectoryIterator (SPL).
- The use of isDot() makes excluding '.' and '..' obsolete (would be double),
  so more classes can be improved here.
- Also use ObjectFactory instead of direct class name for better flexibility.

Signed-off-by: Roland Häder <roland@mxchange.org>
inc/classes/main/database/databases/class_LocalFileDatabase.php
inc/classes/main/io/class_FrameworkDirectoryPointer.php
inc/classes/middleware/compressor/class_CompressorChannel.php
inc/config.php
inc/loader/class_ClassLoader.php

index ed849a99369afd61d633cd215b1783bec5b26e05..dd1f5b745990c5d6d90f148a913626948ccead42 100644 (file)
@@ -350,7 +350,7 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackend {
                // find the 'table' which is in fact a directory on the server
                try {
                        // Get a directory pointer instance
-                       $directoryInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($pathName);
+                       $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
 
                        // Initialize the result data, this need to be rewritten e.g. if a local file cannot be read
                        $resultData = array(
@@ -364,7 +364,7 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackend {
                        $idx = 1;
 
                        // Read the directory with some exceptions
-                       while (($dataFile = $directoryInstance->readDirectoryExcept(array('.', '..', '.htaccess', '.svn', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
+                       while (($dataFile = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
                                // Does the extension match?
                                if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
                                        // Skip this file!
@@ -487,7 +487,7 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackend {
                // Try all the requests
                try {
                        // Get a file pointer instance
-                       $directoryInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($pathName);
+                       $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($pathName));
 
                        // Initialize limit/skip
                        $limitFound = 0;
@@ -500,7 +500,7 @@ class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackend {
                        $searchInstance = $dataSetInstance->getSearchInstance();
 
                        // Read the directory with some exceptions
-                       while (($dataFile = $directoryInstance->readDirectoryExcept(array('.', '..', '.htaccess', '.svn', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
+                       while (($dataFile = $directoryInstance->readDirectoryExcept(array('.htaccess', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
                                // Does the extension match?
                                if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
                                        // Debug message
index a8bf835895508a30a336ec476f6498265b22bd50..26f2d854e4acaebdbdc386dc47ad770a1bab8824 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * A class for directory reading and getting its contents
+ * A class for directory reading and getting its contents, no recursion!
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @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;
        }
 
        /**
index f5fb945ba31ebf1716102506ccddb5a82925c462..c2ee49496ca98e6261acbd4baf6fa38ee56c975f 100644 (file)
@@ -57,10 +57,10 @@ class CompressorChannel extends BaseMiddleware implements Registerable {
                                $compressorInstance->getConfigInstance()->getConfigEntry('compressor_base_path');
 
                        // Get a directory pointer
-                       $dirPointer = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($baseDir);
+                       $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($baseDir));
 
-                       // Read all directories but no sub directories
-                       while ($directoryEntry = $dirPointer->readDirectoryExcept(array('..', '.', '.htaccess', '.svn', 'class_NullCompressor.php'))) {
+                       // Read all directories but no sub directories, .htaccess files and NullCompressor class
+                       while ($directoryEntry = $directoryInstance->readDirectoryExcept(array('.htaccess', 'class_NullCompressor.php'))) {
                                // Is this a class file?
                                if ((substr($directoryEntry, 0, 6) == 'class_') && (substr($directoryEntry, -4, 4) == '.php')) {
                                        /* Get the compressor's name. That's why you must name
@@ -87,7 +87,7 @@ class CompressorChannel extends BaseMiddleware implements Registerable {
                        } // END - while
 
                        // Close the directory
-                       $dirPointer->closeDirectory();
+                       $directoryInstance->closeDirectory();
                } // END - if
 
                // Check again if there is a compressor
index 868f53fd39aed93777a45997479cec348dd33d2f..ba05848decd8715ccbe1ac78d166fd84dd608ae1 100644 (file)
@@ -356,5 +356,8 @@ $cfg->setConfigEntry('hostname_file', '/etc/hostname');
 // CFG: DATABASE-CACHE-ENABLED
 $cfg->setConfigEntry('database_cache_enabled', FALSE);
 
+// CFG: DIRECTORY-CLASS
+$cfg->setConfigEntry('directory_class', 'FrameworkDirectoryPointer');
+
 // [EOF]
 ?>
index b50f3695793a02ad453f559560646c522239b38e..8b48d60acc16239d7dda8698955acec36c83ccab 100644 (file)
@@ -279,9 +279,9 @@ class ClassLoader {
 
                // Get a new iterator
                //* DEBUG: */ echo "<strong>Base path: {$basePath}</strong><br />\n";
-               $iterator = new RecursiveDirectoryIterator($basePath);
-               $recursive = new RecursiveIteratorIterator($iterator);
-               foreach ($recursive as $entry) {
+               $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($basePath));
+
+               foreach ($iterator as $entry) {
                        // Get filename from iterator
                        $fileName = $entry->getFileName();