3 * A class for directory reading and getting its contents
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.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 {
26 * The current path we are working in
28 private $pathName = '';
31 * The directory pointer
33 private $dirPointer = 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 (is_resource($this->getPointer())) {
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
64 * @return $pointerInstance A prepared instance of
65 * FrameworkDirectoryPointer
66 * @throws PathIsEmptyException If the provided path name
68 * @throws InvalidPathStringException If the provided path name is
70 * @throws PathIsNoDirectoryException If the provided path name is
72 * @throws PathReadProtectedException If the provided path name is
74 * @throws DirPointerNotOpened If opendir() returns not a
77 public static final function createFrameworkDirectoryPointer ($pathName, $inConstructor = false) {
78 // Some pre-sanity checks...
79 if (is_null($pathName)) {
84 throw new PathIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
86 } elseif (!is_string($pathName)) {
91 throw new InvalidPathStringException(null, self::EXCEPTION_INVALID_STRING);
93 } elseif (!is_dir($pathName)) {
98 throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
100 } elseif (!is_readable($pathName)) {
102 if ($inConstructor) {
105 throw new PathReadProtectedException($pathName, self::EXCEPTION_READ_PROTECED_PATH);
109 // Try to open a handler
110 $dirPointer = @opendir($pathName);
111 if (!is_resource($dirPointer)) {
112 // Something bad happend
113 if ($inConstructor) {
116 throw new DirPointerNotOpenedException($pathName, self::EXCEPTION_DIR_POINTER_INVALID);
120 // Create new instance
121 $pointerInstance = new FrameworkDirectoryPointer();
123 // Set directory pointer and path name
124 $pointerInstance->setPointer($dirPointer);
125 $pointerInstance->setPathName($pathName);
127 // Return the instance
128 return $pointerInstance;
132 * Read raw lines of data from a directory pointer and return the data
134 * @return string Directory and/or file names read from the current
137 public function readRawDirectory () {
138 // Read data from the directory pointer and return it
139 return readdir($this->getPointer());
143 * Read lines from the current directory pointer except some parts
145 * @param $except Some parts of a directory we want to ignore.
147 * Other values will be silently ignored
148 * @return string Directory and/or file names read from the current
151 public function readDirectoryExcept (array $except = array()) {
152 if (count($except) == 0) {
153 // No exception given, so read all data
154 return $this->readRawDirectory();
157 // Read a raw line...
158 $rawLine = $this->readRawDirectory();
160 // Shall we exclude directories?
161 if ((!is_null($rawLine)) && ($rawLine !== false) && (!in_array($rawLine, $except))) {
163 //* NOISY-DEBUG: */ $this->debugOutput('DIRECTORY: rawLine[' . gettype($rawLine) . ']=' . $rawLine);
165 } elseif ((!is_null($rawLine)) && ($rawLine !== false)) {
167 //* NOISY-DEBUG: */ $this->debugOutput('DIRECTORY: rawline[' . gettype($rawLine) . ']=' . $rawLine . ' - Recursive call!');
168 return $this->readDirectoryExcept($except);
171 // End pointer reached
172 //* NOISY-DEBUG: */ $this->debugOutput('DIRECTORY: Returning NULL!');
177 * Close a directory source and set it's instance to null and the path name
182 public function closeDirectory () {
183 // Close the directory pointer and reset the instance variable
184 @closedir($this->getPointer());
185 $this->setPointer(NULL);
186 $this->setPathName('');
190 * Setter for the directory pointer
192 * @param $dirPointer The directory resource
195 public final function setPointer ($dirPointer) {
196 // Sanity-check if pointer is a valid directory resource
197 if (is_resource($dirPointer) || is_null($dirPointer)) {
198 // Is a valid resource
199 $this->dirPointer = $dirPointer;
202 throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
207 * Getter for the directory pointer
209 * @return $dirPointer The directory pointer which shall be a valid
212 public final function getPointer () {
213 return $this->dirPointer;
217 * Setter for path name
219 * @param $pathName The new path name
222 public final function setPathName ($pathName) {
223 $pathName = (string) $pathName;
224 $this->pathName = $pathName;
228 * Getter for path name
230 * @return $pathName The current path name
232 public final function getPathName () {
233 return $this->pathName;