// 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(
$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!
// 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;
$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
<?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
private $pathName = '';
/**
- * The directory pointer
+ * The directory iterator instance
*/
- private $dirPointer = NULL;
+ private $directoryInstance = NULL;
/**
* Protected constructor
*/
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();
* @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...
}
}
- // 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
/**
* 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;
}
/**
* @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!');
// 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;
}
* @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;
}
/**