From ace9b7397624a6b2defb4b9ecdf37b515314c109 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 22 Aug 2025 04:03:58 +0200 Subject: [PATCH] Continued: - split loadFileContents() up into loadFileContentsAsArray() as both have other purposes and returned types --- .../class_CachedLocalFileDatabase.php | 2 +- .../io_stream/class_FileIoStream.php | 28 +++++++++++++++---- .../text/class_BaseTextFile.php | 17 +++++++++-- .../io/file/class_FileInputStreamer.php | 10 +++++++ .../middleware/io/class_FileIoHandler.php | 15 +++++++++- 5 files changed, 62 insertions(+), 10 deletions(-) diff --git a/framework/main/classes/database/backend/lfdb_legacy/class_CachedLocalFileDatabase.php b/framework/main/classes/database/backend/lfdb_legacy/class_CachedLocalFileDatabase.php index cea1d5e6..a3f5648d 100644 --- a/framework/main/classes/database/backend/lfdb_legacy/class_CachedLocalFileDatabase.php +++ b/framework/main/classes/database/backend/lfdb_legacy/class_CachedLocalFileDatabase.php @@ -196,7 +196,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac private function getDataArrayFromFile (SplFileInfo $infoInstance): array { // Init compressed data //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CACHED-LOCAL-FILE-DATABASE: infoInstance=%s - CALLED!', $infoInstance->__toString())); - $compressedData = $this->getFileIoInstance()->loadFileContents($infoInstance); + $compressedData = $this->getFileIoInstance()->loadFileContentsAsArray($infoInstance); // Is it valid? //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('CACHED-LOCAL-FILE-DATABASE: compressedData[]=%s', gettype($compressedData))); diff --git a/framework/main/classes/file_directories/io_stream/class_FileIoStream.php b/framework/main/classes/file_directories/io_stream/class_FileIoStream.php index 6c31d0ab..3ab47301 100644 --- a/framework/main/classes/file_directories/io_stream/class_FileIoStream.php +++ b/framework/main/classes/file_directories/io_stream/class_FileIoStream.php @@ -175,9 +175,6 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-IO-STREAM: infoInstance=%s - CALLED!', $infoInstance)); $inputBuffer = ''; $lastBuffer = ''; - $header = []; - $data = []; - $readData = ''; // This will contain our read data // Get a file input handler $fileInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_class', array($infoInstance)); @@ -204,6 +201,26 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('FILE-IO-STREAM: Closing file ...'); unset($fileInstance); + // Return read raw data + return $inputBuffer; + } + + /** + * Reads from a local file as array + * + * @param $infoInstance An instance of a SplFileInfo class + * @return $array An array with the element 'header' and 'data + * @see FileInputStreamer + */ + public final function loadFileContentsAsArray (SplFileInfo $infoInstance): array { + // Invoke above method + $inputBuffer = $this->loadFileContents($infoInstance); + + // Initialize some arrays + $readData = ''; + $header = []; + $data = []; + // Convert it into an array /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('FILE-IO-STREAM: Read inputBuffer=%d bytes from infoInstance=%s', strlen($inputBuffer), $infoInstance)); $inputArray = explode(chr(10), $inputBuffer); @@ -270,9 +287,8 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil // Was raw lines read and no header/data? /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('FILE-IO-STREAM: readData()=%d,header()=%d,data()=%d', strlen($readData), count($header), count($data))); if ((!empty($readData)) && (count($header) == 0) && (count($data) == 0)) { - // Return raw lines back - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-IO-STREAM: readData()=%d - EXIT!', strlen($readData))); - return $readData; + // Abort here @TODO + throw new InvalidEncodedFileException(); } // Was a header found? diff --git a/framework/main/classes/file_directories/text/class_BaseTextFile.php b/framework/main/classes/file_directories/text/class_BaseTextFile.php index 5637f5cb..5b32851c 100644 --- a/framework/main/classes/file_directories/text/class_BaseTextFile.php +++ b/framework/main/classes/file_directories/text/class_BaseTextFile.php @@ -72,8 +72,6 @@ abstract class BaseTextFile extends BaseAbstractFile { * * @param $infoInstance An instance of a SplFileInfo class * @return $content Raw file content - * @throws InvalidArrayCountException If an array has not the expected size - * @throws InvalidMD5ChecksumException If two MD5 hashes did not match */ public function loadFileContents (SplFileInfo $infoInstance): string { /* @@ -84,4 +82,19 @@ abstract class BaseTextFile extends BaseAbstractFile { throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION); } + /** + * Reads from a local or remote file as array + * + * @param $infoInstance An instance of a SplFileInfo class + * @return $array An array with the element 'header' and 'data + */ + public function loadFileContentsAsArray (SplFileInfo $infoInstance): array { + /* + * This class (or its implementations) are special file readers/writers. + * There is no need to read/write the whole file. + */ + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEXT-FILE: infoInstance=' . $infoInstance); + throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION); + } + } diff --git a/framework/main/interfaces/io/file/class_FileInputStreamer.php b/framework/main/interfaces/io/file/class_FileInputStreamer.php index 8676e370..23bfbdba 100644 --- a/framework/main/interfaces/io/file/class_FileInputStreamer.php +++ b/framework/main/interfaces/io/file/class_FileInputStreamer.php @@ -41,4 +41,14 @@ interface FileInputStreamer extends StreamableInput { */ function loadFileContents (SplFileInfo $infoInstance): string; + /** + * Reads from a local or remote file and returns an array with 'header' and 'data' elements + * + * @param $infoInstance An instance of a SplFileInfo class + * @return $array An array with the element 'header' and 'data + * @throws InvalidArrayCountException If an array has not the expected size + * @throws InvalidMD5ChecksumException If two MD5 hashes did not match + */ + function loadFileContentsAsArray (SplFileInfo $infoInstance): array; + } diff --git a/framework/main/middleware/io/class_FileIoHandler.php b/framework/main/middleware/io/class_FileIoHandler.php index f122923d..da1f0daf 100644 --- a/framework/main/middleware/io/class_FileIoHandler.php +++ b/framework/main/middleware/io/class_FileIoHandler.php @@ -150,7 +150,8 @@ class FileIoHandler extends BaseMiddleware implements IoHandler { /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-IO-HANDLER: EXIT!'); } - /** Loads data from a file over the input handler + /** + * Loads data from a file over the input handler * * @param $infoInstance An instance of a SplFileInfo class * @return $content Raw file content @@ -161,6 +162,18 @@ class FileIoHandler extends BaseMiddleware implements IoHandler { return $this->getInputStreamerInstance()->loadFileContents($infoInstance); } + /** + * Loads data from a file over the input handler as array + * + * @param $infoInstance An instance of a SplFileInfo class + * @return $array An array with the element 'header' and 'data + */ + public function loadFileContentsAsArray (SplFileInfo $infoInstance): array { + // Read from the input handler + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-IO-HANDLER: infoInstance=%s - CALLED!', $infoInstance->__toString())); + return $this->getInputStreamerInstance()->loadFileContentsAsArray($infoInstance); + } + /** * Determines seek position * -- 2.39.5