]> git.mxchange.org Git - core.git/blob - framework/main/classes/stacker/file/fifo/class_FiFoFileStack.php
Continued:
[core.git] / framework / main / classes / stacker / file / fifo / class_FiFoFileStack.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Stack\File;
4
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;
11
12 // Import SPL stuff
13 use \InvalidArgumentException;
14 use \OutOfBoundsException;
15 use \SplFileInfo;
16
17 /**
18  * A FiFo file-based stack
19  *
20  * @author              Roland Haeder <webmaster@ship-simu.org>
21  * @version             0.0.0
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
25  *
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.
30  *
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.
35  *
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/>.
38  */
39 class FiFoFileStack extends BaseFileStack implements StackableFile, CalculatableBlock, Registerable {
40         /**
41          * Protected constructor
42          *
43          * @return      void
44          */
45         private function __construct () {
46                 // Call parent constructor
47                 parent::__construct(__CLASS__);
48         }
49
50         /**
51          * Creates an instance of this class
52          *
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
57          */
58         public final static function createFiFoFileStack (SplFileInfo $fileInfoInstance, string $type) {
59                 // Validate parameter
60                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: fileInfoInstance[%s]=%s,type=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance, $type));
61                 if (empty($type)) {
62                         // No empty type
63                         throw new InvalidArgumentException('Parameter "type" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
64                 }
65
66                 // Get new instance
67                 $stackInstance = new FiFoFileStack();
68
69                 // Init this stack
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);
72
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;
76         }
77
78         /**
79          * Pushs a value on a named stacker
80          *
81          * @param       $stackerName    Name of the stack
82          * @param       $value                  Value to push on it
83          * @return      void
84          * @throws      InvalidArgumentException        If a parameter is invalid
85          * @throws      StackerFullException    If the stack is full
86          */
87         public function pushNamed (string $stackerName, $value) {
88                 // Validate parameter
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)));
96                 }
97
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);
101
102                 // Trace message
103                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: EXIT!');
104         }
105
106         /**
107          * 'Pops' a value from a named stacker and returns it's value
108          *
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
114          */
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);
121                 }
122
123                 // Get the value
124                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: this->getNamed(%s) ...', $stackerName));
125                 $value = $this->getNamed($stackerName);
126
127                 // Call the protected method
128                 parent::popFirst($stackerName);
129
130                 // Return the value
131                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
132                 return $value;
133         }
134
135         /**
136          * Get value from named stacker
137          *
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
143          */
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);
150                 }
151
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);
155
156                 // Return the value
157                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
158                 return $value;
159         }
160
161         /**
162          * Seeks to given position
163          *
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
168          */
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));
175                 }
176
177                 // @TODO Unfinished method or invoke inner iterator's method?
178                 $this->partialStub('seekPosition=' . $seekPosition);
179
180                 // Trace message
181                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: EXIT!');
182         }
183
184         /**
185          * Size of file stack
186          *
187          * @return      $size   Size (in bytes) of file
188          */
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();
193
194                 // Return size
195                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: size=%d - EXIT!', $size));
196                 return $size;
197         }
198
199 }