Rewritten:
[core.git] / framework / main / classes / file_directories / io / class_FrameworkFileInputOutputPointer.php
index ee070c7c570d0cc8df7339147d4a7055085c7d20..c9c9ca54d651dec37d275715684cbc8c29253fec 100644 (file)
@@ -3,10 +3,18 @@
 namespace 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\Generic\UnsupportedOperationException;
 use CoreFramework\Object\BaseFrameworkSystem;
 
+// Import SPL stuff
+use \SplFileInfo;
+
 /**
  * A class for reading files
  *
@@ -44,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 (!BaseFrameworkSystem::isReachableFilePath($fileName)) {
+               if (!FrameworkBootstrap::isReachableFilePath($fileInstance)) {
                        // File exists but cannot be read
-                       throw new FileIoException($fileName, self::EXCEPTION_FILE_NOT_REACHABLE);
-               } elseif ((!BaseFrameworkSystem::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;
@@ -98,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
        }
@@ -129,14 +131,14 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         * Write data to a file pointer
         *
         * @param       $dataStream             The data stream we shall write to the file
-        * @return      mixed                   Number of writes bytes or FALSE on error
+        * @return      mixed                   Number of writes bytes or false on error
         */
        public function writeToFile ($dataStream) {
                // Validate the pointer
                $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));
        }
 
        /**
@@ -144,7 +146,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
         *
         * @param       $seekPosition   Seek position in file
         * @param       $data                   Data to be written
-        * @return      mixed                   Number of writes bytes or FALSE on error
+        * @return      mixed                   Number of writes bytes or false on error
         */
        public function writeAtPosition ($seekPosition, $data) {
                // First seek to it
@@ -164,7 +166,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
                $this->validateFilePointer();
 
                // Rewind the pointer
-               return rewind($this->getPointer());
+               return $this->getFileObject()->rewind();
        }
 
        /**
@@ -179,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);
        }
 
        /**
@@ -205,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
@@ -239,7 +241,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
 
        /**
         * Checks wether the current entry is valid (not at the end of the file).
-        * This method will return TRUE if an emptied (nulled) entry has been found.
+        * This method will return true if an emptied (nulled) entry has been found.
         *
         * @return      $isValid        Whether the next entry is valid
         * @throws      UnsupportedOperationException   If this method is called
@@ -268,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']));