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;
16 * A FiFo file-based stack
18 * @author Roland Haeder <webmaster@ship-simu.org>
20 * @copyright Copyright (c) 2020 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.ship-simu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class FiFoFileStack extends BaseFileStack implements StackableFile, CalculatableBlock, Registerable {
39 * Protected constructor
43 protected function __construct () {
44 // Call parent constructor
45 parent::__construct(__CLASS__);
49 * Creates an instance of this class
51 * @param $fileInfoInstance An instance of a SplFileInfo class
52 * @param $type Type of this stack (e.g. url_source for URL sources)
53 * @return $stackInstance An instance of a StackableFile class
54 * @throws InvalidArgumentException If a parameter is invalid
56 public final static function createFiFoFileStack (SplFileInfo $fileInfoInstance, string $type) {
58 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: fileInfoInstance[%s]=%s,type=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance, $type));
61 throw new InvalidArgumentException('Parameter "type" is empty');
65 $stackInstance = new FiFoFileStack();
68 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: Calling stackInstance->initFileStack([%s]=%s,%s) ...', get_class($fileInfoInstance), $fileInfoInstance, $type));
69 $stackInstance->initFileStack($fileInfoInstance, $type);
71 // Return the prepared instance
72 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackInstance=%s - EXIT!', $stackInstance->__toString()));
73 return $stackInstance;
77 * Pushs a value on a named stacker
79 * @param $stackerName Name of the stack
80 * @param $value Value to push on it
82 * @throws InvalidArgumentException If a parameter is invalid
83 * @throws StackerFullException If the stack is full
85 public function pushNamed (string $stackerName, $value) {
87 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackerName=%s,value[]=%s - CALLED!', $stackerName, gettype($value)));
88 if (empty($stackerName)) {
89 // No empty stack name
90 throw new InvalidArgumentException('Parameter "stackerName" is empty');
91 } elseif (is_object($value) || is_resource($value)) {
92 // Those types for $value are not allowed
93 throw new InvalidArgumentException(sprintf('value[]=%s is not valid', gettype($value)));
96 // Call the protected method
97 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: Calling parent::addValueToStack(%s,%s) ...', $stackerName, gettype($value)));
98 parent::addValueToStack($stackerName, $value);
101 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: EXIT!');
105 * 'Pops' a value from a named stacker and returns it's value
107 * @param $stackerName Name of the stack
108 * @return $value Value of the current stack entry
109 * @throws InvalidArgumentException If a parameter is invalid
110 * @throws BadMethodCallException If the named stacker was not found
111 * @throws BadMethodCallException If the named stacker is empty
113 public function popNamed (string $stackerName) {
114 // Validate parameter
115 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
116 if (empty($stackerName)) {
117 // No empty stack name
118 throw new InvalidArgumentException('Parameter "stackerName" is empty');
122 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: this->getNamed(%s) ...', $stackerName));
123 $value = $this->getNamed($stackerName);
125 // Call the protected method
126 parent::popFirst($stackerName);
129 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
134 * Get value from named stacker
136 * @param $stackerName Name of the stack
137 * @return $value Value of last added value
138 * @throws InvalidArgumentException If a parameter is invalid
139 * @throws BadMethodCallException If the named stacker was not found
140 * @throws BadMethodCallException If the named stacker is empty
142 public function getNamed (string $stackerName) {
143 // Validate parameter
144 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: stackerName=%s - CALLED!', $stackerName));
145 if (empty($stackerName)) {
146 // No empty stack name
147 throw new InvalidArgumentException('Parameter "stackerName" is empty');
150 // Call the protected method
151 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: parent::getFirstValue(%s) ...', $stackerName));
152 $value = parent::getFirstValue($stackerName);
155 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
160 * Seeks to given position
162 * @param $seekPosition Seek position in file
163 * @param $whence Added to offset (default: only use offset to seek to)
164 * @return $status Status of this operation
165 * @throws InvalidArgumentException If a parameter is invalid
167 public function seek (int $seekPosition, int $whence = SEEK_SET) {
168 // Validate parameter
169 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: seekPosition=%d,whence=%d - CALLED!', $seekPosition, $whence));
170 if ($seekPosition < 0) {
171 // Invalid seek position
172 throw new InvalidArgumentException(sprintf('seekPosition=%d is not valid', $seekPosition));
175 // @TODO Unfinished method or invoke inner iterator's method?
176 $this->partialStub('seekPosition=' . $seekPosition);
179 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: EXIT!');
185 * @return $size Size (in bytes) of file
187 public function size () {
188 // Call the iterator instance
189 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: CALLED!');
190 $size = $this->getIteratorInstance()->size();
193 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: size=%d - EXIT!', $size));