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