From 6e64aa2cb0fbd5a3c712a6a15e08399b1120cc67 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 6 Dec 2020 00:56:23 +0100 Subject: [PATCH] Continued: - validated more parameters to avoid bad invocation with invalid values - int $bytes = NULL was my default, SPL says 0, not NULL as default value - improved logger messages by adding type and all parameters MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../binary/class_BaseBinaryFile.php | 67 ++++++---- .../binary/index/class_IndexFile.php | 2 +- .../class_BaseAbstractFile.php | 16 ++- .../file_directories/class_BaseFileIo.php | 2 +- .../class_FrameworkRawFileInputPointer.php | 2 +- .../class_FrameworkTextFileInputPointer.php | 4 +- .../class_FrameworkFileInputOutputPointer.php | 115 +++++++++++------- .../io_stream/class_FileIoStream.php | 2 +- .../text/class_BaseTextFile.php | 2 +- .../text/input/csv/class_CsvInputFile.php | 52 +++----- .../main/classes/index/class_BaseIndex.php | 2 +- .../index/file_stack/class_FileStackIndex.php | 2 +- .../iterator/file/class_FileIterator.php | 2 +- .../output/console/class_ConsoleOutput.php | 2 +- .../console/class_DebugConsoleOutput.php | 2 +- .../debug/error/class_DebugErrorLogOutput.php | 2 +- .../output/debug/web/class_DebugWebOutput.php | 2 +- .../classes/output/web/class_WebOutput.php | 2 +- .../stacker/file/class_BaseFileStack.php | 14 ++- .../main/interfaces/io/class_Streamable.php | 2 +- .../io/pointer/class_InputPointer.php | 2 +- .../class_SeekableWritableFileIterator.php | 2 +- .../middleware/io/class_FileIoHandler.php | 2 +- 23 files changed, 167 insertions(+), 135 deletions(-) diff --git a/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php b/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php index 6ba224e4..4ea56c68 100644 --- a/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php +++ b/framework/main/classes/file_directories/binary/class_BaseBinaryFile.php @@ -409,15 +409,16 @@ abstract class BaseBinaryFile extends BaseAbstractFile { /** * Initializes this file class * - * @param $infoInstance An instance of a SplFileInfo class + * @param $fileInfoInstance An instance of a SplFileInfo class * @return void */ - protected function initFile (SplFileInfo $infoInstance) { + protected function initFile (SplFileInfo $fileInfoInstance) { // Get a file i/o pointer instance - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: infoInstance=%s - CALLED!', $infoInstance)); - $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($infoInstance)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: fileInfoInstance[%s]=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance)); + $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileInfoInstance)); // ... and set it here + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Setting pointerInstance=%s ...', $pointerInstance->__toString())); $this->setPointerInstance($pointerInstance); // Trace message @@ -431,10 +432,20 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @param $data Data to be written * @param $flushHeader Whether to flush the header (default: flush) * @return void + * @throws InvalidArgumentException If a parameter is invalid */ public function writeData (int $seekPosition, string $data, bool $flushHeader = true) { - // Write data at given position + // Validate parameter /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: seekPosition=%s,data()=%d,flushHeader=%d - CALLED!', $seekPosition, strlen($data), intval($flushHeader))); + if ($seekPosition < 0) { + // Invalid seek position + throw new InvalidArgumentException(sprintf('seekPosition=%d is not valid', $seekPosition)); + } elseif (empty($data)) { + // Empty data is invalid, too + throw new InvalidArgumentException('Parameter "data" is empty'); + } + + // Write data at given position $this->getPointerInstance()->writeAtPosition($seekPosition, $data); // Increment counter @@ -461,10 +472,17 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * * @param $length Length of the block * @return void + * @throws InvalidArgumentException If a parameter is invalid */ protected function markCurrentBlockAsEmpty (int $length) { - // Get current seek position + // Validate parameter //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%d - CALLED!', $length)); + if ($length < 1) { + // Length cannot below one + throw new InvalidArgumentException(sprintf('length=%d is not valid', $length)); + } + + // Get current seek position $currentPosition = $this->key(); // Now add it as gap entry @@ -491,12 +509,12 @@ abstract class BaseBinaryFile extends BaseAbstractFile { if ($this->isFileInitialized()) { // Some bytes has been written, so rewind to start of it. $rewindStatus = $this->rewind(); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('rewindStatus=%s', $rewindStatus)); // Is the rewind() call successfull? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: rewindStatus=%d', $rewindStatus)); if ($rewindStatus != 1) { // Something bad happened - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Could not rewind().')); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Could not rewind().'); } // Read file header @@ -507,7 +525,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile { } // Return result - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('isInitialized=%d - EXIT!', intval($isInitialized))); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isInitialized=%d - EXIT!', intval($isInitialized))); return $isInitialized; } @@ -517,11 +535,10 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @return $isInitialized Whether the file's size is zero */ public function isFileInitialized () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!'); - // Get it from iterator which holds the pointer instance. If false is returned + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!'); $fileSize = $this->size(); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('fileSize=%s', $fileSize)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: fileSize=%s', $fileSize)); /* * The returned file size should not be false or NULL as this means @@ -533,7 +550,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile { $isInitialized = ($fileSize > 0); // Return result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('isInitialized=%d - EXIT!', intval($isInitialized))); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isInitialized=%d - EXIT!', intval($isInitialized))); return $isInitialized; } @@ -543,9 +560,8 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @return void */ public function createFileHeader () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!'); - // The file's header should not be initialized here + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!'); assert(!$this->isFileHeaderInitialized()); // Simple flush file header which will create it. @@ -554,6 +570,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile { // Rewind seek position (to beginning of file) and update/flush file header $this->rewindUpdateSeekPosition(); + // Trace message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!'); } @@ -562,30 +579,32 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * * @param $type Type of the file * @return void + * @throws InvalidArgumentException If a parameter is empty */ public function preAllocateFile (string $type) { // Is it enabled? - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!'); - if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') { - // Not enabled - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Not pre-allocating file.')); - + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: type=%s - CALLED!', $type)); + if (empty($type)) { + // Empty type + throw new InvalidArgumentException('Parameter "type" is empty'); + } elseif (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') { // Don't continue here. + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Not pre-allocating file.')); return; } // Message to user - self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Pre-allocating file ...')); + self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Pre-allocating file ...'); // Calculate minimum length for one entry $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength(); // Calulcate seek position - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('minLengthEntry=%s', $minLengthEntry)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: minLengthEntry=%s', $minLengthEntry)); $seekPosition = $minLengthEntry * FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_count'); // Now simply write a NUL there. This will pre-allocate the file. - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('seekPosition=%s', $seekPosition)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: seekPosition=%d', $seekPosition)); $this->writeData($seekPosition, chr(0)); // Rewind seek position (to beginning of file) and update/flush file header @@ -623,7 +642,7 @@ abstract class BaseBinaryFile extends BaseAbstractFile { * @param $bytes Amount of bytes to read * @return $data Data read from file */ - public function read (int $bytes = NULL) { + public function read (int $bytes = 0) { // Call pointer instance return $this->getPointerInstance()->read($bytes); } diff --git a/framework/main/classes/file_directories/binary/index/class_IndexFile.php b/framework/main/classes/file_directories/binary/index/class_IndexFile.php index 6bcf23bb..c485275f 100644 --- a/framework/main/classes/file_directories/binary/index/class_IndexFile.php +++ b/framework/main/classes/file_directories/binary/index/class_IndexFile.php @@ -52,7 +52,7 @@ class IndexFile extends BaseBinaryFile implements Block { */ public final static function createIndexFile (SplFileInfo $fileInfoInstance, Block $blockInstance) { // Get a new instance - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: fileInfoInstance=%s,blockInstance=%s - CALLED!', $fileInfoInstance, $blockInstance->__toString())); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('INDEX-FILE: fileInfoInstance[%s]=%s,blockInstance=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance, $blockInstance->__toString())); $fileInstance = new IndexFile(); // Set block instance here for callbacks diff --git a/framework/main/classes/file_directories/class_BaseAbstractFile.php b/framework/main/classes/file_directories/class_BaseAbstractFile.php index 171ad3b7..f72d7753 100644 --- a/framework/main/classes/file_directories/class_BaseAbstractFile.php +++ b/framework/main/classes/file_directories/class_BaseAbstractFile.php @@ -150,6 +150,14 @@ abstract class BaseAbstractFile extends BaseFrameworkSystem implements FilePoint return $this->getPointerInstance()->getFileObject(); } + /** + * Getter for file's name + */ + public final function getFilename () { + // Invole file object's method + return $this->getFileObject()->getFilename(); + } + /** * Close a file source and set it's instance to null and the file name * to empty @@ -157,14 +165,12 @@ abstract class BaseAbstractFile extends BaseFrameworkSystem implements FilePoint * @return void */ public function closeFile () { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: CALLED!', __METHOD__, __LINE__)); - // Close down pointer instance as well by unsetting it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-ABSTRACT-FILE: CALLED!'); $this->unsetPointerInstance(); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__)); + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-ABSTRACT-FILE: EXIT!'); } /** diff --git a/framework/main/classes/file_directories/class_BaseFileIo.php b/framework/main/classes/file_directories/class_BaseFileIo.php index 6e95edd0..54ab1dbf 100644 --- a/framework/main/classes/file_directories/class_BaseFileIo.php +++ b/framework/main/classes/file_directories/class_BaseFileIo.php @@ -150,7 +150,7 @@ abstract class BaseFileIo extends BaseFrameworkSystem implements FilePointer, Cl * @param $whence Added to offset (default: only use offset to seek to) * @return $status Status of file seek: 0 = success, -1 = failed */ - public function seek ($offset, $whence = SEEK_SET) { + public function seek (int $offset, int $whence = SEEK_SET) { // Seek to position $status = $this->getFileObject()->fseek($offset, $whence); diff --git a/framework/main/classes/file_directories/input/raw/class_FrameworkRawFileInputPointer.php b/framework/main/classes/file_directories/input/raw/class_FrameworkRawFileInputPointer.php index 0a8b447e..5e3c658e 100644 --- a/framework/main/classes/file_directories/input/raw/class_FrameworkRawFileInputPointer.php +++ b/framework/main/classes/file_directories/input/raw/class_FrameworkRawFileInputPointer.php @@ -125,7 +125,7 @@ class FrameworkRawFileInputPointer extends BaseFileIo implements InputPointer { * @param $bytes Amount of bytes to read * @return $data Data read from file */ - public function read (int $bytes = NULL) { + public function read (int $bytes = 0) { // Try to read given characters $data = $this->getFileObject()->fread($bytes); diff --git a/framework/main/classes/file_directories/input/text/class_FrameworkTextFileInputPointer.php b/framework/main/classes/file_directories/input/text/class_FrameworkTextFileInputPointer.php index 642e555f..20e127f2 100644 --- a/framework/main/classes/file_directories/input/text/class_FrameworkTextFileInputPointer.php +++ b/framework/main/classes/file_directories/input/text/class_FrameworkTextFileInputPointer.php @@ -120,7 +120,7 @@ class FrameworkTextFileInputPointer extends BaseFileIo implements InputPointer { * @throws NullPointerException If the file pointer instance is not set by setFileObject() * @throws InvalidResourceException If there is no object being set */ - public function read (int $bytes = NULL) { + public function read (int $bytes = 0) { // Some sanity checks if (is_null($this->getFileObject())) { // Pointer not initialized @@ -131,7 +131,7 @@ class FrameworkTextFileInputPointer extends BaseFileIo implements InputPointer { } // Is $bytes set? - if (is_int($bytes)) { + if ($bytes > 0) { // Try to read given characters $data = $this->getFileObject()->fread($bytes); } else { diff --git a/framework/main/classes/file_directories/io/class_FrameworkFileInputOutputPointer.php b/framework/main/classes/file_directories/io/class_FrameworkFileInputOutputPointer.php index b67025c3..61db8a66 100644 --- a/framework/main/classes/file_directories/io/class_FrameworkFileInputOutputPointer.php +++ b/framework/main/classes/file_directories/io/class_FrameworkFileInputOutputPointer.php @@ -13,7 +13,9 @@ use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException; use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem; // Import SPL stuff +use \InvalidArgumentException; use \SplFileInfo; +use \UnexpectedValueException; /** * A class for reading files @@ -82,7 +84,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP if ((is_null($fileObject)) || ($fileObject === false)) { // Something bad happend throw new FileIoException($fileInstance->getPathname(), self::EXCEPTION_FILE_POINTER_INVALID); - } // END - if + } // Create new instance $pointerInstance = new FrameworkFileInputOutputPointer(); @@ -94,36 +96,14 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP return $pointerInstance; } - /** - * Validates file pointer and throws exceptions. This method does not return - * anything (not reliable) as this method checks the file pointer and on - * case of an error it throws an exception. If this method does not throw - * any exceptions, the file pointer seems to be fine. - * - * @return void - * @throws NullPointerException If the file pointer instance - * is not set by setFileObject() - * @todo Add more checks - */ - private function validateFilePointer () { - if (is_null($this->getFileObject())) { - // Pointer not initialized - throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); - } // END - if - - // All fine here - } - /** * Read 1024 bytes data from a file pointer * * @return mixed The result of fread() */ public function readFromFile () { - // Validate the pointer - $this->validateFilePointer(); - // Read data from the file pointer and return it + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-INPUT-OUTPUT-POINTER: CALLED!'); return $this->read(1024); } @@ -134,26 +114,52 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP * @return mixed Number of writes bytes or false on error */ public function writeToFile (string $dataStream) { - // Validate the pointer - $this->validateFilePointer(); + // Validate parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: dataStream(%d)=%s - CALLED!', strlen($dataStream), $dataStream)); + if (empty($dataStream)) { + // Empty dataStream + throw new InvalidArgumentException('Parameter "dataStream" is empty'); + } // Write data to the file pointer and return written bytes - return $this->getFileObject()->fwrite($dataStream, strlen($dataStream)); + $status = $this->getFileObject()->fwrite($dataStream, strlen($dataStream)); + + // Return status + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: status[%s]=%d - EXIT!', gettype($status), $status)); + return $status; } /** * Writes at given position by seeking to it. * * @param $seekPosition Seek position in file - * @param $data Data to be written + * @param $dataStream Data to be written * @return mixed Number of writes bytes or false on error + * @throws InvalidArgumentException If a parameter is not valid */ - public function writeAtPosition (int $seekPosition, string $data) { + public function writeAtPosition (int $seekPosition, string $dataStream) { + // Validate parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: seekPosition=%d,dataStream()=%s - CALLED!', $seekPosition, strlen($dataStream), $dataStream)); + if ($seekPosition < 0) { + // Invalid seek position + throw new InvalidArgumentException(sprintf('seekPosition=%d is not valid.', $seekPosition)); + } elseif (empty($dataStream)) { + // Empty dataStream + throw new InvalidArgumentException('Parameter "dataStream" is empty'); + } + // First seek to it - $this->seek($seekPosition); + if (!$this->seek($seekPosition)) { + // Could not seek + throw new InvalidArgumentException(sprintf('Could not seek to seekPosition=%d', $seekPosition)); + } // Then write the data at that position - return $this->writeToFile($data); + $status = $this->writeToFile($dataStream); + + // Return status + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: status[%s]=%d - EXIT!', gettype($status), $status)); + return $status; } /** @@ -162,10 +168,8 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP * @return $status Status of this operation */ public function rewind () { - // Validate the pointer - $this->validateFilePointer(); - // Rewind the pointer + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-INPUT-OUTPUT-POINTER: CALLED!'); return $this->getFileObject()->rewind(); } @@ -175,13 +179,22 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP * @param $seekPosition Seek position in file * @param $whence "Seek mode" (see http://de.php.net/fseek) * @return $status Status of this operation + * @throws InvalidArgumentException If a parameter is not valid */ - public function seek ($seekPosition, $whence = SEEK_SET) { - // Validate the pointer - $this->validateFilePointer(); + public function seek (int $seekPosition, int $whence = SEEK_SET) { + // Validate parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: seekPosition=%d,whence=%d - CALLED!', $seekPosition, $whence)); + if ($seekPosition < 0) { + // Invalid seek position + throw new InvalidArgumentException(sprintf('seekPosition=%d is not valid.', $seekPosition)); + } // Move the file pointer - return $this->getFileObject()->fseek($seekPosition, $whence); + $status = $this->getFileObject()->fseek($seekPosition, $whence); + + // Return status + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: status[%s]=%d - EXIT!', gettype($status), $status)); + return $status; } /** @@ -191,6 +204,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP */ public function readLine () { // Read whole line + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-INPUT-OUTPUT-POINTER: CALLED!'); return $this->read(); } @@ -199,13 +213,18 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP * * @param $bytes Amount of bytes to read * @return $data Data read from file + * @throws InvalidArgumentException If a parameter is invalid */ - public function read (int $bytes = NULL) { - // Validate the pointer - $this->validateFilePointer(); + public function read (int $bytes = 0) { + // Validatre parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: bytes=%d - CALLED!', $bytes)); + if ($bytes < 0) { + // Bytes cannot be lesser than zero + throw new InvalidArgumentException(sprintf('bytes=%d is not valid', $bytes)); + } // Is $bytes set? - if (is_int($bytes)) { + if ($bytes > 0) { // Try to read given characters $data = $this->getFileObject()->fread($bytes); } else { @@ -214,6 +233,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP } // Then return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: data[%s]=%s - EXIT!', gettype($data), $data)); return $data; } @@ -264,18 +284,21 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP * "Getter" for file size * * @return $fileSize Size of currently loaded file + * @throws UnexpectedValueException If $fileData does not contain "size" */ public function getFileSize () { - // Check if the pointer is still valid - $this->validateFilePointer(); - // Get file's data + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-INPUT-OUTPUT-POINTER: CALLED!'); $fileData = $this->getFileObject()->fstat(); // Make sure the required array key is there - assert(isset($fileData['size'])); + if (!isset($fileData['size'])) { + // Not valid array + throw new UnexpectedValueException(sprintf('fileData=%s has no element "size"', print_r($fileData, TRUE))); + } // Return size + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-INPUT-OUTPUT-POINTER: fileData[size]=%d - EXIT!', $fileData['size']); return $fileData['size']; } 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 1dfc59ce..d547e234 100644 --- a/framework/main/classes/file_directories/io_stream/class_FileIoStream.php +++ b/framework/main/classes/file_directories/io_stream/class_FileIoStream.php @@ -289,7 +289,7 @@ class FileIoStream extends BaseFrameworkSystem implements FileInputStreamer, Fil * @param $whence Added to offset (default: only use offset to seek to) * @return $status Status of file seek: 0 = success, -1 = failed */ - public function seek ($offset, $whence = SEEK_SET) { + public function seek (int $offset, int $whence = SEEK_SET) { $this->partialStub('offset=' . $offset . ',whence=' . $whence); } diff --git a/framework/main/classes/file_directories/text/class_BaseTextFile.php b/framework/main/classes/file_directories/text/class_BaseTextFile.php index 901b70b3..44e100b5 100644 --- a/framework/main/classes/file_directories/text/class_BaseTextFile.php +++ b/framework/main/classes/file_directories/text/class_BaseTextFile.php @@ -60,7 +60,7 @@ abstract class BaseTextFile extends BaseAbstractFile { * @param $whence Added to offset (default: only use offset to seek to) * @return $status Status of file seek: 0 = success, -1 = failed */ - public function seek ($offset, $whence = SEEK_SET) { + public function seek (int $offset, int $whence = SEEK_SET) { // Not possible in text files self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEXT-FILE: offset=' . $offset . ',whence=' . $whence); throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); diff --git a/framework/main/classes/file_directories/text/input/csv/class_CsvInputFile.php b/framework/main/classes/file_directories/text/input/csv/class_CsvInputFile.php index d4c84ae6..66fe6ed1 100644 --- a/framework/main/classes/file_directories/text/input/csv/class_CsvInputFile.php +++ b/framework/main/classes/file_directories/text/input/csv/class_CsvInputFile.php @@ -68,22 +68,16 @@ class CsvInputFile extends BaseInputTextFile implements CsvInputStreamer { * @return $lineArray An indexed array with the read line */ public function readCsvFileLine (string $columnSeparator) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] columnSeparator=%s - CALLED!', __METHOD__, __LINE__, $columnSeparator)); - // Read raw line + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] columnSeparator=%s - CALLED!', __METHOD__, __LINE__, $columnSeparator)); $data = $this->getPointerInstance()->readLine(); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] data()=%d', __METHOD__, __LINE__, strlen($data))); - // Parse data + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] data()=%d', __METHOD__, __LINE__, strlen($data))); $lineArray = $this->parseDataToIndexedArray($data, $columnSeparator); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] lineArray()=%d - EXIT!', __METHOD__, __LINE__, count($lineArray))); - // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] lineArray()=%d - EXIT!', __METHOD__, __LINE__, count($lineArray))); return $lineArray; } @@ -95,10 +89,8 @@ class CsvInputFile extends BaseInputTextFile implements CsvInputStreamer { * @return $lineArray An indexed array with the read line */ private function parseDataToIndexedArray (string $data, string $columnSeparator) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] data()=%d,columnSeparator=%s - CALLED!', __METHOD__, __LINE__, strlen($data), $columnSeparator)); - // Init return array + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] data()=%d,columnSeparator=%s - CALLED!', __METHOD__, __LINE__, strlen($data), $columnSeparator)); $lineArray = []; // Whether the parser reads a quoted string (which may contain the column separator again) @@ -112,65 +104,49 @@ class CsvInputFile extends BaseInputTextFile implements CsvInputStreamer { // "Cache" char $char = substr($data, $idx, 1); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] idx=%s,char=%s ...', __METHOD__, __LINE__, $idx, $char)); - // Is the column separator found and not within quotes? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] idx=%d,char=%s ...', __METHOD__, __LINE__, $idx, $char)); if (($isInQuotes === false) && ($char == $columnSeparator)) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Adding column=%s ...', __METHOD__, __LINE__, $column)); - // Add this line to the array + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Adding column=%s ...', __METHOD__, __LINE__, $column)); array_push($lineArray, $column); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] line[]=%d - After add!', __METHOD__, __LINE__, count($lineArray))); - // Clear variable ... + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] line[]=%d - After add!', __METHOD__, __LINE__, count($lineArray))); $column = ''; // ... and skip it continue; } elseif ($char == chr(34)) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] column=%s ...', __METHOD__, __LINE__, $column)); - // $column must be empty at this point if we are at starting quote + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] column=%s ...', __METHOD__, __LINE__, $column)); assert(($isInQuotes === true) || (empty($column))); // Double-quote found, so flip variable $isInQuotes = (!$isInQuotes); - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] isInQuotes=%d ...', __METHOD__, __LINE__, intval($isInQuotes))); - // Skip double-quote (escaping of them is not yet supported) + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] isInQuotes=%d ...', __METHOD__, __LINE__, intval($isInQuotes))); continue; } - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Adding char=%s ...', __METHOD__, __LINE__, $idx, $char)); - // Add char to column + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Adding char=%s ...', __METHOD__, __LINE__, $idx, $char)); $column .= $char; - } // END - for + } // Is there something outstanding? if (!empty($column)) { - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Adding column=%s ...', __METHOD__, __LINE__, $column)); - // Then don't forget this. :-) + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Adding column=%s ...', __METHOD__, __LINE__, $column)); array_push($lineArray, $column); // Debug message //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] line[]=%d - After add!', __METHOD__, __LINE__, count($lineArray))); - } // END - if - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] line[]=%d - EXIT!', __METHOD__, __LINE__, count($lineArray))); + } // Return it + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] line[]=%d - EXIT!', __METHOD__, __LINE__, count($lineArray))); return $lineArray; } diff --git a/framework/main/classes/index/class_BaseIndex.php b/framework/main/classes/index/class_BaseIndex.php index 0e2c415c..6f0c5a14 100644 --- a/framework/main/classes/index/class_BaseIndex.php +++ b/framework/main/classes/index/class_BaseIndex.php @@ -175,7 +175,7 @@ abstract class BaseIndex extends BaseFrameworkSystem { */ protected function initIndex (SplFileInfo $fileInfoInstance) { // Get a file i/o pointer instance for index file - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('fileInfoInstance=%s - CALLED!', $fileInfoInstance)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('fileInfoInstance[%s]=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance)); $fileInstance = ObjectFactory::createObjectByConfiguredName('index_file_class', array($fileInfoInstance, $this)); // Get iterator instance diff --git a/framework/main/classes/index/file_stack/class_FileStackIndex.php b/framework/main/classes/index/file_stack/class_FileStackIndex.php index 952f1afb..1a8783e4 100644 --- a/framework/main/classes/index/file_stack/class_FileStackIndex.php +++ b/framework/main/classes/index/file_stack/class_FileStackIndex.php @@ -54,7 +54,7 @@ class FileStackIndex extends BaseIndex implements IndexableStack, Registerable { */ public final static function createFileStackIndex (SplFileInfo $fileInfoInstance) { // Get a new instance - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: fileInfoInstance=%s - CALLED!', $fileInfoInstance)); + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: fileInfoInstance[%s]=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance)); $indexInstance = new FileStackIndex(); // Initialize index diff --git a/framework/main/classes/iterator/file/class_FileIterator.php b/framework/main/classes/iterator/file/class_FileIterator.php index 360996c2..98aae71f 100644 --- a/framework/main/classes/iterator/file/class_FileIterator.php +++ b/framework/main/classes/iterator/file/class_FileIterator.php @@ -163,7 +163,7 @@ class FileIterator extends BaseIterator implements SeekableWritableFileIterator * @param $bytes Amount of bytes to read * @return $data Data read from file */ - public function read (int $bytes = NULL) { + public function read (int $bytes = 0) { // Call block instance return $this->getBlockInstance()->read($bytes); } diff --git a/framework/main/classes/output/console/class_ConsoleOutput.php b/framework/main/classes/output/console/class_ConsoleOutput.php index 3debd0f0..9dcae976 100644 --- a/framework/main/classes/output/console/class_ConsoleOutput.php +++ b/framework/main/classes/output/console/class_ConsoleOutput.php @@ -118,7 +118,7 @@ class ConsoleOutput extends BaseOutput implements OutputStreamer { * @return $status Status of file seek: 0 = success, -1 = failed * @throws UnsupportedOperationException If this method is called */ - public function seek ($offset, $whence = SEEK_SET) { + public function seek (int $offset, int $whence = SEEK_SET) { self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONSOLE-OUTPUT: offset=' . $offset . ',whence=' . $whence); throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } diff --git a/framework/main/classes/output/debug/console/class_DebugConsoleOutput.php b/framework/main/classes/output/debug/console/class_DebugConsoleOutput.php index eeaa8394..7875e001 100644 --- a/framework/main/classes/output/debug/console/class_DebugConsoleOutput.php +++ b/framework/main/classes/output/debug/console/class_DebugConsoleOutput.php @@ -124,7 +124,7 @@ class DebugConsoleOutput extends BaseDebugOutput implements Debugger, OutputStre * @return $status Status of file seek: 0 = success, -1 = failed * @throws UnsupportedOperationException If this method is called */ - public function seek ($offset, $whence = SEEK_SET) { + public function seek (int $offset, int $whence = SEEK_SET) { self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEBUG-CONSOLE-OUTPUT: offset=' . $offset . ',whence=' . $whence); throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } diff --git a/framework/main/classes/output/debug/error/class_DebugErrorLogOutput.php b/framework/main/classes/output/debug/error/class_DebugErrorLogOutput.php index dea30153..cdbcf57c 100644 --- a/framework/main/classes/output/debug/error/class_DebugErrorLogOutput.php +++ b/framework/main/classes/output/debug/error/class_DebugErrorLogOutput.php @@ -122,7 +122,7 @@ class DebugErrorLogOutput extends BaseDebugOutput implements Debugger, OutputStr * @return $status Status of file seek: 0 = success, -1 = failed * @throws UnsupportedOperationException If this method is called */ - public function seek ($offset, $whence = SEEK_SET) { + public function seek (int $offset, int $whence = SEEK_SET) { self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEBUG-ERROR-LOG-OUTPUT: offset=' . $offset . ',whence=' . $whence); throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } diff --git a/framework/main/classes/output/debug/web/class_DebugWebOutput.php b/framework/main/classes/output/debug/web/class_DebugWebOutput.php index 6e448d7d..2f87522c 100644 --- a/framework/main/classes/output/debug/web/class_DebugWebOutput.php +++ b/framework/main/classes/output/debug/web/class_DebugWebOutput.php @@ -111,7 +111,7 @@ class DebugWebOutput extends BaseDebugOutput implements Debugger, OutputStreamer * @return $status Status of file seek: 0 = success, -1 = failed * @throws UnsupportedOperationException If this method is called */ - public function seek ($offset, $whence = SEEK_SET) { + public function seek (int $offset, int $whence = SEEK_SET) { self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DEBUG-WEB-OUTPUT: offset=' . $offset . ',whence=' . $whence); throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } diff --git a/framework/main/classes/output/web/class_WebOutput.php b/framework/main/classes/output/web/class_WebOutput.php index d9c8ac69..44e24e01 100644 --- a/framework/main/classes/output/web/class_WebOutput.php +++ b/framework/main/classes/output/web/class_WebOutput.php @@ -104,7 +104,7 @@ class WebOutput extends BaseOutput implements OutputStreamer, Registerable { * @return $status Status of file seek: 0 = success, -1 = failed * @throws UnsupportedOperationException If this method is called */ - public function seek ($offset, $whence = SEEK_SET) { + public function seek (int $offset, int $whence = SEEK_SET) { self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('WEB-OUTPUT: offset=' . $offset . ',whence=' . $whence); throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } diff --git a/framework/main/classes/stacker/file/class_BaseFileStack.php b/framework/main/classes/stacker/file/class_BaseFileStack.php index a5a2eaa6..abce939f 100644 --- a/framework/main/classes/stacker/file/class_BaseFileStack.php +++ b/framework/main/classes/stacker/file/class_BaseFileStack.php @@ -16,6 +16,7 @@ use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait; use Org\Mxchange\CoreFramework\Utils\String\StringUtils; // Import SPL stuff +use \InvalidArgumentException; use \SplFileInfo; use \UnexpectedValueException; @@ -217,15 +218,22 @@ abstract class BaseFileStack extends BaseStacker implements StackableFile { * @param $fileInfoInstance An instance of a SplFileInfo class * @param $type Type of this stack (e.g. url_source for URL sources) * @return void + * @throws InvalidArgumentException If a parameter is invalid * @todo Currently the stack file is not cached, please implement a memory-handling class and if enough RAM is found, cache the whole stack file. */ protected function initFileStack (SplFileInfo $fileInfoInstance, string $type) { + // Validate parameter + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: fileInfoInstance[%s]=%s,type=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance, $type)); + if (empty($type)) { + // Invalid parameter + throw new InvalidArgumentException('Parameter "type" is empty'); + } + // Get a stack file instance - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: fileInfoInstance=%s,type=%s - CALLED!', $fileInfoInstance, $type)); - $fileInstance = ObjectFactory::createObjectByConfiguredName('stack_file_class', array($fileInfoInstance, $this)); + $stackInstance = ObjectFactory::createObjectByConfiguredName('stack_file_class', array($fileInfoInstance, $this)); // Get iterator instance - $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_iterator_class', array($fileInstance)); + $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_iterator_class', array($stackInstance)); // Set iterator here $this->setIteratorInstance($iteratorInstance); diff --git a/framework/main/interfaces/io/class_Streamable.php b/framework/main/interfaces/io/class_Streamable.php index 1e2de933..3fe663d3 100644 --- a/framework/main/interfaces/io/class_Streamable.php +++ b/framework/main/interfaces/io/class_Streamable.php @@ -42,7 +42,7 @@ interface Streamable extends FrameworkInterface { * @param $whence Added to offset (default: only use offset to seek to) * @return $status Status of file seek: 0 = success, -1 = failed */ - function seek ($offset, $whence = SEEK_SET); + function seek (int $offset, int $whence = SEEK_SET); /** * Size of file stack diff --git a/framework/main/interfaces/io/pointer/class_InputPointer.php b/framework/main/interfaces/io/pointer/class_InputPointer.php index faa64157..b146de23 100644 --- a/framework/main/interfaces/io/pointer/class_InputPointer.php +++ b/framework/main/interfaces/io/pointer/class_InputPointer.php @@ -52,6 +52,6 @@ interface InputPointer extends StreamableInput, FilePointer { * is not set by setFileObject() * @throws InvalidResourceException If there is being set */ - function read (int $bytes = NULL); + function read (int $bytes = 0); } diff --git a/framework/main/interfaces/iterator/file/class_SeekableWritableFileIterator.php b/framework/main/interfaces/iterator/file/class_SeekableWritableFileIterator.php index b93ace41..4a497bf3 100644 --- a/framework/main/interfaces/iterator/file/class_SeekableWritableFileIterator.php +++ b/framework/main/interfaces/iterator/file/class_SeekableWritableFileIterator.php @@ -50,7 +50,7 @@ interface SeekableWritableFileIterator extends SeekableIterator { * @param $bytes Amount of bytes to read * @return $data Data read from file */ - function read (int $bytes); + function read (int $bytes = 0); /** * Analyzes entries in index file. This will count all found (and valid) diff --git a/framework/main/middleware/io/class_FileIoHandler.php b/framework/main/middleware/io/class_FileIoHandler.php index d145b3bb..d2a2a432 100644 --- a/framework/main/middleware/io/class_FileIoHandler.php +++ b/framework/main/middleware/io/class_FileIoHandler.php @@ -156,7 +156,7 @@ class FileIoHandler extends BaseMiddleware implements IoHandler { * @param $whence Added to offset (default: only use offset to seek to) * @return $status Status of file seek: 0 = success, -1 = failed */ - public function seek ($offset, $whence = SEEK_SET) { + public function seek (int $offset, int $whence = SEEK_SET) { $this->partialStub('offset=' . $offset . ',whence=' . $whence); } -- 2.39.2