]> git.mxchange.org Git - core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Fri, 22 Aug 2025 02:03:58 +0000 (04:03 +0200)
committerRoland Häder <roland@mxchange.org>
Fri, 22 Aug 2025 02:03:58 +0000 (04:03 +0200)
- split loadFileContents() up into loadFileContentsAsArray() as both have other
  purposes and returned types

framework/main/classes/database/backend/lfdb_legacy/class_CachedLocalFileDatabase.php
framework/main/classes/file_directories/io_stream/class_FileIoStream.php
framework/main/classes/file_directories/text/class_BaseTextFile.php
framework/main/interfaces/io/file/class_FileInputStreamer.php
framework/main/middleware/io/class_FileIoHandler.php

index cea1d5e66ccf6e3f9f35dba6bf91ebcce05c9925..a3f5648d453fcbd6a2f999bc86cd44b7eeb9655e 100644 (file)
@@ -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)));
index 6c31d0ab7c5a61d9dd7c2cb2f828ab60597a2c03..3ab47301e54390f910e32c3f701142d6278c2f9e 100644 (file)
@@ -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?
index 5637f5cbf81fb7d75b47ce0d1b7b9b221176a1db..5b32851c64ee6b74c39f56a8e473de5561e2c852 100644 (file)
@@ -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);
+       }
+
 }
index 8676e3701d016ff1f8c04f4d91041f053ca191e3..23bfbdba6d34ad7fd3e34010afd7de1d1586d9dd 100644 (file)
@@ -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;
+
 }
index f122923d833759aa046ff715411f706250ddea9a..da1f0dafad7dfc232b767a7ea0505ecc732722ee 100644 (file)
@@ -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
         *