3 namespace Org\Mxchange\CoreFramework\Stack\File;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filesystem\Block\CalculatableBlock;
7 use Org\Mxchange\CoreFramework\Registry\Registerable;
8 use Org\Mxchange\CoreFramework\Stack\File\BaseFileStack;
9 use Org\Mxchange\CoreFramework\Stack\File\StackableFile;
12 use \InvalidArgumentException;
13 use \OutOfBoundsException;
17 * A FiFo file-based stack
19 * @author Roland Haeder <webmaster@ship-simu.org>
21 * @copyright Copyright (c) 2020 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.ship-simu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class FiFoFileStack extends BaseFileStack implements StackableFile, CalculatableBlock, Registerable {
40 * Protected constructor
44 protected function __construct () {
45 // Call parent constructor
46 parent::__construct(__CLASS__);
50 * Creates an instance of this class
52 * @param $fileInfoInstance An instance of a SplFileInfo class
53 * @param $type Type of this stack (e.g. url_source for URL sources)
54 * @return $stackInstance An instance of a StackableFile class
55 * @throws InvalidArgumentException If a parameter is invalid
57 public final static function createFiFoFileStack (SplFileInfo $fileInfoInstance, string $type) {
59 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: fileInfoInstance[%s]=%s,type=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance, $type));
62 throw new InvalidArgumentException('Parameter "type" is empty');
66 $stackInstance = new FiFoFileStack();
69 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: Calling stackInstance->initFileStack([%s]=%s,%s) ...', get_class($fileInfoInstance), $fileInfoInstance, $type));
70 $stackInstance->initFileStack($fileInfoInstance, $type);
72 // Return the prepared instance
73 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackInstance=%s - EXIT!', $stackInstance->__toString()));
74 return $stackInstance;
78 * Pushs a value on a named stacker
80 * @param $stackerName Name of the stack
81 * @param $value Value to push on it
83 * @throws InvalidArgumentException If a parameter is invalid
84 * @throws StackerFullException If the stack is full
86 public function pushNamed (string $stackerName, $value) {
88 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackerName=%s,value[]=%s - CALLED!', $stackerName, gettype($value)));
89 if (empty($stackerName)) {
90 // No empty stack name
91 throw new InvalidArgumentException('Parameter "stackerName" is empty');
92 } elseif (is_object($value) || is_resource($value)) {
93 // Those types for $value are not allowed
94 throw new InvalidArgumentException(sprintf('value[]=%s is not valid', gettype($value)));
97 // Call the protected method
98 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: Calling parent::addValueToStack(%s,%s) ...', $stackerName, gettype($value)));
99 parent::addValueToStack($stackerName, $value);
102 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: EXIT!');
106 * 'Pops' a value from a named stacker and returns it's value
108 * @param $stackerName Name of the stack
109 * @return $value Value of the current stack entry
110 * @throws InvalidArgumentException If a parameter is invalid
111 * @throws BadMethodCallException If the named stacker was not found
112 * @throws BadMethodCallException If the named stacker is empty
114 public function popNamed (string $stackerName) {
115 // Validate parameter
116 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
117 if (empty($stackerName)) {
118 // No empty stack name
119 throw new InvalidArgumentException('Parameter "stackerName" is empty');
123 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: this->getNamed(%s) ...', $stackerName));
124 $value = $this->getNamed($stackerName);
126 // Call the protected method
127 parent::popFirst($stackerName);
130 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
135 * Get value from named stacker
137 * @param $stackerName Name of the stack
138 * @return $value Value of last added value
139 * @throws InvalidArgumentException If a parameter is invalid
140 * @throws BadMethodCallException If the named stacker was not found
141 * @throws BadMethodCallException If the named stacker is empty
143 public function getNamed (string $stackerName) {
144 // Validate parameter
145 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
146 if (empty($stackerName)) {
147 // No empty stack name
148 throw new InvalidArgumentException('Parameter "stackerName" is empty');
151 // Call the protected method
152 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: parent::getFirstValue(%s) ...', $stackerName));
153 $value = parent::getFirstValue($stackerName);
156 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
161 * Seeks to given position
163 * @param $seekPosition Seek position in file
164 * @param $whence Added to offset (default: only use offset to seek to)
165 * @return $status Status of this operation
166 * @throws OutOfBoundsException If the position is not seekable
168 public function seek (int $seekPosition, int $whence = SEEK_SET) {
169 // Validate parameter
170 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: seekPosition=%d,whence=%d - CALLED!', $seekPosition, $whence));
171 if ($seekPosition < 0) {
172 // Invalid seek position
173 throw new OutOfBoundsException(sprintf('seekPosition=%d is not valid', $seekPosition));
176 // @TODO Unfinished method or invoke inner iterator's method?
177 $this->partialStub('seekPosition=' . $seekPosition);
180 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: EXIT!');
186 * @return $size Size (in bytes) of file
188 public function size () {
189 // Call the iterator instance
190 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: CALLED!');
191 $size = $this->getIteratorInstance()->size();
194 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: size=%d - EXIT!', $size));