3 * A class for directory reading and getting its contents
5 class FrameworkDirectoryPointer extends BaseFrameworkSystem {
7 * The current path we are working in
9 private $pathName = "";
12 * The directory pointer
14 private $dirPointer = null;
19 private function __construct () {
20 // Call parent constructor
21 parent::constructor(__CLASS__);
23 // Set part description
24 $this->setPartDescr("Verzeichnis-Handler");
27 $this->createUniqueID();
30 $this->removeNumberFormaters();
34 * Destructor for cleaning purposes, etc
36 public function __destruct() {
37 // Is there a resource pointer? Then we have to close the directory here!
38 if (is_resource($this->getPointer())) {
39 // Try to close a directory
40 $this->closeDirectory();
43 // Call the parent destructor
48 * Create a directory pointer based on the given path. The path will also
51 * @param $pathName The path name we shall pass
53 * @param $inConstructor If we are in de/con-structor
54 * or from somewhere else
55 * @throws PathIsEmptyException If the provided path name
57 * @throws InvalidPathStringException If the provided path name is
59 * @throws PathIsNoDirectoryException If the provided path name is
61 * @throws PathReadProtectedException If the provided path name is
63 * @throws DirPointerNotOpened If opendir() returns not a
65 * @return $pointerInstance A prepared instance of
66 * FrameworkDirectoryPointer
68 public final static function createFrameworkDirectoryPointer ($pathName, $inConstructor = false) {
69 // Some pre-sanity checks...
70 if (is_null($pathName)) {
75 throw new PathIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
77 } elseif (!is_string($pathName)) {
82 throw new InvalidPathStringException(null, self::EXCEPTION_INVALID_STRING);
84 } elseif (!is_dir($pathName)) {
89 throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
91 } elseif (!is_readable($pathName)) {
96 throw new PathReadProtectedException($pathName, self::EXCEPTION_READ_PROTECED_PATH);
100 // Try to open a handler
101 $dirPointer = @opendir($pathName);
102 if (!is_resource($dirPointer)) {
103 // Something bad happend
104 if ($inConstructor) {
107 throw new DirPointerNotOpenedException($pathName, self::EXCEPTION_DIR_POINTER_INVALID);
111 // Create new instance
112 $pointerInstance = new FrameworkDirectoryPointer();
114 // Set directory pointer and path name
115 $pointerInstance->setPointer($dirPointer);
116 $pointerInstance->setPathName($pathName);
118 // Return the instance
119 return $pointerInstance;
123 * Read raw lines of data from a directory pointer and return the data
125 * @return string Directory and/or file names read from the current
127 * @throws NullPointerException If the directory pointer instance
128 * is not set by setPointer()
129 * @throws InvalidDirectoryResourceException If there is being set
130 * an invalid directory resource
132 public function readRawDirectory () {
133 if (is_null($this->getPointer())) {
134 // Pointer not initialized
135 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
136 } elseif (!is_resource($this->getPointer())) {
137 // Pointer is not a valid resource!
138 throw new InvalidDirectoryResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
141 // Read data from the directory pointer and return it
142 return readdir($this->getPointer());
146 * Read lines from the current directory pointer except some parts
148 * @param $except Some parts of a directory we want to ignore.
150 * Other values will be silently ignored
151 * @return string Directory and/or file names read from the current
154 public function readDirectoryExcept ($except = "") {
155 if ((empty($except)) || (!is_array($except)) || (count($except) == 0)) {
156 // No exception given, so read all data
157 return $this->readRawDirectory();
160 // Read a raw line...
161 $rawLine = $this->readRawDirectory();
163 // Shall we exclude directories?
164 if ((!is_null($rawLine)) && ($rawLine !== false) && (in_array($rawLine, $except))) {
166 return $this->readDirectoryExcept($except);
167 } elseif ((!is_null($rawLine)) && ($rawLine !== false)) {
172 // End pointer reached
177 * Close a directory source and set it's instance to null and the path name
181 * @throws NullPointerException If the directory pointer instance
182 * is not set by setPointer()
183 * @throws InvalidDirectoryResourceException If there is being set
185 public function closeDirectory () {
186 if (is_null($this->getPointer())) {
187 // Pointer not initialized
189 } elseif (!is_resource($this->getPointer())) {
190 // Pointer is not a valid resource!
191 throw new InvalidDirectoryResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
194 // Close the directory pointer and reset the instance variable
195 @closedir($this->getPointer());
196 $this->setPointer(null);
197 $this->setPathName("");
201 * Setter for the directory pointer
203 * @param $dirPointer The directory resource
206 public final function setPointer ($dirPointer) {
207 // Sanity-check if the pointer is a valid directory resource
208 if (is_resource($dirPointer) || is_null($dirPointer)) {
209 // Is a valid resource
210 $this->dirPointer = $dirPointer;
213 throw new InvalidDirectoryResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
218 * Getter for the directory pointer
220 * @return $dirPointer The directory pointer which shall be a valid
223 public final function getPointer () {
224 return $this->dirPointer;
228 * Setter for path name
230 * @param $pathName The new path name
233 public final function setPathName ($pathName) {
234 $pathName = (string) $pathName;
235 $this->pathName = $pathName;
239 * Getter for path name
241 * @return $pathName The current path name
243 public final function getPathName () {
244 return $this->pathName;