]> 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\NullPointerException;
8 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
9 use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait;
10
11 // Import SPL stuff
12 use \IteratorAggregate;
13
14 /**
15  * A general Registry
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
19  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
20  * @license             GNU GPL 3.0 or any newer version
21  * @link                http://www.shipsimu.org
22  *
23  * This program is free software: you can redistribute it and/or modify
24  * it under the terms of the GNU General Public License as published by
25  * the Free Software Foundation, either version 3 of the License, or
26  * (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  * GNU General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35  */
36 abstract class BaseRegistry extends BaseFrameworkSystem implements Register, Registerable, IteratorAggregate {
37         // Load traits
38         use IteratorTrait;
39
40         /**
41          * Glue for generating a registry key
42          */
43         const REGISTRY_KEY_GLUE = '_';
44
45         /**
46          * Protected constructor
47          *
48          * @param       $className      Name of the class
49          * @return      void
50          */
51         protected function __construct (string $className) {
52                 // Call parent constructor
53                 parent::__construct($className);
54
55                 // Init generic arrays
56                 $this->initGenericArrayGroup('registry', 'generic');
57                 $this->initGenericArrayGroup('registry', 'instance');
58         }
59
60         /**
61          * Returns an iterator for this whole registry.
62          *
63          * @param       $onlyRegistries         Only iterate on these sub-registry keys, default is all
64          * @return      $iteratorInstance       An instance of a Iterator class
65          */
66         public function getIterator (array $onlyRegistries = []) {
67                 // Is it set?
68                 if (is_null($this->getIteratorInstance())) {
69                         // Then instance it
70                         $iteratorInstance = ObjectFactory::createObjectByConfiguredName('registry_iterator_class', [$this]);
71
72                         // ... and set it here
73                         $this->setIteratorInstance($iteratorInstance);
74                 } else {
75                         // Use set iterator
76                         $iteratorInstance = $this->getIteratorInstance();
77                 }
78
79                 // Init iterator instance
80                 $iteratorInstance->initIterator($onlyRegistries);
81
82                 // Return it
83                 return $iteratorInstance;
84         }
85
86         /**
87          * Checks whether an instance key was found
88          *
89          * @param       $instanceKey    The key holding an instance in registry
90          * @return      $exists                 Whether the key exists in registry
91          */
92         public function instanceExists (string $instanceKey) {
93                 // Does this key exists?
94                 $exists = $this->isGenericArrayKeySet('registry', 'instance', $instanceKey);
95
96                 // Return the result
97                 return $exists;
98         }
99
100         /**
101          * Adds/overwrites a new instance to the registry at the given key
102          *
103          * @param       $instanceKey            The key to identify the instance
104          * @param       $objectInstance         An instance we shall store
105          * @return      void
106          */
107         public function addInstance (string $instanceKey, Registerable $objectInstance) {
108                 $this->setGenericArrayKey('registry', 'instance', $instanceKey, $objectInstance);
109         }
110
111         /**
112          * Getter for whole generic registry
113          *
114          * @return      $instanceRegistry       The whole generic registry array
115          */
116         public final function getGenericRegistry () {
117                 return $this->getGenericSubArray('registry', 'generic');
118         }
119
120         /**
121          * Getter for whole instance registry
122          *
123          * @return      $instanceRegistry       The whole instance registry array
124          */
125         public final function getInstanceRegistry () {
126                 return $this->getGenericSubArray('registry', 'instance');
127         }
128
129         /**
130          * Adds a new entry to the given list name. If you want to add objects
131          * please use addInstance() and getInstance() instead.
132          *
133          * @param       $key    The key to identify the whole list
134          * @param       $value  The value to be stored
135          * @return      void
136          */
137         public final function addEntry (string $key, $value) {
138                 // Push it
139                 $this->pushValueToGenericArrayKey('registry', 'generic', $key, $value);
140         }
141
142         /**
143          * Getter for entries or "sub entries"
144          *
145          * @param       $key    Key
146          * @return      $entries        An array with entries from this registry
147          */
148         public final function getEntries (string $key = NULL) {
149                 // Default is whole array
150                 $entries = $this->getGenericArray('registry');
151
152                 // Is $key set?
153                 if (!is_null($key)) {
154                         // Then use this entry
155                         $entries = $this->getGenericArrayKey('registry', 'generic', $key);
156                 }
157
158                 // Return the array
159                 return $entries;
160         }
161
162         /**
163          * "Getter" for an array of all entries for given key
164          *
165          * @param       $arrayKey       The array (key) to look in
166          * @param       $lookFor        The key to look for
167          * @return      $entry          An array with all keys
168          */
169         public function getArrayFromKey (string $arrayKey, string $lookFor) {
170                 // Init array
171                 $entry = [];
172
173                 // "Walk" over all entries
174                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking arrayKey=' . $arrayKey . ',lookFor=' . $lookFor);
175                 foreach ($this->getEntries($arrayKey) as $key => $value) {
176                         // If $value matches the $lookFor, we need to look for more entries for this!
177                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key=' . $key . ',value=' . $value . ',lookFor=' . $lookFor);
178                         if ($lookFor == $value) {
179                                 // Look for more entries
180                                 foreach ($this->getEntries() as $key2 => $value2) {
181                                         // Now "walk" through all entries, if an array is returned
182                                         if (is_array($value2)) {
183                                                 // "Walk" through all of them
184                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key2=' . $key2 . ',value2()=' . count($value2) . ',lookFor=' . $lookFor);
185                                                 foreach ($value2 as $key3 => $value3) {
186                                                         // $value3 needs to be an array
187                                                         assert(is_array($value3));
188
189                                                         // Both keys must match!
190                                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Checking key=' . $key . ',key3=' . $key3 . ',isset()=' . isset($value3[$key]) . ' ...');
191                                                         if (($key == $key3) || (isset($value3[$key]))) {
192                                                                 // Then add it
193                                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Adding ' . $value3[$key] . ' ...');
194                                                                 $entry[$key3] = $value3[$key];
195                                                         }
196                                                 }
197                                         }
198                                 }
199
200                                 // Skip further lookups
201                                 break;
202                         }
203                 }
204
205                 // Return it
206                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('REGISTRY: Returning entry(' . count($entry) . ')=' . print_r($entry, true));
207                 return $entry;
208         }
209
210         /**
211          * Gets a registered instance or null if not found
212          *
213          * @param       $instanceKey            The key to identify the instance
214          * @return      $objectInstance         An instance we shall store
215          * @throws      NullPointerException    If the requested key is not found
216          */
217         public function getInstance (string $instanceKey) {
218                 // Is the instance there?
219                 if (!$this->instanceExists($instanceKey)) {
220                         // This might happen if a non-registered key was requested
221                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
222                 }
223
224                 // Get the instance
225                 $objectInstance = $this->getGenericArrayKey('registry', 'instance', $instanceKey);
226
227                 // Return the result
228                 return $objectInstance;
229         }
230
231         /**
232          * "Getter" for a registry key for given prefix and array. This method
233          * calls implode() to get a suitable key. This method does not care about
234          * the indexes.
235          *
236          * @param       $prefix                 Prefix for the key
237          * @param       $data                   An array with data
238          * @return      $registryKey    A registry key
239          */
240         public static function getRegistryKeyFromArray (string $prefix, array $data) {
241                 // "Generate" the key
242                 $registryKey = $prefix . self::REGISTRY_KEY_GLUE . implode(self::REGISTRY_KEY_GLUE, $data);
243
244                 // Return it
245                 return $registryKey;
246         }
247
248 }