* @version 0.0.0 * @copyright Copyright (c) 2023 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class FiFoFileStack extends BaseFileStack implements StackableFile, CalculatableBlock, Registerable { /** * Protected constructor * * @return void */ private function __construct () { // Call parent constructor parent::__construct(__CLASS__); } /** * Creates an instance of this class * * @param $fileInfoInstance An instance of a SplFileInfo class * @param $type Type of this stack (e.g. url_source for URL sources) * @return $stackInstance An instance of a StackableFile class * @throws InvalidArgumentException If a parameter is invalid */ public final static function createFiFoFileStack (SplFileInfo $fileInfoInstance, string $type) { // Validate parameter /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: fileInfoInstance[%s]=%s,type=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance, $type)); if (empty($type)) { // No empty type throw new InvalidArgumentException('Parameter "type" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } // Get new instance $stackInstance = new FiFoFileStack(); // Init this stack /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: Invoking stackInstance->initFileStack([%s]=%s,%s) ...', get_class($fileInfoInstance), $fileInfoInstance, $type)); $stackInstance->initFileStack($fileInfoInstance, $type); // Return the prepared instance /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackInstance=%s - EXIT!', $stackInstance->__toString())); return $stackInstance; } /** * Pushs a value on a named stacker * * @param $stackerName Name of the stack * @param $value Value to push on it * @return void * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If the stack is full */ public function pushNamed (string $stackerName, $value) { // Validate parameter /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackerName=%s,value[]=%s - CALLED!', $stackerName, gettype($value))); if (empty($stackerName)) { // No empty stack name throw new InvalidArgumentException('Parameter "stackerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } elseif (is_object($value) || is_resource($value)) { // Those types for $value are not allowed throw new InvalidArgumentException(sprintf('value[]=%s is not valid', gettype($value))); } // Call the protected method /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: Invoking parent::addValueToStack(%s,%s) ...', $stackerName, gettype($value))); parent::addValueToStack($stackerName, $value); // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: EXIT!'); } /** * 'Pops' a value from a named stacker and returns it's value * * @param $stackerName Name of the stack * @return $value Value of the current stack entry * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If the named stacker was not found * @throws BadMethodCallException If the named stacker is empty */ public function popNamed (string $stackerName) { // Validate parameter /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackerName=%s - CALLED!', $stackerName)); if (empty($stackerName)) { // No empty stack name throw new InvalidArgumentException('Parameter "stackerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } // Get the value /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: this->getNamed(%s) ...', $stackerName)); $value = $this->getNamed($stackerName); // Call the protected method parent::popFirst($stackerName); // Return the value /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: value[]=%s - EXIT!', gettype($value))); return $value; } /** * Get value from named stacker * * @param $stackerName Name of the stack * @return $value Value of last added value * @throws InvalidArgumentException If a parameter is invalid * @throws BadMethodCallException If the named stacker was not found * @throws BadMethodCallException If the named stacker is empty */ public function getNamed (string $stackerName) { // Validate parameter /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackerName=%s - CALLED!', $stackerName)); if (empty($stackerName)) { // No empty stack name throw new InvalidArgumentException('Parameter "stackerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } // Call the protected method /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: parent::getFirstValue(%s) ...', $stackerName)); $value = parent::getFirstValue($stackerName); // Return the value /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: value[]=%s - EXIT!', gettype($value))); return $value; } /** * Seeks to given position * * @param $seekPosition Seek position in file * @param $whence Added to offset (default: only use offset to seek to) * @return $status Status of this operation * @throws OutOfBoundsException If the position is not seekable */ public function seek (int $seekPosition, int $whence = SEEK_SET) { // Validate parameter /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: seekPosition=%d,whence=%d - CALLED!', $seekPosition, $whence)); if ($seekPosition < 0) { // Invalid seek position throw new OutOfBoundsException(sprintf('seekPosition=%d is not valid', $seekPosition)); } // @TODO Unfinished method or invoke inner iterator's method? DebugMiddleware::getSelfInstance()->partialStub('seekPosition=' . $seekPosition); // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: EXIT!'); } /** * Size of file stack * * @return $size Size (in bytes) of file */ public function size () { // Call the iterator instance /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: CALLED!'); $size = $this->getIteratorInstance()->size(); // Return size /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: size=%d - EXIT!', $size)); return $size; } }