X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=framework%2Fmain%2Fclasses%2Ffile_directories%2Fio%2Fclass_FrameworkFileInputOutputPointer.php;h=1217e83a4bcd63be9ad7f935f318ea820d13a32e;hp=03252adbe15feb4ebc577fadbd2a24774c1fe6a5;hb=868c877607670760eb36e63ebeb1a04237907be9;hpb=2c22024e1c9ac0aef5bb8c347326192af8826ae5 diff --git a/framework/main/classes/file_directories/io/class_FrameworkFileInputOutputPointer.php b/framework/main/classes/file_directories/io/class_FrameworkFileInputOutputPointer.php index 03252adb..1217e83a 100644 --- a/framework/main/classes/file_directories/io/class_FrameworkFileInputOutputPointer.php +++ b/framework/main/classes/file_directories/io/class_FrameworkFileInputOutputPointer.php @@ -1,15 +1,19 @@ 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']));