3 namespace Org\Mxchange\CoreFramework\Filesytem\Pointer;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filesystem\FrameworkDirectory;
7 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
8 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
9 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
12 use Org\Mxchange\CoreFramework\Deprecated\PathIsNoDirectoryException;
15 use \DirectoryIterator;
16 use \InvalidArgumentException;
19 * A class for directory reading and getting its contents, no recursion!
21 * @author Roland Haeder <webmaster@shipsimu.org>
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.shipsimu.org
27 * This program is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program. If not, see <http://www.gnu.org/licenses/>.
40 class FrameworkDirectoryPointer extends BaseFrameworkSystem implements FrameworkDirectory {
42 * The current path we are working in
44 private $pathName = '';
47 * The directory iterator instance
49 private $iteratorInstance = NULL;
52 * Protected constructor
54 private function __construct () {
55 // Call parent constructor
56 parent::__construct(__CLASS__);
60 * Destructor for cleaning purposes, etc
62 public function __destruct() {
63 // Is there a resource pointer? Then we have to close the directory here!
64 if ($this->getDirectoryIteratorInstance() instanceof DirectoryIterator) {
65 // Try to close a directory
66 $this->closeDirectory();
69 // Call the parent destructor
74 * Create a directory pointer based on the given path. The path will also
77 * @param $pathName The path name we shall pass to opendir()
78 * @return $pointerInstance A prepared instance of FrameworkDirectoryPointer
79 * @throws NullPointerException If the provided path name is null
80 * @throws InvalidPathStringException If the provided path name is not a string
81 * @throws PathIsNoDirectoryException If the provided path name is not valid
82 * @throws PathReadProtectedException If the provided path name is read-protected
84 public static final function createFrameworkDirectoryPointer (string $pathName) {
85 // Some pre-sanity checks...
86 if (empty($pathName)) {
88 throw new InvalidArgumentException('Parameter "pathName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
89 } elseif (!is_dir($pathName)) {
91 throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
92 } elseif (!is_readable($pathName)) {
94 throw new PathReadProtectedException($pathName, self::EXCEPTION_READ_PROTECED_PATH);
97 // Create new instance
98 $pointerInstance = new FrameworkDirectoryPointer();
100 // Get an iterator for the directory
101 $iteratorInstance = new DirectoryIterator($pathName);
103 // ... and rewind back
104 $iteratorInstance->rewind();
106 // Set directory pointer and path name
107 $pointerInstance->setDirectoryIteratorInstance($iteratorInstance);
108 $pointerInstance->setPathName($pathName);
110 // Return the instance
111 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: Opened pathName=' . $pathName . ' - EXIT!');
112 return $pointerInstance;
116 * Read raw lines of data from a directory pointer and return the data
118 * @return $currentEntry Current entry from encapsulated iterator
120 public function readRawDirectory () {
121 // Can the next entry be read?
122 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: CALLED!');
123 assert($this->getDirectoryIteratorInstance()->valid());
125 // Read data from the directory pointer and return it
126 $currentEntry = $this->getDirectoryIteratorInstance()->current();
128 // Return found entry
129 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DIRECTORY-POINTER: currentEntry[%s]=%s - EXIT!', gettype($currentEntry), $currentEntry));
130 return $currentEntry;
134 * Read lines from the current directory pointer except some parts
136 * @param $except Some parts of a directory we want to ignore. Valid: directory and file names, other values will be silently ignored
137 * @return SplFileInfo An instance of a SplFileInfo class
139 public function readDirectoryExcept (array $except = []) {
140 // No exceptions given?
141 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DIRECTORY-POINTER: except()=%d - CALLED!', count($except)));
142 if (count($except) == 0) {
143 // No exception given, so read all files and directories, but not recursive
144 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: No exceptions given, please use readRawDirectory() instead!');
145 return $this->readRawDirectory();
146 } elseif (!$this->getDirectoryIteratorInstance()->valid()) {
147 // No more left to read
148 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: EOD reached - EXIT!');
153 $fileInfoInstance = NULL;
155 // Read a raw line...
156 $currentEntry = $this->readRawDirectory();
158 // Shall we exclude directories?
159 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DIRECTORY-POINTER: currentEntry[]=%s', gettype($currentEntry)));
160 if (is_object($currentEntry)) {
162 $fileInfoInstance = $currentEntry;
164 // Is it a dot-directory or excluded?
165 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DIRECTORY-POINTER: fileInfoInstance->filename=%s,isDot=%d', $fileInfoInstance->getFilename(), intval($this->getDirectoryIteratorInstance()->isDot())));
166 while ($this->getDirectoryIteratorInstance()->valid() && ($this->getDirectoryIteratorInstance()->isDot() || in_array($fileInfoInstance->getFilename(), $except))) {
167 // Update current instance
168 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DIRECTORY-POINTER: fileInfoInstance->filename=%s,isDot=%d', $fileInfoInstance->getFilename(), intval($this->getDirectoryIteratorInstance()->isDot())));
169 $fileInfoInstance = $this->readRawDirectory();
172 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: Invoking directoryIteratorInstance->next() ...');
173 $this->getDirectoryIteratorInstance()->next();
178 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DIRECTORY-POINTER: fileInfoInstance[%s]=%s - EXIT!', gettype($fileInfoInstance), $fileInfoInstance));
179 return $fileInfoInstance;
183 * Close a directory source and set it's instance to null and the path name
188 public function closeDirectory () {
189 // Close the directory by unsetting it
190 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: CALLED!');
191 $this->unsetDirectoryIteratorInstance();
192 $this->setPathName('');
196 * Setter for the directory pointer
198 * @param $iteratorInstance An instanceof a DirectoryIterator class
201 protected final function setDirectoryIteratorInstance (DirectoryIterator $iteratorInstance) {
203 $this->iteratorInstance = $iteratorInstance;
207 * Getter for the directory pointer
209 * @return $iteratorInstance The directory pointer which shall be a valid directory resource
211 public final function getDirectoryIteratorInstance () {
212 return $this->iteratorInstance;
216 * Remove directory iterator instance (effectively closing it) by setting
217 * it to NULL. This will trigger a call on the destructor which will then
218 * "close" the iterator.
220 * @param $iteratorInstance An instanceof a DirectoryIterator class
223 protected final function unsetDirectoryIteratorInstance () {
224 // "Unset" the instance
225 $this->iteratorInstance = NULL;
229 * Setter for path name
231 * @param $pathName The new path name
234 protected final function setPathName (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;