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__);
42 // Set part description
43 $this->setObjectDescription("Helper for handling directories");
46 $this->generateUniqueId();
49 $this->removeNumberFormaters();
53 * Destructor for cleaning purposes, etc
55 public function __destruct() {
56 // Is there a resource pointer? Then we have to close the directory here!
57 if (is_resource($this->getPointer())) {
58 // Try to close a directory
59 $this->closeDirectory();
62 // Call the parent destructor
67 * Create a directory pointer based on the given path. The path will also
70 * @param $pathName The path name we shall pass
72 * @param $inConstructor If we are in de/con-structor
73 * or from somewhere else
74 * @throws PathIsEmptyException If the provided path name
76 * @throws InvalidPathStringException If the provided path name is
78 * @throws PathIsNoDirectoryException If the provided path name is
80 * @throws PathReadProtectedException If the provided path name is
82 * @throws DirPointerNotOpened If opendir() returns not a
84 * @return $pointerInstance A prepared instance of
85 * FrameworkDirectoryPointer
87 public final static function createFrameworkDirectoryPointer ($pathName, $inConstructor = false) {
88 // Some pre-sanity checks...
89 if (is_null($pathName)) {
94 throw new PathIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
96 } elseif (!is_string($pathName)) {
101 throw new InvalidPathStringException(null, self::EXCEPTION_INVALID_STRING);
103 } elseif (!is_dir($pathName)) {
105 if ($inConstructor) {
108 throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
110 } elseif (!is_readable($pathName)) {
112 if ($inConstructor) {
115 throw new PathReadProtectedException($pathName, self::EXCEPTION_READ_PROTECED_PATH);
119 // Try to open a handler
120 $dirPointer = @opendir($pathName);
121 if (!is_resource($dirPointer)) {
122 // Something bad happend
123 if ($inConstructor) {
126 throw new DirPointerNotOpenedException($pathName, self::EXCEPTION_DIR_POINTER_INVALID);
130 // Create new instance
131 $pointerInstance = new FrameworkDirectoryPointer();
133 // Set directory pointer and path name
134 $pointerInstance->setPointer($dirPointer);
135 $pointerInstance->setPathName($pathName);
137 // Return the instance
138 return $pointerInstance;
142 * Read raw lines of data from a directory pointer and return the data
144 * @return string Directory and/or file names read from the current
147 public function readRawDirectory () {
148 // Read data from the directory pointer and return it
149 return readdir($this->getPointer());
153 * Read lines from the current directory pointer except some parts
155 * @param $except Some parts of a directory we want to ignore.
157 * Other values will be silently ignored
158 * @return string Directory and/or file names read from the current
161 public function readDirectoryExcept ($except = "") {
162 if ((empty($except)) || (!is_array($except)) || (count($except) == 0)) {
163 // No exception given, so read all data
164 return $this->readRawDirectory();
167 // Read a raw line...
168 $rawLine = $this->readRawDirectory();
170 // Shall we exclude directories?
171 if ((!is_null($rawLine)) && ($rawLine !== false) && (in_array($rawLine, $except))) {
173 return $this->readDirectoryExcept($except);
174 } elseif ((!is_null($rawLine)) && ($rawLine !== false)) {
179 // End pointer reached
184 * Close a directory source and set it's instance to null and the path name
189 public function closeDirectory () {
190 // Close the directory pointer and reset the instance variable
191 @closedir($this->getPointer());
192 $this->setPointer(null);
193 $this->setPathName("");
197 * Setter for the directory pointer
199 * @param $dirPointer The directory resource
202 public final function setPointer ($dirPointer) {
203 // Sanity-check if the pointer is a valid directory resource
204 if (is_resource($dirPointer) || is_null($dirPointer)) {
205 // Is a valid resource
206 $this->dirPointer = $dirPointer;
209 throw new InvalidDirectoryResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
214 * Getter for the directory pointer
216 * @return $dirPointer The directory pointer which shall be a valid
219 public final function getPointer () {
220 return $this->dirPointer;
224 * Setter for path name
226 * @param $pathName The new path name
229 public 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;