3 namespace Org\Mxchange\CoreFramework\Handler\Filesystem;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
8 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
9 use Org\Mxchange\CoreFramework\Handler\Stream\IoHandler;
10 use Org\Mxchange\CoreFramework\Middleware\BaseMiddleware;
11 use Org\Mxchange\CoreFramework\Traits\Streamer\File\Input\FileInputStreamerTrait;
12 use Org\Mxchange\CoreFramework\Traits\Streamer\File\Output\FileOutputStreamerTrait;
18 * This is a file IO handler. It handles reading from and writing to files.
19 * Missing paths in writing process will be automatically created.
21 * @author Roland Haeder <webmaster@shipsimu.org>
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.shipsimu.org
27 * This program is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program. If not, see <http://www.gnu.org/licenses/>.
40 class FileIoHandler extends BaseMiddleware implements IoHandler {
42 use FileInputStreamerTrait;
43 use FileOutputStreamerTrait;
46 * An instance of this class
48 private static $selfInstance = NULL;
51 * Protected constructor
55 private function __construct () {
56 // Call parent constructor
57 parent::__construct(__CLASS__);
60 self::$selfInstance = $this;
64 * Creates an instance of this class and prepares the IO system. This is
65 * being done by setting the default file IO class
67 * @return $ioInstance A prepared instance of FilIOHandler
69 public static final function createFileIoHandler () {
71 $ioHandler = new FileIoHandler();
73 // Set the *real* file IO instances (both the same)
74 $ioHandler->setInputStreamerInstance(ObjectFactory::createObjectByConfiguredName('file_input_class'));
75 $ioHandler->setOutputStreamerInstance(ObjectFactory::createObjectByConfiguredName('file_output_class'));
82 * Getter for an instance of this class
84 * @return $selfInstance An instance of this class
86 public static final function getSelfInstance () {
87 return self::$selfInstance;
91 * Saves streamed (that are mostly serialized objects) data to files or
94 * @param $infoInstance An instance of a SplFileInfo class
95 * @param $dataArray Array containing the compressor's extension and streamed data
97 * @throws UnsupportedOperationException If this method is called
99 public function saveFile (SplFileInfo $infoInstance, array $dataArray) {
100 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('infoInstance.pathname=' . $infoInstance->getPathname() . ',dataArray()=' . count($dataArray));
101 throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
105 * Saves a file with data by using the current output stream
107 * @param $infoInstance An instance of a SplFileInfo class
108 * @param $dataStream File data stream
109 * @param $objectInstance An instance of a FrameworkInterface class (default: NULL)
112 public function saveStreamToFile (SplFileInfo $infoInstance, string $dataStream, FrameworkInterface $objectInstance = NULL) {
113 // Default is this array
114 $className = $this->__toString();
116 // Is the object instance set?
117 if ($objectInstance instanceof FrameworkInterface) {
119 $className = $objectInstance->__toString();
122 // Prepare output array
128 // Send the infoInstance and dataArray to the output handler
129 $this->getOutputStreamerInstance()->saveFile($infoInstance, $dataArray);
132 /** Loads data from a file over the input handler
134 * @param $infoInstance An instance of a SplFileInfo class
135 * @return $array Array with the file contents
137 public function loadFileContents (SplFileInfo $infoInstance) {
138 // Read from the input handler
139 return $this->getInputStreamerInstance()->loadFileContents($infoInstance);
143 * Determines seek position
145 * @return $seekPosition Current seek position
148 public function determineSeekPosition () {
149 $this->partialStub();
153 * Seek to given offset (default) or other possibilities as fseek() gives.
155 * @param $offset Offset to seek to (or used as "base" for other seeks)
156 * @param $whence Added to offset (default: only use offset to seek to)
159 public function seek (int $offset, int $whence = SEEK_SET) {
160 $this->partialStub('offset=' . $offset . ',whence=' . $whence);
166 * @return $size Size (in bytes) of file
168 public function size () {
169 $this->partialStub();
173 * "Getter" for seek position
175 * @return $seekPosition Current seek position
178 public function getPosition () {
179 $this->partialStub();