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