// 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 - 2020 Core Developer Team
* @license GNU GPL 3.0 or any newer version
* @link http://www.shipsimu.org
*
* @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);
+ if (empty($pathName)) {
+ // Throw IAE
+ throw new InvalidArgumentException('Parameter "pathName" is empty');
} elseif (!is_dir($pathName)) {
// Not a directory
throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
$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__)->debugOutput('DIRECTORY-POINTER: Opened pathName=' . $pathName . ' - EXIT!');
return $pointerInstance;
}
* @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__)->debugOutput('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__)->debugOutput(sprintf('DIRECTORY-POINTER: currentEntry[%s]=%s - EXIT!', gettype($currentEntry), $currentEntry));
return $currentEntry;
}
* @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
*/
- public function readDirectoryExcept (array $except = array()) {
+ public function readDirectoryExcept (array $except = []) {
// No exceptions given?
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(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!');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: No exceptions given, please use readRawDirectory() instead!');
return $this->readRawDirectory();
} 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__)->debugOutput('DIRECTORY-POINTER: EOD reached - EXIT!');
return NULL;
}
// 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__)->debugOutput(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__)->debugOutput(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__)->debugOutput(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__)->debugOutput('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 - while
} // 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__)->debugOutput(sprintf('DIRECTORY-POINTER: fileInfoInstance[%s]=%s - EXIT!', gettype($fileInfoInstance), $fileInfoInstance));
return $fileInfoInstance;
}
*/
public function closeDirectory () {
// Close the directory by unsetting it
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: CALLED!');
$this->unsetDirectoryIteratorInstance();
$this->setPathName('');
}
* @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;
}