]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/file_directories/class_BaseAbstractFile.php
Continued:
[core.git] / framework / main / classes / file_directories / class_BaseAbstractFile.php
index b867067b0b821b0abd0bf586838a41e563b58935..2e0aefe49594de36f6adc9b54e80881d87d368b2 100644 (file)
@@ -1,18 +1,23 @@
 <?php
 // Own namespace
-namespace CoreFramework\Filesystem\File;
+namespace Org\Mxchange\CoreFramework\Filesystem\File;
 
 // Import framework stuff
-use CoreFramework\Filesystem\CloseableFile;
-use CoreFramework\Filesystem\FilePointer;
-use CoreFramework\Object\BaseFrameworkSystem;
+use Org\Mxchange\CoreFramework\Filesystem\CloseableFile;
+use Org\Mxchange\CoreFramework\Filesystem\FilePointer;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
+use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
+use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
+
+// Import SPL stuff
+use \InvalidArgumentException;
 
 /**
  * An abstract file class
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -29,16 +34,16 @@ use CoreFramework\Object\BaseFrameworkSystem;
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, CloseableFile {
+abstract class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, CloseableFile {
        /**
         * Counter for total entries
         */
        private $totalEntries = 0;
 
        /**
-        * The current file we are working in
+        * An instance of a file I/O pointer class (not handler)
         */
-       private $fileName = '';
+       private $pointerInstance = NULL;
 
        /**
         * Protected constructor
@@ -46,9 +51,52 @@ class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, Close
         * @param       $className      Name of the class
         * @return      void
         */
-       protected function __construct ($className) {
+       protected function __construct (string $className) {
                // Call parent constructor
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-ABSTRACT-FILE: className=%s - CONSTRUCTED!', $className));
                parent::__construct($className);
+
+               // Trace message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: EXIT!');
+       }
+
+       /**
+        * Setter for FilePointer instance
+        *
+        * @param       $pointerInstance        An instance of an FilePointer class
+        * @return      void
+        */
+       protected final function setPointerInstance (FilePointer $pointerInstance = NULL) {
+               $this->pointerInstance = $pointerInstance;
+       }
+
+       /**
+        * Getter for FilePointer instance
+        *
+        * @return      $pointerInstance        An instance of an FilePointer class
+        */
+       public final function getPointerInstance () {
+               return $this->pointerInstance;
+       }
+
+       /**
+        * Unsets pointer instance which triggers a call of __destruct() if the
+        * instance is still there. This is surely not fatal on already "closed"
+        * file pointer instances.
+        *
+        * I don't want to mess around with above setter by giving it a default
+        * value NULL as setter should always explicitly only set (existing) object
+        * instances and NULL is NULL.
+        *
+        * @return      void
+        */
+       protected final function unsetPointerInstance () {
+               // Simply invoke setter with no parameter
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
+               $this->setPointerInstance();
+
+               // Trace message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: EXIT!');
        }
 
        /**
@@ -58,10 +106,14 @@ class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, Close
         */
        public final function __destruct() {
                // Try to close a file
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: DESTRUCTED!');
                $this->closeFile();
 
                // Call the parent destructor
                parent::__destruct();
+
+               // Trace message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: EXIT!');
        }
 
        /**
@@ -71,6 +123,7 @@ class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, Close
         */
        public function getFileSize () {
                // Call pointer instanze
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
                return $this->getPointerInstance()->getFileSize();
        }
 
@@ -81,6 +134,7 @@ class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, Close
         */
        public final function getCounter () {
                // Get it
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-ABSTRACT-FILE: Getting this->totalEntries=%d ... - CALLED!', $this->totalEntries));
                return $this->totalEntries;
        }
 
@@ -90,8 +144,9 @@ class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, Close
         * @param       $totalEntries   Total entries in this file
         * @return      void
         */
-       protected final function setCounter ($counter) {
+       protected final function setCounter (int $counter) {
                // Set it
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-ABSTRACT-FILE: Setting this->totalEntries=%d ... - CALLED!', $counter));
                $this->totalEntries = $counter;
        }
 
@@ -101,38 +156,32 @@ class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, Close
         * @return      void
         */
        protected final function incrementCounter () {
-               // Get it
+               // Count up
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
                $this->totalEntries++;
-       }
 
-       /**
-        * Getter for the file pointer
-        *
-        * @return      $filePointer    The file pointer which shall be a valid file resource
-        * @throws      UnsupportedOperationException   If this method is called
-        */
-       public final function getPointer () {
-               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+               // Trace message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: EXIT!');
        }
 
        /**
-        * Setter for file name
+        * Getter for the file object
         *
-        * @param       $fileName       The new file name
-        * @return      void
+        * @return      $fileObject             An instance of a SplFileObject
         */
-       protected final function setFileName ($fileName) {
-               $fileName = (string) $fileName;
-               $this->fileName = $fileName;
+       public final function getFileObject () {
+               // Call pointer instanze
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
+               return $this->getPointerInstance()->getFileObject();
        }
 
        /**
-        * Getter for file name
-        *
-        * @return      $fileName       The current file name
+        * Getter for file's name
         */
-       public final function getFileName () {
-               return $this->fileName;
+       public final function getFilename () {
+               // Invole file object's method
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
+               return $this->getFileObject()->getFilename();
        }
 
        /**
@@ -142,17 +191,12 @@ class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, Close
         * @return      void
         */
        public function closeFile () {
-               // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileName()));
-
                // Close down pointer instance as well by unsetting it
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
                $this->unsetPointerInstance();
 
-               // Remove file name
-               $this->setFileName('');
-
-               // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
+               // Trace message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: EXIT!');
        }
 
        /**
@@ -163,6 +207,7 @@ class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, Close
         */
        public function size () {
                // Call pointer instance
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
                return $this->getPointerInstance()->size();
        }
 
@@ -171,11 +216,12 @@ class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, Close
         *
         * @return      mixed   The result of fread()
         * @throws      NullPointerException    If the file pointer instance
-        *                                                                      is not set by setPointer()
+        *                                                                      is not set by setFileObject()
         * @throws      InvalidResourceException        If there is being set
         */
        public function readFromFile () {
                // Call pointer instance
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
                return $this->getPointerInstance()->readFromFile();
        }
 
@@ -185,11 +231,18 @@ class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, Close
         * @param       $dataStream             The data stream we shall write to the file
         * @return      mixed                   Number of writes bytes or false on error
         * @throws      NullPointerException    If the file pointer instance
-        *                                                                      is not set by setPointer()
+        *                                                                      is not set by setFileObject()
         * @throws      InvalidResourceException        If there is being set
         *                                                                                      an invalid file resource
         */
-       public function writeToFile ($dataStream) {
+       public function writeToFile (string $dataStream) {
+               // Check parameter
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-ABSTRACT-FILE: dataStream()=%d - CALLED!', strlen($dataStream)));
+               if (empty($dataStream)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "dataStream" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               }
+
                // Call pointer instance
                return $this->getPointerInstance()->writeToFile($dataStream);
        }
@@ -201,6 +254,7 @@ class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, Close
         */
        public final function isEndOfFileReached () {
                // Call pointer instance
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
                return $this->getPointerInstance()->isEndOfFileReached();
        }