3 namespace Org\Mxchange\CoreFramework\Stack\File;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filesystem\Block\CalculatableBlock;
7 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
8 use Org\Mxchange\CoreFramework\Middleware\Debug\DebugMiddleware;
9 use Org\Mxchange\CoreFramework\Registry\Registerable;
10 use Org\Mxchange\CoreFramework\Stack\File\BaseFileStack;
11 use Org\Mxchange\CoreFramework\Stack\File\StackableFile;
14 use \InvalidArgumentException;
15 use \OutOfBoundsException;
19 * A FiFo file-based stack
21 * @author Roland Haeder <webmaster@ship-simu.org>
23 * @copyright Copyright (c) 2023 Core Developer Team
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.ship-simu.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 FiFoFileStack extends BaseFileStack implements StackableFile, CalculatableBlock, Registerable {
42 * Protected constructor
46 private function __construct () {
47 // Call parent constructor
48 parent::__construct(__CLASS__);
52 * Creates an instance of this class
54 * @param $fileInfoInstance An instance of a SplFileInfo class
55 * @param $type Type of this stack (e.g. url_source for URL sources)
56 * @return $stackInstance An instance of a StackableFile class
57 * @throws InvalidArgumentException If a parameter is invalid
59 public final static function createFiFoFileStack (SplFileInfo $fileInfoInstance, string $type) {
61 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: fileInfoInstance[%s]=%s,type=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance, $type));
64 throw new InvalidArgumentException('Parameter "type" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
68 $stackInstance = new FiFoFileStack();
71 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: Invoking stackInstance->initFileStack([%s]=%s,%s) ...', get_class($fileInfoInstance), $fileInfoInstance, $type));
72 $stackInstance->initFileStack($fileInfoInstance, $type);
74 // Return the prepared instance
75 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackInstance=%s - EXIT!', $stackInstance->__toString()));
76 return $stackInstance;
80 * Pushs a value on a named stacker
82 * @param $stackerName Name of the stack
83 * @param $value Value to push on it
85 * @throws InvalidArgumentException If a parameter is invalid
86 * @throws BadMethodCallException If the stack is full
88 public function pushNamed (string $stackerName, $value) {
90 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackerName=%s,value[]=%s - CALLED!', $stackerName, gettype($value)));
91 if (empty($stackerName)) {
92 // No empty stack name
93 throw new InvalidArgumentException('Parameter "stackerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
94 } elseif (is_object($value) || is_resource($value)) {
95 // Those types for $value are not allowed
96 throw new InvalidArgumentException(sprintf('value[]=%s is not valid', gettype($value)));
99 // Call the protected method
100 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: Invoking parent::addValueToStack(%s,%s) ...', $stackerName, gettype($value)));
101 parent::addValueToStack($stackerName, $value);
104 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: EXIT!');
108 * 'Pops' a value from a named stacker and returns it's value
110 * @param $stackerName Name of the stack
111 * @return $value Value of the current stack entry
112 * @throws InvalidArgumentException If a parameter is invalid
113 * @throws BadMethodCallException If the named stacker was not found
114 * @throws BadMethodCallException If the named stacker is empty
116 public function popNamed (string $stackerName) {
117 // Validate parameter
118 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
119 if (empty($stackerName)) {
120 // No empty stack name
121 throw new InvalidArgumentException('Parameter "stackerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
125 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: this->getNamed(%s) ...', $stackerName));
126 $value = $this->getNamed($stackerName);
128 // Call the protected method
129 parent::popFirst($stackerName);
132 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
137 * Get value from named stacker
139 * @param $stackerName Name of the stack
140 * @return $value Value of last added value
141 * @throws InvalidArgumentException If a parameter is invalid
142 * @throws BadMethodCallException If the named stacker was not found
143 * @throws BadMethodCallException If the named stacker is empty
145 public function getNamed (string $stackerName) {
146 // Validate parameter
147 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
148 if (empty($stackerName)) {
149 // No empty stack name
150 throw new InvalidArgumentException('Parameter "stackerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
153 // Call the protected method
154 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: parent::getFirstValue(%s) ...', $stackerName));
155 $value = parent::getFirstValue($stackerName);
158 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
163 * Seeks to given position
165 * @param $seekPosition Seek position in file
166 * @param $whence Added to offset (default: only use offset to seek to)
167 * @return $status Status of this operation
168 * @throws OutOfBoundsException If the position is not seekable
170 public function seek (int $seekPosition, int $whence = SEEK_SET) {
171 // Validate parameter
172 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: seekPosition=%d,whence=%d - CALLED!', $seekPosition, $whence));
173 if ($seekPosition < 0) {
174 // Invalid seek position
175 throw new OutOfBoundsException(sprintf('seekPosition=%d is not valid', $seekPosition));
178 // @TODO Unfinished method or invoke inner iterator's method?
179 DebugMiddleware::getSelfInstance()->partialStub('seekPosition=' . $seekPosition);
182 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: EXIT!');
188 * @return $size Size (in bytes) of file
190 public function size () {
191 // Call the iterator instance
192 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: CALLED!');
193 $size = $this->getIteratorInstance()->size();
196 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: size=%d - EXIT!', $size));