Continued:
[core.git] / framework / main / classes / file_directories / directory / class_FrameworkDirectoryPointer.php
index 4b2e1107bfd40c60a4db648669951fe84d697ed1..f080e3281e1107430d23f36ea77135f7df59bc9c 100644 (file)
@@ -4,6 +4,7 @@ namespace Org\Mxchange\CoreFramework\Filesytem\Pointer;
 
 // Import framework stuff
 use Org\Mxchange\CoreFramework\Filesystem\FrameworkDirectory;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
 
@@ -12,13 +13,14 @@ use Org\Mxchange\CoreFramework\Deprecated\PathIsNoDirectoryException;
 
 // Import SPL stuff
 use \DirectoryIterator;
+use \InvalidArgumentException;
 
 /**
  * A class for directory reading and getting its contents, no recursion!
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.shipsimu.org
  *
@@ -49,7 +51,7 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework
        /**
         * Protected constructor
         */
-       protected function __construct () {
+       private function __construct () {
                // Call parent constructor
                parent::__construct(__CLASS__);
        }
@@ -59,13 +61,17 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework
         */
        public function __destruct() {
                // Is there a resource pointer? Then we have to close the directory here!
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FRAMEWORK-DIRECTORY-POINTER: DESTRUCTED!');
                if ($this->getDirectoryIteratorInstance() instanceof DirectoryIterator) {
                        // Try to close a directory
                        $this->closeDirectory();
-               } // END - if
+               }
 
                // Call the parent destructor
                parent::__destruct();
+
+               // Trace message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FRAMEWORK-DIRECTORY-POINTER: EXIT!');
        }
 
        /**
@@ -79,14 +85,12 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework
         * @throws      PathIsNoDirectoryException      If the provided path name is not valid
         * @throws      PathReadProtectedException      If the provided path name is read-protected
         */
-       public static final function createFrameworkDirectoryPointer ($pathName) {
+       public static final function createFrameworkDirectoryPointer (string $pathName) {
                // Some pre-sanity checks...
-               if (is_null($pathName)) {
-                       // No pathname given
-                       throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
-               } elseif (!is_string($pathName)) {
-                       // Is not a string
-                       throw new InvalidPathStringException(NULL, self::EXCEPTION_INVALID_STRING);
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FRAMEWORK-DIRECTORY-POINTER: pathName=%s - CALLED!', $pathName));
+               if (empty($pathName)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "pathName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
                } elseif (!is_dir($pathName)) {
                        // Not a directory
                        throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
@@ -109,7 +113,7 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework
                $pointerInstance->setPathName($pathName);
 
                // Return the instance
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: Opened pathName=' . $pathName . ' - EXIT!');
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DIRECTORY-POINTER: pointerInstance=%s - EXIT!', $pointerInstance->__toString()));
                return $pointerInstance;
        }
 
@@ -119,16 +123,15 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework
         * @return      $currentEntry   Current entry from encapsulated iterator
         */
        public function readRawDirectory () {
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . '] - CALLED!');
-
                // Can the next entry be read?
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('DIRECTORY-POINTER: CALLED!');
                assert($this->getDirectoryIteratorInstance()->valid());
 
                // Read data from the directory pointer and return it
                $currentEntry = $this->getDirectoryIteratorInstance()->current();
 
                // Return found entry
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: currentEntry[]=' . gettype($currentEntry) . ' - EXIT!');
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DIRECTORY-POINTER: currentEntry[%s]=%s - EXIT!', gettype($currentEntry), $currentEntry));
                return $currentEntry;
        }
 
@@ -137,16 +140,17 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework
         *
         * @param       $except         Some parts of a directory we want to ignore. Valid: directory and file names, other values will be silently ignored
         * @return      SplFileInfo             An instance of a SplFileInfo class
+        * @throws      InvalidArgumentException        If a parameter has an invalid value
         */
-       public function readDirectoryExcept (array $except = array()) {
+       public function readDirectoryExcept (array $except = []) {
                // No exceptions given?
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DIRECTORY-POINTER: except()=%d - CALLED!', count($except)));
                if (count($except) == 0) {
                        // No exception given, so read all files and directories, but not recursive
-                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: No exceptions given, please use readRawDirectory() instead!');
-                       return $this->readRawDirectory();
+                       throw new InvalidArgumentException('Parameter "except" is an empty array', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
                } elseif (!$this->getDirectoryIteratorInstance()->valid()) {
                        // No more left to read
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: EOD reached.');
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('DIRECTORY-POINTER: EOD reached - EXIT!');
                        return NULL;
                }
 
@@ -155,30 +159,28 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework
 
                // Read a raw line...
                $currentEntry = $this->readRawDirectory();
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: currentEntry[]=' . gettype($currentEntry));
 
                // Shall we exclude directories?
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DIRECTORY-POINTER: currentEntry[]=%s', gettype($currentEntry)));
                if (is_object($currentEntry)) {
                        // Get file name
                        $fileInfoInstance = $currentEntry;
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: fileInfoInstance[' . gettype($fileInfoInstance) . ']=' . $fileInfoInstance . ',isDot=' . intval($this->getDirectoryIteratorInstance()->isDot()));
 
                        // Is it a dot-directory or excluded?
-                       if (($this->getDirectoryIteratorInstance()->isDot()) && (!in_array($fileInfoInstance, $except))) {
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('DIRECTORY-POINTER: fileInfoInstance->filename=%s,isDot=%d', $fileInfoInstance->getFilename(), intval($this->getDirectoryIteratorInstance()->isDot())));
+                       while ($this->getDirectoryIteratorInstance()->valid() && ($this->getDirectoryIteratorInstance()->isDot() || in_array($fileInfoInstance->getFilename(), $except))) {
+                               // Update current instance
+                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('DIRECTORY-POINTER: fileInfoInstance->filename=%s,isDot=%d', $fileInfoInstance->getFilename(), intval($this->getDirectoryIteratorInstance()->isDot())));
+                               $fileInfoInstance = $this->readRawDirectory();
+
                                // To next entry
+                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('DIRECTORY-POINTER: Invoking directoryIteratorInstance->next() ...');
                                $this->getDirectoryIteratorInstance()->next();
-
-                               // Exclude this part
-                               $fileInfoInstance = $this->readDirectoryExcept($except);
-                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: fileInfoInstance[' . gettype($fileInfoInstance) . ']=' . $fileInfoInstance . ' - Recursive call!');
-                       } // END - if
-               } // END - if
-
-               // To next entry
-               $this->getDirectoryIteratorInstance()->next();
+                       }
+               }
 
                // Return read line
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: fileInfoInstance[' . gettype($fileInfoInstance) . ']=' . $fileInfoInstance);
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DIRECTORY-POINTER: fileInfoInstance[%s]=%s - EXIT!', gettype($fileInfoInstance), $fileInfoInstance));
                return $fileInfoInstance;
        }
 
@@ -190,8 +192,12 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework
         */
        public function closeDirectory () {
                // Close the directory by unsetting it
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('DIRECTORY-POINTER: CALLED!');
                $this->unsetDirectoryIteratorInstance();
                $this->setPathName('');
+
+               // Trace message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('DIRECTORY-POINTER: CALLED!');
        }
 
        /**
@@ -233,8 +239,7 @@ class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Framework
         * @param       $pathName       The new path name
         * @return      void
         */
-       protected final function setPathName ($pathName) {
-               $pathName = (string) $pathName;
+       protected final function setPathName (string $pathName) {
                $this->pathName = $pathName;
        }