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\Registry\Registerable;
9 use Org\Mxchange\CoreFramework\Stack\File\BaseFileStack;
10 use Org\Mxchange\CoreFramework\Stack\File\StackableFile;
13 use \InvalidArgumentException;
14 use \OutOfBoundsException;
18 * A FiFo file-based stack
20 * @author Roland Haeder <webmaster@ship-simu.org>
22 * @copyright Copyright (c) 2022 Core Developer Team
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.ship-simu.org
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 class FiFoFileStack extends BaseFileStack implements StackableFile, CalculatableBlock, Registerable {
41 * Protected constructor
45 private function __construct () {
46 // Call parent constructor
47 parent::__construct(__CLASS__);
51 * Creates an instance of this class
53 * @param $fileInfoInstance An instance of a SplFileInfo class
54 * @param $type Type of this stack (e.g. url_source for URL sources)
55 * @return $stackInstance An instance of a StackableFile class
56 * @throws InvalidArgumentException If a parameter is invalid
58 public final static function createFiFoFileStack (SplFileInfo $fileInfoInstance, string $type) {
60 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: fileInfoInstance[%s]=%s,type=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance, $type));
63 throw new InvalidArgumentException('Parameter "type" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
67 $stackInstance = new FiFoFileStack();
70 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: Invoking stackInstance->initFileStack([%s]=%s,%s) ...', get_class($fileInfoInstance), $fileInfoInstance, $type));
71 $stackInstance->initFileStack($fileInfoInstance, $type);
73 // Return the prepared instance
74 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackInstance=%s - EXIT!', $stackInstance->__toString()));
75 return $stackInstance;
79 * Pushs a value on a named stacker
81 * @param $stackerName Name of the stack
82 * @param $value Value to push on it
84 * @throws InvalidArgumentException If a parameter is invalid
85 * @throws StackerFullException If the stack is full
87 public function pushNamed (string $stackerName, $value) {
89 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackerName=%s,value[]=%s - CALLED!', $stackerName, gettype($value)));
90 if (empty($stackerName)) {
91 // No empty stack name
92 throw new InvalidArgumentException('Parameter "stackerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
93 } elseif (is_object($value) || is_resource($value)) {
94 // Those types for $value are not allowed
95 throw new InvalidArgumentException(sprintf('value[]=%s is not valid', gettype($value)));
98 // Call the protected method
99 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: Invoking parent::addValueToStack(%s,%s) ...', $stackerName, gettype($value)));
100 parent::addValueToStack($stackerName, $value);
103 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: EXIT!');
107 * 'Pops' a value from a named stacker and returns it's value
109 * @param $stackerName Name of the stack
110 * @return $value Value of the current stack entry
111 * @throws InvalidArgumentException If a parameter is invalid
112 * @throws BadMethodCallException If the named stacker was not found
113 * @throws BadMethodCallException If the named stacker is empty
115 public function popNamed (string $stackerName) {
116 // Validate parameter
117 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
118 if (empty($stackerName)) {
119 // No empty stack name
120 throw new InvalidArgumentException('Parameter "stackerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
124 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: this->getNamed(%s) ...', $stackerName));
125 $value = $this->getNamed($stackerName);
127 // Call the protected method
128 parent::popFirst($stackerName);
131 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
136 * Get value from named stacker
138 * @param $stackerName Name of the stack
139 * @return $value Value of last added value
140 * @throws InvalidArgumentException If a parameter is invalid
141 * @throws BadMethodCallException If the named stacker was not found
142 * @throws BadMethodCallException If the named stacker is empty
144 public function getNamed (string $stackerName) {
145 // Validate parameter
146 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
147 if (empty($stackerName)) {
148 // No empty stack name
149 throw new InvalidArgumentException('Parameter "stackerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
152 // Call the protected method
153 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: parent::getFirstValue(%s) ...', $stackerName));
154 $value = parent::getFirstValue($stackerName);
157 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
162 * Seeks to given position
164 * @param $seekPosition Seek position in file
165 * @param $whence Added to offset (default: only use offset to seek to)
166 * @return $status Status of this operation
167 * @throws OutOfBoundsException If the position is not seekable
169 public function seek (int $seekPosition, int $whence = SEEK_SET) {
170 // Validate parameter
171 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: seekPosition=%d,whence=%d - CALLED!', $seekPosition, $whence));
172 if ($seekPosition < 0) {
173 // Invalid seek position
174 throw new OutOfBoundsException(sprintf('seekPosition=%d is not valid', $seekPosition));
177 // @TODO Unfinished method or invoke inner iterator's method?
178 $this->partialStub('seekPosition=' . $seekPosition);
181 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: EXIT!');
187 * @return $size Size (in bytes) of file
189 public function size () {
190 // Call the iterator instance
191 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: CALLED!');
192 $size = $this->getIteratorInstance()->size();
195 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: size=%d - EXIT!', $size));