Rewritten:
[core.git] / framework / main / classes / file_directories / io / class_FrameworkFileInputOutputPointer.php
index e1a0b59..c9c9ca5 100644 (file)
@@ -12,6 +12,9 @@ use CoreFramework\Generic\NullPointerException;
 use CoreFramework\Generic\UnsupportedOperationException;
 use CoreFramework\Object\BaseFrameworkSystem;
 
+// Import SPL stuff
+use \SplFileInfo;
+
 /**
  * A class for reading files
  *
@@ -49,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))) {
+                       throw new FileReadProtectedException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_READ);
+               } elseif (($fileInstance->isFile()) && (!$fileInstance->isWritable())) {
                        // File exists but cannot be written
-                       throw new FileWriteProtectedException($fileName, self::EXCEPTION_FILE_CANNOT_BE_WRITTEN);
-               } elseif (!is_writable(dirname($fileName))) {
+                       throw new FileWriteProtectedException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_WRITTEN);
+               } 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);
                }
 
                // 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;
@@ -103,16 +103,13 @@ 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
+        * @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
        }
@@ -141,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));
        }
 
        /**
@@ -169,7 +166,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
                $this->validateFilePointer();
 
                // Rewind the pointer
-               return rewind($this->getPointer());
+               return $this->getFileObject()->rewind();
        }
 
        /**
@@ -184,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);
        }
 
        /**
@@ -210,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
@@ -273,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']));