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