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