]> git.mxchange.org Git - core.git/blob - framework/main/classes/registry/class_BaseRegistry.php
Continued:
[core.git] / framework / main / classes / registry / class_BaseRegistry.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Registry;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
8 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
9 use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait;
10
11 // Import SPL stuff
12 use \InvalidArgumentExeption;
13 use \IteratorAggregate;
14 use \UnexpectedValueException;
15
16 /**
17  * A general Registry
18  *
19  * @author              Roland Haeder <webmaster@shipsimu.org>
20  * @version             0.0.0
21  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
22  * @license             GNU GPL 3.0 or any newer version
23  * @link                http://www.shipsimu.org
24  *
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.
29  *
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.
34  *
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/>.
37  */
38 abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable, IteratorAggregate {
39         // Load traits
40         use IteratorTrait;
41
42         /**
43          * Glue for generating a registry key
44          */
45         const REGISTRY_KEY_GLUE = '_';
46
47         /**
48          * Protected constructor
49          *
50          * @param       $className      Name of the class
51          * @return      void
52          */
53         protected function __construct (string $className) {
54                 // Call parent constructor
55                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: className=%s - CONSTRUCTED!', $className));
56                 parent::__construct($className);
57
58                 // Init generic arrays
59                 $this->initGenericArrayGroup('registry', 'generic');
60
61                 // Trace message
62                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-REGISTRY: EXIT!');
63         }
64
65         /**
66          * Returns an iterator for this whole registry.
67          *
68          * @param       $onlyRegistries         Only iterate on these sub-registry keys, default is all
69          * @return      $iteratorInstance       An instance of a Iterator class
70          */
71         public function getIterator (array $onlyRegistries = []) {
72                 // Get iterator
73                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: onlyRegistries()=%d - CALLED!', count($onlyRegistries)));
74                 $iteratorInstance = $this->getIteratorInstance();
75
76                 // Is it set?
77                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: iteratorInstance[]=%s', gettype($iteratorInstance)));
78                 if (is_null($iteratorInstance)) {
79                         // Then instance it
80                         $iteratorInstance = ObjectFactory::createObjectByConfiguredName('registry_iterator_class', [$this]);
81
82                         // ... and set it here
83                         $this->setIteratorInstance($iteratorInstance);
84                 }
85
86                 // Init iterator instance
87                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: Invoking iteratorInstance->initIterator(onlyRegistries()=%d) ...', count($onlyRegistries)));
88                 $iteratorInstance->initIterator($onlyRegistries);
89
90                 // Return it
91                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: iteratorInstance=%s - EXIT!', $iteratorInstance->__toString()));
92                 return $iteratorInstance;
93         }
94
95         /**
96          * Getter for whole generic registry
97          *
98          * @return      $instanceRegistry       The whole generic registry array
99          */
100         public final function getGenericRegistry () {
101                 return $this->getGenericSubArray('registry', 'generic');
102         }
103
104         /**
105          * Adds a new entry to the given list name. If you want to add objects
106          * please use addInstance() and getInstance() instead.
107          *
108          * @param       $key    The key to identify the whole list
109          * @param       $value  The value to be stored
110          * @return      void
111          * @throws      InvalidArgumentException        If a paramter has an invalid value
112          */
113         public final function addEntry (string $key, $value) {
114                 // Check parameter
115                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: key=%s,value[]=%s - CALLED!', $key, gettype($value)));
116                 if (empty($key)) {
117                         // Throw IAE
118                         throw new InvalidArgumentExeption('Parameter "key" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
119                 }
120
121                 // Push it
122                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: Invoking this->pushValueToGenericArrayKey(registry,generic,%s,value[]=%s) ...', $key, gettype($value)));
123                 $this->pushValueToGenericArrayKey('registry', 'generic', $key, $value);
124
125                 // Trace message
126                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-REGISTRY: EXIT!');
127         }
128
129         /**
130          * Getter for entries or "sub entries"
131          *
132          * @param       $key    Key
133          * @return      $entries        An array with entries from this registry
134          * @throws      InvalidArgumentException        If a paramter has an invalid value
135          */
136         public final function getEntries (string $key = NULL) {
137                 // Check parameter
138                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: key[%s]=%s - CALLED!', gettype($key), $key));
139                 if (!is_null($key) && empty($key)) {
140                         // Throw IAE
141                         throw new InvalidArgumentExeption('Parameter "key" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
142                 }
143
144                 // Default is whole array
145                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-REGISTRY: Invoking this->getGenericArray(registry) ...');
146                 $entries = $this->getGenericArray('registry');
147
148                 // Is $key set?
149                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: key[]=%s,entries()=%d', $key, count($entries)));
150                 if (!is_null($key)) {
151                         // Then use this entry
152                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: Invoking this->getGenericArrayKey(registry,generic,%s) ...', $key));
153                         $entries = $this->getGenericArrayKey('registry', 'generic', $key);
154                 }
155
156                 // Return the array
157                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: entries()=%d - EXIT!', count($entries)));
158                 return $entries;
159         }
160
161         /**
162          * "Getter" for an array of all entries for given key
163          *
164          * @param       $arrayKey       The array (key) to look in
165          * @param       $lookFor        The key to look for
166          * @return      $entry          An array with all keys
167          * @throws      InvalidArgumentException        If a paramter has an invalid value
168          * @throws      UnexpectedValueException        If $value3 is not an array
169          */
170         public function getArrayFromKey (string $arrayKey, string $lookFor) {
171                 // Check parameter
172                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: arrayKey=%s,lookFor=%s - CALLED!', $arrayKey, $lookFor));
173                 if (empty($arrayKey)) {
174                         // Throw IAE
175                         throw new InvalidArgumentExeption('Parameter "arrayKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
176                 } elseif (empty($lookFor)) {
177                         // Throw IAE
178                         throw new InvalidArgumentExeption('Parameter "lookFor" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
179                 }
180
181                 // Init array
182                 $entry = [];
183
184                 // "Walk" over all entries
185                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Checking arrayKey=%s,lookFor=%s', $arrayKey, $lookFor));
186                 foreach ($this->getEntries($arrayKey) as $key => $value) {
187                         // If $value matches the $lookFor, we need to look for more entries for this!
188                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Checking key=%s,value=%s,lookFor=%s', $key, $value, $lookFor));
189                         if ($lookFor == $value) {
190                                 // Look for more entries
191                                 foreach ($this->getEntries() as $key2 => $value2) {
192                                         // Now "walk" through all entries, if an array is returned
193                                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: key2=%s,value2[]=%s', $key2, gettype($value2)));
194                                         if (is_array($value2)) {
195                                                 // "Walk" through all of them
196                                                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Checking key2=%s,value2()=%s,lookFor=%s', $key2, count($value2), $lookFor));
197                                                 foreach ($value2 as $key3 => $value3) {
198                                                         // $value3 needs to be an array
199                                                         if (!is_array($value3)) {
200                                                                 // Throw exception
201                                                                 throw new UnexpectedValueException(sprintf('arrayKey=%s,key=%s,value=%s,key2=%s,key3=%s has unexpected value3[]=%s', $arrayKey, $key, $value, $key2, $key3, gettype($value3)), FrameworkInterface::EXCEPTION_UNEXPECTED_VALUE);
202                                                         }
203
204                                                         // Both keys must match!
205                                                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Checking key=%s,key3=%s,isset()=%s ...', $key, $key3, intval(isset($value3[$key]))));
206                                                         if (($key == $key3) || (isset($value3[$key]))) {
207                                                                 // Then add it
208                                                                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-REGISTRY: Adding value3[%s]=%s ...', $key, $value3[$key]));
209                                                                 $entry[$key3] = $value3[$key];
210                                                         }
211                                                 }
212                                         }
213                                 }
214
215                                 // Skip further lookups
216                                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('BASE-REGISTRY: BREAK!');
217                                 break;
218                         }
219                 }
220
221                 // Return it
222                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: entry()=%d - EXIT!', count($entry)));
223                 return $entry;
224         }
225
226         /**
227          * "Getter" for a registry key for given prefix and array. This method
228          * calls implode() to get a suitable key. This method does not care about
229          * the indexes.
230          *
231          * @param       $prefix                 Prefix for the key
232          * @param       $data                   An array with data
233          * @return      $registryKey    A registry key
234          */
235         public static function getRegistryKeyFromArray (string $prefix, array $data) {
236                 // Check parameter
237                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: prefix=%s,data()=%d - CALLED!', $prefix, count($data)));
238                 if (empty($prefix)) {
239                         // Throw IAE
240                         throw new InvalidArgumentException('Parameter "prefix" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
241                 } elseif (count($data) == 0) {
242                         // Throw IAE
243                         throw new InvalidArgumentException('Parameter "data" is an empty array', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
244                 }
245
246                 // "Generate" the key
247                 $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);
248
249                 // Return it
250                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-REGISTRY: registryKey=%s - EXIT!', $registryKey));
251                 return $registryKey;
252         }
253
254 }