]> git.mxchange.org Git - core.git/blob - framework/main/classes/stacker/file/fifo/class_FiFoFileStack.php
42eab523b6cab53796820dad5f15eb6a0ffcba60
[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\Registry\Registerable;
8 use Org\Mxchange\CoreFramework\Stack\File\BaseFileStack;
9 use Org\Mxchange\CoreFramework\Stack\File\StackableFile;
10
11 // Import SPL stuff
12 use \InvalidArgumentException;
13 use \SplFileInfo;
14
15 /**
16  * A FiFo file-based stack
17  *
18  * @author              Roland Haeder <webmaster@ship-simu.org>
19  * @version             0.0.0
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
23  *
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.
28  *
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.
33  *
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/>.
36  */
37 class FiFoFileStack extends BaseFileStack implements StackableFile, CalculatableBlock, Registerable {
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         protected function __construct () {
44                 // Call parent constructor
45                 parent::__construct(__CLASS__);
46         }
47
48         /**
49          * Creates an instance of this class
50          *
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
55          */
56         public final static function createFiFoFileStack (SplFileInfo $fileInfoInstance, string $type) {
57                 // Validate parameter
58                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: fileInfoInstance[%s]=%s,type=%s - CALLED!', get_class($fileInfoInstance), $fileInfoInstance, $type));
59                 if (empty($type)) {
60                         // No empty type
61                         throw new InvalidArgumentException('Parameter "type" is empty');
62                 }
63
64                 // Get new instance
65                 $stackInstance = new FiFoFileStack();
66
67                 // Init this stack
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);
70
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;
74         }
75
76         /**
77          * Pushs a value on a named stacker
78          *
79          * @param       $stackerName    Name of the stack
80          * @param       $value                  Value to push on it
81          * @return      void
82          * @throws      InvalidArgumentException        If a parameter is invalid
83          * @throws      StackerFullException    If the stack is full
84          */
85         public function pushNamed (string $stackerName, $value) {
86                 // Validate parameter
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)));
94                 }
95
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);
99
100                 // Trace message
101                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: EXIT!');
102         }
103
104         /**
105          * 'Pops' a value from a named stacker and returns it's value
106          *
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
112          */
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');
119                 }
120
121                 // Get the value
122                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: this->getNamed(%s) ...', $stackerName));
123                 $value = $this->getNamed($stackerName);
124
125                 // Call the protected method
126                 parent::popFirst($stackerName);
127
128                 // Return the value
129                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
130                 return $value;
131         }
132
133         /**
134          * Get value from named stacker
135          *
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
141          */
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');
148                 }
149
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);
153
154                 // Return the value
155                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: value[]=%s - EXIT!', gettype($value)));
156                 return $value;
157         }
158
159         /**
160          * Seeks to given position
161          *
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
166          */
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));
173                 }
174
175                 // @TODO Unfinished method or invoke inner iterator's method?
176                 $this->partialStub('seekPosition=' . $seekPosition);
177
178                 // Trace message
179                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FIFO-FILE-STACK: EXIT!');
180         }
181
182         /**
183          * Size of file stack
184          *
185          * @return      $size   Size (in bytes) of file
186          */
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();
191
192                 // Return size
193                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: size=%d - EXIT!', $size));
194                 return $size;
195         }
196
197 }