Continued:
[core.git] / framework / main / classes / file_directories / io / class_FrameworkFileInputOutputPointer.php
index 03252adbe15feb4ebc577fadbd2a24774c1fe6a5..1217e83a4bcd63be9ad7f935f318ea820d13a32e 100644 (file)
@@ -1,15 +1,19 @@
 <?php
 // Own namespace
-namespace CoreFramework\Filesystem\Pointer;
+namespace Org\Mxchange\CoreFramework\Filesystem\Pointer;
 
 // Import framework stuff
-use CoreFramework\Bootstrap\FrameworkBootstrap;
-use CoreFramework\FileSystem\BaseFileIo;
-use CoreFramework\FileSystem\FileReadProtectedException;
-use CoreFramework\FileSystem\FileWriteProtectedException;
-use CoreFramework\Filesystem\PathWriteProtectedException;
-use CoreFramework\Generic\NullPointerException;
-use CoreFramework\Object\BaseFrameworkSystem;
+use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
+use Org\Mxchange\CoreFramework\FileSystem\BaseFileIo;
+use Org\Mxchange\CoreFramework\FileSystem\FileReadProtectedException;
+use Org\Mxchange\CoreFramework\FileSystem\FileWriteProtectedException;
+use Org\Mxchange\CoreFramework\Filesystem\PathWriteProtectedException;
+use Org\Mxchange\CoreFramework\Generic\NullPointerException;
+use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
+use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
+
+// Import SPL stuff
+use \SplFileInfo;
 
 /**
  * A class for reading files
@@ -48,46 +52,43 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         * Create a file pointer based on the given file. The file will also
         * be verified here.
         *
-        * @param       $fileName       The file name we shall pass to fopen()
+        * @param       $fileInstance   An instance of a SplFileInfo class
         * @return      void
-        * @throws      FileIsEmptyException            If the given file name is NULL or empty
         * @throws      FileReadProtectedException      If PHP cannot read an existing file
         * @throws      FileWriteProtectedException     If PHP cannot write an existing file
         * @throws      PathWriteProtectedException     If PHP cannot write to an existing path
         * @throws      FileIoException                         If fopen() returns not a file resource
         */
-       public static final function createFrameworkFileInputOutputPointer ($fileName) {
+       public static final function createFrameworkFileInputOutputPointer (SplFileInfo $fileInstance) {
                // Some pre-sanity checks...
-               if ((is_null($fileName)) || (empty($fileName))) {
-                       // No filename given
-                       throw new FileIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
-               } elseif (!FrameworkBootstrap::isReachableFilePath($fileName)) {
+               if (!FrameworkBootstrap::isReachableFilePath($fileInstance)) {
                        // File exists but cannot be read
-                       throw new FileIoException($fileName, self::EXCEPTION_FILE_NOT_REACHABLE);
-               } elseif ((!FrameworkBootstrap::isReadableFile($fileName)) && (file_exists($fileName))) {
+                       throw new FileIoException($fileInstance, self::EXCEPTION_FILE_NOT_REACHABLE);
+               } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && (file_exists($fileInstance))) {
                        // File exists but cannot be read
-                       throw new FileReadProtectedException($fileName, self::EXCEPTION_FILE_CANNOT_BE_READ);
-               } elseif ((file_exists($fileName)) && (!is_writable($fileName))) {
-                       // File exists but cannot be written
-                       throw new FileWriteProtectedException($fileName, self::EXCEPTION_FILE_CANNOT_BE_WRITTEN);
-               } elseif (!is_writable(dirname($fileName))) {
+                       throw new FileReadProtectedException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_READ);
+               } elseif (!is_writable($fileInstance->getPath())) {
                        // Path is not writable
-                       throw new PathWriteProtectedException($fileName, self::EXCEPTION_PATH_CANNOT_BE_WRITTEN);
+                       throw new PathWriteProtectedException($fileInstance, self::EXCEPTION_PATH_CANNOT_BE_WRITTEN);
+               } elseif (($fileInstance->isFile()) && (!$fileInstance->isWritable())) {
+                       // File exists but cannot be written
+                       throw new FileWriteProtectedException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_WRITTEN);
                }
 
                // Try to open a handler
-               $filePointer = fopen($fileName, 'c+b');
-               if ((is_null($filePointer)) || ($filePointer === false)) {
+               $fileObject = $fileInstance->openFile('c+b');
+
+               // Is it valid?
+               if ((is_null($fileObject)) || ($fileObject === false)) {
                        // Something bad happend
-                       throw new FileIoException($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
+                       throw new FileIoException($fileInstance->getPathname(), self::EXCEPTION_FILE_POINTER_INVALID);
                } // END - if
 
                // Create new instance
                $pointerInstance = new FrameworkFileInputOutputPointer();
 
-               // Set file pointer and file name
-               $pointerInstance->setPointer($filePointer);
-               $pointerInstance->setFileName($fileName);
+               // Set file object and file name
+               $pointerInstance->setFileObject($fileObject);
 
                // Return the instance
                return $pointerInstance;
@@ -101,17 +102,14 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         *
         * @return      void
         * @throws      NullPointerException    If the file pointer instance
-        *                                                                      is not set by setPointer()
-        * @throws      InvalidResourceException        If there is being set
+        *                                                                      is not set by setFileObject()
+        * @todo Add more checks
         */
        private function validateFilePointer () {
-               if (is_null($this->getPointer())) {
+               if (is_null($this->getFileObject())) {
                        // Pointer not initialized
                        throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
-               } elseif (!is_resource($this->getPointer())) {
-                       // Pointer is not a valid resource!
-                       throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
-               }
+               } // END - if
 
                // All fine here
        }
@@ -140,7 +138,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
                $this->validateFilePointer();
 
                // Write data to the file pointer and return written bytes
-               return fwrite($this->getPointer(), $dataStream, strlen($dataStream));
+               return $this->getFileObject()->fwrite($dataStream, strlen($dataStream));
        }
 
        /**
@@ -168,7 +166,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
                $this->validateFilePointer();
 
                // Rewind the pointer
-               return rewind($this->getPointer());
+               return $this->getFileObject()->rewind();
        }
 
        /**
@@ -183,7 +181,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
                $this->validateFilePointer();
 
                // Move the file pointer
-               return fseek($this->getPointer(), $seekPosition, $whence);
+               return $this->getFileObject()->fseek($seekPosition, $whence);
        }
 
        /**
@@ -209,10 +207,10 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
                // Is $bytes set?
                if (is_int($bytes)) {
                        // Try to read given characters
-                       $data = fread($this->getPointer(), $bytes);
+                       $data = $this->getFileObject()->fread($bytes);
                } else {
                        // Try to read whole line
-                       $data = fread($this->getPointer());
+                       $data = $this->getFileObject()->fgets();
                }
 
                // Then return it
@@ -272,7 +270,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
                $this->validateFilePointer();
 
                // Get file's data
-               $fileData = fstat($this->getPointer());
+               $fileData = $this->getFileObject()->fstat();
 
                // Make sure the required array key is there
                assert(isset($fileData['size']));