3 namespace Org\Mxchange\CoreFramework\Filesytem\Pointer;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filesystem\FrameworkDirectory;
7 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
8 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
11 use Org\Mxchange\CoreFramework\Deprecated\PathIsNoDirectoryException;
14 use \DirectoryIterator;
17 * A class for directory reading and getting its contents, no recursion!
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class FrameworkDirectoryPointer extends BaseFrameworkSystem implements FrameworkDirectory {
40 * The current path we are working in
42 private $pathName = '';
45 * The directory iterator instance
47 private $iteratorInstance = NULL;
50 * Protected constructor
52 protected function __construct () {
53 // Call parent constructor
54 parent::__construct(__CLASS__);
58 * Destructor for cleaning purposes, etc
60 public function __destruct() {
61 // Is there a resource pointer? Then we have to close the directory here!
62 if ($this->getDirectoryIteratorInstance() instanceof DirectoryIterator) {
63 // Try to close a directory
64 $this->closeDirectory();
67 // Call the parent destructor
72 * Create a directory pointer based on the given path. The path will also
75 * @param $pathName The path name we shall pass to opendir()
76 * @return $pointerInstance A prepared instance of FrameworkDirectoryPointer
77 * @throws NullPointerException If the provided path name is null
78 * @throws InvalidPathStringException If the provided path name is not a string
79 * @throws PathIsNoDirectoryException If the provided path name is not valid
80 * @throws PathReadProtectedException If the provided path name is read-protected
82 public static final function createFrameworkDirectoryPointer ($pathName) {
83 // Some pre-sanity checks...
84 if (is_null($pathName)) {
86 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
87 } elseif (!is_string($pathName)) {
89 throw new InvalidPathStringException(NULL, self::EXCEPTION_INVALID_STRING);
90 } elseif (!is_dir($pathName)) {
92 throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
93 } elseif (!is_readable($pathName)) {
95 throw new PathReadProtectedException($pathName, self::EXCEPTION_READ_PROTECED_PATH);
98 // Create new instance
99 $pointerInstance = new FrameworkDirectoryPointer();
101 // Get an iterator for the directory
102 $iteratorInstance = new DirectoryIterator($pathName);
104 // ... and rewind back
105 $iteratorInstance->rewind();
107 // Set directory pointer and path name
108 $pointerInstance->setDirectoryIteratorInstance($iteratorInstance);
109 $pointerInstance->setPathName($pathName);
111 // Return the instance
112 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: Opened pathName=' . $pathName . ' - EXIT!');
113 return $pointerInstance;
117 * Read raw lines of data from a directory pointer and return the data
119 * @return $currentEntry Current entry from encapsulated iterator
121 public function readRawDirectory () {
122 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . '] - CALLED!');
124 // Can the next entry be read?
125 assert($this->getDirectoryIteratorInstance()->valid());
127 // Read data from the directory pointer and return it
128 $currentEntry = $this->getDirectoryIteratorInstance()->current();
130 // Return found entry
131 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: currentEntry[]=' . gettype($currentEntry) . ' - EXIT!');
132 return $currentEntry;
136 * Read lines from the current directory pointer except some parts
138 * @param $except Some parts of a directory we want to ignore. Valid: directory and file names, other values will be silently ignored
139 * @return SplFileInfo An instance of a SplFileInfo class
141 public function readDirectoryExcept (array $except = array()) {
142 // No exceptions given?
143 if (count($except) == 0) {
144 // No exception given, so read all files and directories, but not recursive
145 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: No exceptions given, please use readRawDirectory() instead!');
146 return $this->readRawDirectory();
147 } elseif (!$this->getDirectoryIteratorInstance()->valid()) {
148 // No more left to read
149 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: EOD reached.');
154 $fileInfoInstance = NULL;
156 // Read a raw line...
157 $currentEntry = $this->readRawDirectory();
158 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: currentEntry[]=' . gettype($currentEntry));
160 // Shall we exclude directories?
161 if (is_object($currentEntry)) {
163 $fileInfoInstance = $currentEntry;
164 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: fileInfoInstance[' . gettype($fileInfoInstance) . ']=' . $fileInfoInstance . ',isDot=' . intval($this->getDirectoryIteratorInstance()->isDot()));
166 // Is it a dot-directory or excluded?
167 if (($this->getDirectoryIteratorInstance()->isDot()) && (!in_array($fileInfoInstance, $except))) {
169 $this->getDirectoryIteratorInstance()->next();
172 $fileInfoInstance = $this->readDirectoryExcept($except);
173 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: fileInfoInstance[' . gettype($fileInfoInstance) . ']=' . $fileInfoInstance . ' - Recursive call!');
178 $this->getDirectoryIteratorInstance()->next();
181 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: fileInfoInstance[' . gettype($fileInfoInstance) . ']=' . $fileInfoInstance);
182 return $fileInfoInstance;
186 * Close a directory source and set it's instance to null and the path name
191 public function closeDirectory () {
192 // Close the directory by unsetting it
193 $this->unsetDirectoryIteratorInstance();
194 $this->setPathName('');
198 * Setter for the directory pointer
200 * @param $iteratorInstance An instanceof a DirectoryIterator class
203 protected final function setDirectoryIteratorInstance (DirectoryIterator $iteratorInstance) {
205 $this->iteratorInstance = $iteratorInstance;
209 * Getter for the directory pointer
211 * @return $iteratorInstance The directory pointer which shall be a valid directory resource
213 public final function getDirectoryIteratorInstance () {
214 return $this->iteratorInstance;
218 * Remove directory iterator instance (effectively closing it) by setting
219 * it to NULL. This will trigger a call on the destructor which will then
220 * "close" the iterator.
222 * @param $iteratorInstance An instanceof a DirectoryIterator class
225 protected final function unsetDirectoryIteratorInstance () {
226 // "Unset" the instance
227 $this->iteratorInstance = NULL;
231 * Setter for path name
233 * @param $pathName The new path name
236 protected final function setPathName ($pathName) {
237 $pathName = (string) $pathName;
238 $this->pathName = $pathName;
242 * Getter for path name
244 * @return $pathName The current path name
246 public final function getPathName () {
247 return $this->pathName;