3 * This is a file IO handler. It handles reading from and writing to files.
4 * Missing paths in writing process will be automatically created.
6 class FileIOHandler extends BaseMiddleware {
8 * The *real* file input class we shall use for reading data
10 private $inputStream = null;
13 * The *real* file output class we shall use for reading data
15 private $outputStream = null;
18 * An instance of this class
20 private static $thisInstance = null;
27 private function __construct () {
28 // Call parent constructor
29 parent::constructor(__CLASS__);
32 $this->setPartDescr("Datei-Ein-/Ausgabe-Handler");
34 // Create an unique ID
35 $this->createUniqueID();
38 $this->removeNumberFormaters();
39 $this->removeSystemArray();
42 self::$thisInstance = $this;
46 * Creates an instance of this class and prepares the IO system. This is
47 * being done by setting the default file IO class
49 * @return $ioInstance A prepared instance of FilIOHandler
51 public final static function createFileIOHandler () {
53 $ioHandler = new FileIOHandler();
55 // Set the *real* file IO instances (both the same)
56 $ioHandler->setInputStream(FileIOStream::createFileIOStream());
57 $ioHandler->setOutputStream(FileIOStream::createFileIOStream());
64 * Getter for an instance of this class
66 * @return $thisInstance An instance of this class
68 public final static function getInstance () {
69 return self::$thisInstance;
73 * Setter for the *real* file input instance
75 * @param $inputStream The *real* file-input class
77 public final function setInputStream (FileInputStreamer $inputStream) {
78 $this->inputStream = $inputStream;
82 * Getter for the *real* file input instance
84 * @return $inputStream The *real* file-input class
86 public final function getInputStream () {
87 return $this->inputStream;
91 * Setter for the *real* file output instance
93 * @param $outputStream The *real* file-output class
95 public final function setOutputStream (FileOutputStreamer $outputStream) {
96 $this->outputStream = $outputStream;
100 * Getter for the *real* file output instance
102 * @return $outputStream The *real* file-output class
104 public final function getOutputStream () {
105 return $this->outputStream;
109 * Saves a file with data by using the current output stream
111 * @see FileOutputStreamer
113 public function saveFile ($fileName, $dataArray) {
115 $outInstance = $this->getOutputStream();
117 // Is it a valid stream?
118 if (is_null($outInstance)) {
120 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
121 } elseif (!is_object($outInstance)) {
122 // Not an object! ;-(
123 throw new NoObjectException($outInstance, self::EXCEPTION_IS_NO_OBJECT);
124 } elseif (!method_exists($outInstance, 'saveFile')) {
125 // Nope, so throw exception
126 throw new MissingMethodException(array($outInstance, 'saveFile'), self::EXCEPTION_MISSING_METHOD);
129 // Send the fileName and dataArray to the output handler
130 $outInstance->saveFile($fileName, $dataArray);
133 /** Loads data from a file over the input handler
135 * @see FileInputStreamer
137 public function loadFileContents ($fqfn) {
138 // Initialize the array
142 $inInstance = $this->getInputStream();
144 // Is it a valid stream?
145 if (is_null($inInstance)) {
147 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
148 } elseif (!is_object($inInstance)) {
149 // Not an object! ;-(
150 throw new NoObjectException($inInstance, self::EXCEPTION_IS_NO_OBJECT);
151 } elseif (!method_exists($inInstance, 'loadFileContents')) {
152 // Nope, so throw exception
153 throw new MissingMethodException(array($inInstance, 'loadFileContents'), self::EXCEPTION_MISSING_METHOD);
156 // Read from the input handler
157 return $inInstance->loadFileContents($fqfn);