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;
15 use \InvalidArgumentException;
18 * A class for directory reading and getting its contents, no recursion!
20 * @author Roland Haeder <webmaster@shipsimu.org>
22 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.shipsimu.org
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 class FrameworkDirectoryPointer extends BaseFrameworkSystem implements FrameworkDirectory {
41 * The current path we are working in
43 private $pathName = '';
46 * The directory iterator instance
48 private $iteratorInstance = NULL;
51 * Protected constructor
53 private function __construct () {
54 // Call parent constructor
55 parent::__construct(__CLASS__);
59 * Destructor for cleaning purposes, etc
61 public function __destruct() {
62 // Is there a resource pointer? Then we have to close the directory here!
63 if ($this->getDirectoryIteratorInstance() instanceof DirectoryIterator) {
64 // Try to close a directory
65 $this->closeDirectory();
68 // Call the parent destructor
73 * Create a directory pointer based on the given path. The path will also
76 * @param $pathName The path name we shall pass to opendir()
77 * @return $pointerInstance A prepared instance of FrameworkDirectoryPointer
78 * @throws NullPointerException If the provided path name is null
79 * @throws InvalidPathStringException If the provided path name is not a string
80 * @throws PathIsNoDirectoryException If the provided path name is not valid
81 * @throws PathReadProtectedException If the provided path name is read-protected
83 public static final function createFrameworkDirectoryPointer (string $pathName) {
84 // Some pre-sanity checks...
85 if (empty($pathName)) {
87 throw new InvalidArgumentException('Parameter "pathName" is empty');
88 } elseif (!is_dir($pathName)) {
90 throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
91 } elseif (!is_readable($pathName)) {
93 throw new PathReadProtectedException($pathName, self::EXCEPTION_READ_PROTECED_PATH);
96 // Create new instance
97 $pointerInstance = new FrameworkDirectoryPointer();
99 // Get an iterator for the directory
100 $iteratorInstance = new DirectoryIterator($pathName);
102 // ... and rewind back
103 $iteratorInstance->rewind();
105 // Set directory pointer and path name
106 $pointerInstance->setDirectoryIteratorInstance($iteratorInstance);
107 $pointerInstance->setPathName($pathName);
109 // Return the instance
110 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: Opened pathName=' . $pathName . ' - EXIT!');
111 return $pointerInstance;
115 * Read raw lines of data from a directory pointer and return the data
117 * @return $currentEntry Current entry from encapsulated iterator
119 public function readRawDirectory () {
120 // Can the next entry be read?
121 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: CALLED!');
122 assert($this->getDirectoryIteratorInstance()->valid());
124 // Read data from the directory pointer and return it
125 $currentEntry = $this->getDirectoryIteratorInstance()->current();
127 // Return found entry
128 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DIRECTORY-POINTER: currentEntry[%s]=%s - EXIT!', gettype($currentEntry), $currentEntry));
129 return $currentEntry;
133 * Read lines from the current directory pointer except some parts
135 * @param $except Some parts of a directory we want to ignore. Valid: directory and file names, other values will be silently ignored
136 * @return SplFileInfo An instance of a SplFileInfo class
138 public function readDirectoryExcept (array $except = []) {
139 // No exceptions given?
140 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DIRECTORY-POINTER: except()=%d - CALLED!', count($except)));
141 if (count($except) == 0) {
142 // No exception given, so read all files and directories, but not recursive
143 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: No exceptions given, please use readRawDirectory() instead!');
144 return $this->readRawDirectory();
145 } elseif (!$this->getDirectoryIteratorInstance()->valid()) {
146 // No more left to read
147 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: EOD reached - EXIT!');
152 $fileInfoInstance = NULL;
154 // Read a raw line...
155 $currentEntry = $this->readRawDirectory();
157 // Shall we exclude directories?
158 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DIRECTORY-POINTER: currentEntry[]=%s', gettype($currentEntry)));
159 if (is_object($currentEntry)) {
161 $fileInfoInstance = $currentEntry;
163 // Is it a dot-directory or excluded?
164 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DIRECTORY-POINTER: fileInfoInstance->filename=%s,isDot=%d', $fileInfoInstance->getFilename(), intval($this->getDirectoryIteratorInstance()->isDot())));
165 while ($this->getDirectoryIteratorInstance()->valid() && ($this->getDirectoryIteratorInstance()->isDot() || in_array($fileInfoInstance->getFilename(), $except))) {
166 // Update current instance
167 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DIRECTORY-POINTER: fileInfoInstance->filename=%s,isDot=%d', $fileInfoInstance->getFilename(), intval($this->getDirectoryIteratorInstance()->isDot())));
168 $fileInfoInstance = $this->readRawDirectory();
171 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: Invoking directoryIteratorInstance->next() ...');
172 $this->getDirectoryIteratorInstance()->next();
177 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DIRECTORY-POINTER: fileInfoInstance[%s]=%s - EXIT!', gettype($fileInfoInstance), $fileInfoInstance));
178 return $fileInfoInstance;
182 * Close a directory source and set it's instance to null and the path name
187 public function closeDirectory () {
188 // Close the directory by unsetting it
189 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: CALLED!');
190 $this->unsetDirectoryIteratorInstance();
191 $this->setPathName('');
195 * Setter for the directory pointer
197 * @param $iteratorInstance An instanceof a DirectoryIterator class
200 protected final function setDirectoryIteratorInstance (DirectoryIterator $iteratorInstance) {
202 $this->iteratorInstance = $iteratorInstance;
206 * Getter for the directory pointer
208 * @return $iteratorInstance The directory pointer which shall be a valid directory resource
210 public final function getDirectoryIteratorInstance () {
211 return $this->iteratorInstance;
215 * Remove directory iterator instance (effectively closing it) by setting
216 * it to NULL. This will trigger a call on the destructor which will then
217 * "close" the iterator.
219 * @param $iteratorInstance An instanceof a DirectoryIterator class
222 protected final function unsetDirectoryIteratorInstance () {
223 // "Unset" the instance
224 $this->iteratorInstance = NULL;
228 * Setter for path name
230 * @param $pathName The new path name
233 protected final function setPathName (string $pathName) {
234 $this->pathName = $pathName;
238 * Getter for path name
240 * @return $pathName The current path name
242 public final function getPathName () {
243 return $this->pathName;