class_FrameworkFileOutputPointer.php

Go to the documentation of this file.
00001 <?php
00024 class FrameworkFileOutputPointer extends BaseFrameworkSystem {
00028         private $fileName = "";
00029 
00033         private $filePointer = null;
00034 
00038         protected function __construct () {
00039                 // Call parent constructor
00040                 parent::__construct(__CLASS__);
00041 
00042                 // Clean-up a little
00043                 $this->removeNumberFormaters();
00044                 $this->removeSystemArray();
00045         }
00046 
00050         public final function __destruct() {
00051                 // Is there a resource pointer? Then we have to close the file here!
00052                 if (is_resource($this->getPointer())) {
00053                         // Try to close a file
00054                         $this->closeFile();
00055                 }
00056 
00057                 // Call the parent destructor
00058                 parent::__destruct();
00059         }
00060 
00072         public final static function createFrameworkFileOutputPointer ($fileName, $mode) {
00073                 // Some pre-sanity checks...
00074                 if (is_null($fileName)) {
00075                         // No filename given
00076                         throw new FileIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
00077                 }
00078 
00079                 // Try to open a handler
00080                 $filePointer = @fopen($fileName, $mode);
00081                 if (($filePointer === null) || ($filePointer === false)) {
00082                         // Something bad happend
00083                         throw new FilePointerNotOpenedException ($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
00084                 }
00085 
00086                 // Create new instance
00087                 $pointerInstance = new FrameworkFileOutputPointer();
00088 
00089                 // Set file pointer and file name
00090                 $pointerInstance->setPointer($filePointer);
00091                 $pointerInstance->setFileName($fileName);
00092 
00093                 // Return the instance
00094                 return $pointerInstance;
00095         }
00096 
00107         public function writeToFile ($dataStream) {
00108                 if (is_null($this->getPointer())) {
00109                         // Pointer not initialized
00110                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
00111                 } elseif (!is_resource($this->getPointer())) {
00112                         // Pointer is not a valid resource!
00113                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
00114                 }
00115 
00116                 // Read data from the file pointer and return it
00117                 return fwrite($this->getPointer(), $dataStream);
00118         }
00119 
00129         public function closeFile () {
00130                 if (is_null($this->getPointer())) {
00131                         // Pointer not initialized
00132                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
00133                 } elseif (!is_resource($this->getPointer())) {
00134                         // Pointer is not a valid resource!
00135                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
00136                 }
00137 
00138                 // Close the file pointer and reset the instance variable
00139                 @fclose($this->getPointer());
00140                 $this->setPointer(null);
00141                 $this->setFileName("");
00142         }
00143 
00150         public final function setPointer ($filePointer) {
00151                 // Sanity-check if pointer is a valid file resource
00152                 if (is_resource($filePointer) || is_null($filePointer)) {
00153                         // Is a valid resource
00154                         $this->filePointer = $filePointer;
00155                 } else {
00156                         // Throw exception
00157                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
00158                 }
00159         }
00160 
00167         public final function getPointer () {
00168                 return $this->filePointer;
00169         }
00170 
00177         public final function setFileName ($fileName) {
00178                 $fileName = (string) $fileName;
00179                 $this->fileName = $fileName;
00180         }
00181 
00187         public final function getFileName () {
00188                 return $this->fileName;
00189         }
00190 }
00191 
00192 // [EOF]
00193 ?>

Generated on Mon Dec 8 01:06:46 2008 for Ship-Simulator by  doxygen 1.5.6