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, this is free software
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__);
43 $this->removeNumberFormaters();
44 $this->removeSystemArray();
48 * Destructor for cleaning purposes, etc
50 public function __destruct() {
51 // Is there a resource pointer? Then we have to close the directory here!
52 if (is_resource($this->getPointer())) {
53 // Try to close a directory
54 $this->closeDirectory();
57 // Call the parent destructor
62 * Create a directory pointer based on the given path. The path will also
65 * @param $pathName The path name we shall pass
67 * @param $inConstructor If we are in de/con-structor
68 * or from somewhere else
69 * @throws PathIsEmptyException If the provided path name
71 * @throws InvalidPathStringException If the provided path name is
73 * @throws PathIsNoDirectoryException If the provided path name is
75 * @throws PathReadProtectedException If the provided path name is
77 * @throws DirPointerNotOpened If opendir() returns not a
79 * @return $pointerInstance A prepared instance of
80 * FrameworkDirectoryPointer
82 public final static function createFrameworkDirectoryPointer ($pathName, $inConstructor = false) {
83 // Some pre-sanity checks...
84 if (is_null($pathName)) {
89 throw new PathIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
91 } elseif (!is_string($pathName)) {
96 throw new InvalidPathStringException(null, self::EXCEPTION_INVALID_STRING);
98 } elseif (!is_dir($pathName)) {
100 if ($inConstructor) {
103 throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
105 } elseif (!is_readable($pathName)) {
107 if ($inConstructor) {
110 throw new PathReadProtectedException($pathName, self::EXCEPTION_READ_PROTECED_PATH);
114 // Try to open a handler
115 $dirPointer = @opendir($pathName);
116 if (!is_resource($dirPointer)) {
117 // Something bad happend
118 if ($inConstructor) {
121 throw new DirPointerNotOpenedException($pathName, self::EXCEPTION_DIR_POINTER_INVALID);
125 // Create new instance
126 $pointerInstance = new FrameworkDirectoryPointer();
128 // Set directory pointer and path name
129 $pointerInstance->setPointer($dirPointer);
130 $pointerInstance->setPathName($pathName);
132 // Return the instance
133 return $pointerInstance;
137 * Read raw lines of data from a directory pointer and return the data
139 * @return string Directory and/or file names read from the current
142 public function readRawDirectory () {
143 // Read data from the directory pointer and return it
144 return readdir($this->getPointer());
148 * Read lines from the current directory pointer except some parts
150 * @param $except Some parts of a directory we want to ignore.
152 * Other values will be silently ignored
153 * @return string Directory and/or file names read from the current
156 public function readDirectoryExcept ($except = "") {
157 if ((empty($except)) || (!is_array($except)) || (count($except) == 0)) {
158 // No exception given, so read all data
159 return $this->readRawDirectory();
162 // Read a raw line...
163 $rawLine = $this->readRawDirectory();
165 // Shall we exclude directories?
166 if ((!is_null($rawLine)) && ($rawLine !== false) && (in_array($rawLine, $except))) {
168 return $this->readDirectoryExcept($except);
169 } elseif ((!is_null($rawLine)) && ($rawLine !== false)) {
174 // End pointer reached
179 * Close a directory source and set it's instance to null and the path name
184 public function closeDirectory () {
185 // Close the directory pointer and reset the instance variable
186 @closedir($this->getPointer());
187 $this->setPointer(null);
188 $this->setPathName("");
192 * Setter for the directory pointer
194 * @param $dirPointer The directory resource
197 public final function setPointer ($dirPointer) {
198 // Sanity-check if pointer is a valid directory resource
199 if (is_resource($dirPointer) || is_null($dirPointer)) {
200 // Is a valid resource
201 $this->dirPointer = $dirPointer;
204 throw new InvalidDirectoryResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
209 * Getter for the directory pointer
211 * @return $dirPointer The directory pointer which shall be a valid
214 public final function getPointer () {
215 return $this->dirPointer;
219 * Setter for path name
221 * @param $pathName The new path name
224 public final function setPathName ($pathName) {
225 $pathName = (string) $pathName;
226 $this->pathName = $pathName;
230 * Getter for path name
232 * @return $pathName The current path name
234 public final function getPathName () {
235 return $this->pathName;