]> 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\Generic\NullPointerException;
9 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
10 use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait;
11
12 // Import SPL stuff
13 use \InvalidArgumentExeption;
14 use \IteratorAggregate;
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 - 2022 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                 parent::__construct($className);
56
57                 // Init generic arrays
58                 $this->initGenericArrayGroup('registry', 'generic');
59                 $this->initGenericArrayGroup('registry', 'instance');
60         }
61
62         /**
63          * Returns an iterator for this whole registry.
64          *
65          * @param       $onlyRegistries         Only iterate on these sub-registry keys, default is all
66          * @return      $iteratorInstance       An instance of a Iterator class
67          */
68         public function getIterator (array $onlyRegistries = []) {
69                 // Is it set?
70                 if (is_null($this->getIteratorInstance())) {
71                         // Then instance it
72                         $iteratorInstance = ObjectFactory::createObjectByConfiguredName('registry_iterator_class', [$this]);
73
74                         // ... and set it here
75                         $this->setIteratorInstance($iteratorInstance);
76                 } else {
77                         // Use set iterator
78                         $iteratorInstance = $this->getIteratorInstance();
79                 }
80
81                 // Init iterator instance
82                 $iteratorInstance->initIterator($onlyRegistries);
83
84                 // Return it
85                 return $iteratorInstance;
86         }
87
88         /**
89          * Checks whether an instance key was found
90          *
91          * @param       $instanceKey    The key holding an instance in registry
92          * @return      $exists                 Whether the key exists in registry
93          */
94         public function instanceExists (string $instanceKey) {
95                 // Does this key exists?
96                 $exists = $this->isGenericArrayKeySet('registry', 'instance', $instanceKey);
97
98                 // Return the result
99                 return $exists;
100         }
101
102         /**
103          * Adds/overwrites a new instance to the registry at the given key
104          *
105          * @param       $instanceKey            The key to identify the instance
106          * @param       $objectInstance         An instance we shall store
107          * @return      void
108          */
109         public function addInstance (string $instanceKey, Registerable $objectInstance) {
110                 // Validate parameter
111                 if (empty($instanceKey)) {
112                         // Throw IAE
113                         throw new InvalidArgumentExeption('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
114                 }
115
116                 // Set entry in generic array
117                 $this->setGenericArrayKey('registry', 'instance', $instanceKey, $objectInstance);
118         }
119
120         /**
121          * Getter for whole generic registry
122          *
123          * @return      $instanceRegistry       The whole generic registry array
124          */
125         public final function getGenericRegistry () {
126                 return $this->getGenericSubArray('registry', 'generic');
127         }
128
129         /**
130          * Getter for whole instance registry
131          *
132          * @return      $instanceRegistry       The whole instance registry array
133          */
134         public final function getInstanceRegistry () {
135                 return $this->getGenericSubArray('registry', 'instance');
136         }
137
138         /**
139          * Adds a new entry to the given list name. If you want to add objects
140          * please use addInstance() and getInstance() instead.
141          *
142          * @param       $key    The key to identify the whole list
143          * @param       $value  The value to be stored
144          * @return      void
145          */
146         public final function addEntry (string $key, $value) {
147                 // Push it
148                 $this->pushValueToGenericArrayKey('registry', 'generic', $key, $value);
149         }
150
151         /**
152          * Getter for entries or "sub entries"
153          *
154          * @param       $key    Key
155          * @return      $entries        An array with entries from this registry
156          */
157         public final function getEntries (string $key = NULL) {
158                 // Default is whole array
159                 $entries = $this->getGenericArray('registry');
160
161                 // Is $key set?
162                 if (!is_null($key)) {
163                         // Then use this entry
164                         $entries = $this->getGenericArrayKey('registry', 'generic', $key);
165                 }
166
167                 // Return the array
168                 return $entries;
169         }
170
171         /**
172          * "Getter" for an array of all entries for given key
173          *
174          * @param       $arrayKey       The array (key) to look in
175          * @param       $lookFor        The key to look for
176          * @return      $entry          An array with all keys
177          */
178         public function getArrayFromKey (string $arrayKey, string $lookFor) {
179                 // Init array
180                 $entry = [];
181
182                 // "Walk" over all entries
183                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking arrayKey=' . $arrayKey . ',lookFor=' . $lookFor);
184                 foreach ($this->getEntries($arrayKey) as $key => $value) {
185                         // If $value matches the $lookFor, we need to look for more entries for this!
186                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor);
187                         if ($lookFor == $value) {
188                                 // Look for more entries
189                                 foreach ($this->getEntries() as $key2 => $value2) {
190                                         // Now "walk" through all entries, if an array is returned
191                                         if (is_array($value2)) {
192                                                 // "Walk" through all of them
193                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key2=' . $key2 . ',value2()=' . count($value2) . ',lookFor=' . $lookFor);
194                                                 foreach ($value2 as $key3 => $value3) {
195                                                         // $value3 needs to be an array
196                                                         assert(is_array($value3));
197
198                                                         // Both keys must match!
199                                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key=' . $key . ',key3=' . $key3 . ',isset()=' . isset($value3[$key]) . ' ...');
200                                                         if (($key == $key3) || (isset($value3[$key]))) {
201                                                                 // Then add it
202                                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Adding ' . $value3[$key] . ' ...');
203                                                                 $entry[$key3] = $value3[$key];
204                                                         }
205                                                 }
206                                         }
207                                 }
208
209                                 // Skip further lookups
210                                 break;
211                         }
212                 }
213
214                 // Return it
215                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Returning entry(' . count($entry) . ')=' . print_r($entry, true));
216                 return $entry;
217         }
218
219         /**
220          * Gets a registered instance or null if not found
221          *
222          * @param       $instanceKey            The key to identify the instance
223          * @return      $objectInstance         An instance we shall store
224          * @throws      NullPointerException    If the requested key is not found
225          */
226         public function getInstance (string $instanceKey) {
227                 // Is the instance there?
228                 if (!$this->instanceExists($instanceKey)) {
229                         // This might happen if a non-registered key was requested
230                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
231                 }
232
233                 // Get the instance
234                 $objectInstance = $this->getGenericArrayKey('registry', 'instance', $instanceKey);
235
236                 // Return the result
237                 return $objectInstance;
238         }
239
240         /**
241          * "Getter" for a registry key for given prefix and array. This method
242          * calls implode() to get a suitable key. This method does not care about
243          * the indexes.
244          *
245          * @param       $prefix                 Prefix for the key
246          * @param       $data                   An array with data
247          * @return      $registryKey    A registry key
248          */
249         public static function getRegistryKeyFromArray (string $prefix, array $data) {
250                 // "Generate" the key
251                 $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);
252
253                 // Return it
254                 return $registryKey;
255         }
256
257 }