3 * A class for directory reading and getting its contents, no recursion!
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class FrameworkDirectoryPointer extends BaseFrameworkSystem implements Directory {
26 * The current path we are working in
28 private $pathName = '';
31 * The directory iterator instance
33 private $iteratorInstance = NULL;
36 * Protected constructor
38 protected function __construct () {
39 // Call parent constructor
40 parent::__construct(__CLASS__);
44 * Destructor for cleaning purposes, etc
46 public function __destruct() {
47 // Is there a resource pointer? Then we have to close the directory here!
48 if ($this->getIteratorInstance() instanceof DirectoryIterator) {
49 // Try to close a directory
50 $this->closeDirectory();
53 // Call the parent destructor
58 * Create a directory pointer based on the given path. The path will also
61 * @param $pathName The path name we shall pass to opendir()
62 * @param $inConstructor If we are in de/con-structor or from somewhere else
63 * @return $pointerInstance A prepared instance of FrameworkDirectoryPointer
64 * @throws PathIsEmptyException If the provided path name is empty
65 * @throws InvalidPathStringException If the provided path name is not a string
66 * @throws PathIsNoDirectoryException If the provided path name is not valid
67 * @throws PathReadProtectedException If the provided path name is read-protected
68 * @todo Get rid of inConstructor, could be old-lost code.
70 public static final function createFrameworkDirectoryPointer ($pathName, $inConstructor = FALSE) {
71 // Some pre-sanity checks...
72 if (is_null($pathName)) {
77 throw new PathIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
79 } elseif (!is_string($pathName)) {
84 throw new InvalidPathStringException(NULL, self::EXCEPTION_INVALID_STRING);
86 } elseif (!is_dir($pathName)) {
91 throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
93 } elseif (!is_readable($pathName)) {
98 throw new PathReadProtectedException($pathName, self::EXCEPTION_READ_PROTECED_PATH);
102 // Create new instance
103 $pointerInstance = new FrameworkDirectoryPointer();
105 // Get an iterator for the directory
106 $iteratorInstance = new DirectoryIterator($pathName);
108 // ... and rewind back
109 $iteratorInstance->rewind();
111 // Set directory pointer and path name
112 $pointerInstance->setIteratorInstance($iteratorInstance);
113 $pointerInstance->setPathName($pathName);
115 // Return the instance
116 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: Opened pathName=' . $pathName . ' - EXIT!');
117 return $pointerInstance;
121 * Read raw lines of data from a directory pointer and return the data
123 * @return $currentEntry Current entry from encapsulated iterator
125 public function readRawDirectory () {
126 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . '] - CALLED!');
128 // Can the next entry be read?
129 assert($this->getIteratorInstance()->valid());
132 $currentEntry = FALSE;
134 // Read data from the directory pointer and return it
135 $currentEntry = $this->getIteratorInstance()->current();
137 // Return found entry
138 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: currentEntry[]=' . gettype($currentEntry) . ' - EXIT!');
139 return $currentEntry;
143 * Read lines from the current directory pointer except some parts
145 * @param $except Some parts of a directory we want to ignore. Valid: directory and file names, other values will be silently ignored
146 * @return string Directory and/or file names read from the current directory pointer
148 public function readDirectoryExcept (array $except = array()) {
149 // No exceptions given?
150 if (count($except) == 0) {
151 // No exception given, so read all files and directories, but not recursive
152 self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: No exceptions given, please use readRawDirectory() instead!');
153 return $this->readRawDirectory();
154 } elseif (!$this->getIteratorInstance()->valid()) {
155 // No more left to read
162 // Read a raw line...
163 $currentEntry = $this->readRawDirectory();
164 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: currentEntry[]=' . gettype($currentEntry));
166 // Shall we exclude directories?
167 if (is_object($currentEntry)) {
169 $rawLine = $currentEntry->getFilename();
170 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: rawLine[' . gettype($rawLine) . ']=' . $rawLine);
172 // Is it not excluded?
173 if (in_array($rawLine, $except)) {
175 $this->getIteratorInstance()->next();
178 $rawLine = $this->readDirectoryExcept($except);
179 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: rawline[' . gettype($rawLine) . ']=' . $rawLine . ' - Recursive call!');
184 $this->getIteratorInstance()->next();
187 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: rawLine[' . gettype($rawLine) . ']=' . $rawLine);
192 * Close a directory source and set it's instance to null and the path name
197 public function closeDirectory () {
198 // Close the directory by unsetting it
199 $this->setIteratorInstance(NULL);
200 $this->setPathName('');
204 * Setter for the directory pointer
206 * @param $iteratorInstance An instanceof a DirectoryIterator class or NULL to unset ("close") it.
209 protected final function setIteratorInstance (DirectoryIterator $iteratorInstance = NULL) {
210 // Set instance (or NULL)
211 $this->iteratorInstance = $iteratorInstance;
215 * Getter for the directory pointer
217 * @return $iteratorInstance The directory pointer which shall be a valid directory resource
219 public final function getIteratorInstance () {
220 return $this->iteratorInstance;
224 * Setter for path name
226 * @param $pathName The new path name
229 protected final function setPathName ($pathName) {
230 $pathName = (string) $pathName;
231 $this->pathName = $pathName;
235 * Getter for path name
237 * @return $pathName The current path name
239 public final function getPathName () {
240 return $this->pathName;