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