]> git.mxchange.org Git - core.git/blob - framework/main/classes/registry/object/class_ObjectRegistry.php
Continued:
[core.git] / framework / main / classes / registry / object / class_ObjectRegistry.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Registry\Object;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
7 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
8 use Org\Mxchange\CoreFramework\Registry\BaseRegistry;
9 use Org\Mxchange\CoreFramework\Registry\Registerable;
10
11 // Import SPL stuff
12 use \InvalidArgumentException;
13
14 /**
15  * A registry for several data types and objects. Objects should be added by
16  * addInstance() and therefore must implement the interface Registerable.
17  *
18  * @author              Roland Haeder <webmaster@shipsimu.org>
19  * @version             0.0.0
20  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 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 class ObjectRegistry extends BaseRegistry implements ObjectRegister {
38         /**
39          * Instances of this class
40          */
41         private static $registryInstances = [];
42
43         /**
44          * Protected constructor
45          *
46          * @return      void
47          */
48         private function __construct () {
49                 // Call parent constructor
50                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('OBJECT-REGISTRY: CONSTRUCTED!');
51                 parent::__construct(__CLASS__);
52
53                 // Init own array
54                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('OBJECT-REGISTRY: Invoking this->initGenericArrayGroup(registry,instance) ...');
55                 $this->initGenericArrayGroup('registry', 'instance');
56
57                 // Trace message
58                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('OBJECT-REGISTRY: EXIT!');
59         }
60
61         /**
62          * Singleton getter for self instance. This class has no factory pattern
63          * because here is no need for special parameters.
64          *
65          * @param       $key    Key for for this instance
66          * @return      $registryInstance       Instance of this class
67          * @throws      InvalidArgumentException        If a parameter has an invalid value
68          */
69         public static final function getRegistry (string $key) {
70                 // Check parameter
71                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: key=%s - CALLED!', $key));
72                 if (empty($key)) {
73                         // Throw IAE
74                         throw new InvalidArgumentException('Parameter "key" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
75                 }
76
77                 // Is an instance there?
78                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('OBJECT-REGISTRY: self::registryInstance[%s]?=%d', $key, intval(isset(self::$registryInstances[$key]))));
79                 if (!isset(self::$registryInstances[$key])) {
80                         // Not yet, so create one
81                         self::$registryInstances[$key] = new ObjectRegistry();
82                 }
83
84                 // Return the instance
85                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: self::registryInstance[%s]=%s - EXIT!', $key, self::$registryInstances[$key]->__toString()));
86                 return self::$registryInstances[$key];
87         }
88
89         /**
90          * Checks whether an instance key was found
91          *
92          * @param       $instanceKey    The key holding an instance in registry
93          * @return      $exists                 Whether the key exists in registry
94          * @throws      InvalidArgumentException        If a paramter has an invalid value
95          */
96         public function instanceExists (string $instanceKey) {
97                 // Check parameter
98                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: instanceKey=%s - CALLED!', $instanceKey));
99                 if (empty($instanceKey)) {
100                         // Throw IAE
101                         throw new InvalidArgumentException('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
102                 }
103
104                 // Does this key exists?
105                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: Invoking this->isGenericArrayKeySet(registry, instance, %s) ...', $instanceKey));
106                 $exists = $this->isGenericArrayKeySet('registry', 'instance', $instanceKey);
107
108                 // Return the result
109                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: exists=%d - EXIT!', intval($exists)));
110                 return $exists;
111         }
112
113         /**
114          * Getter for whole instance registry
115          *
116          * @return      $instanceRegistry       The whole instance registry array
117          */
118         public final function getInstanceRegistry () {
119                 return $this->getGenericSubArray('registry', 'instance');
120         }
121
122         /**
123          * Adds/overwrites a new instance to the registry at the given key
124          *
125          * @param       $instanceKey            The key to identify the instance
126          * @param       $objectInstance         An instance we shall store
127          * @return      void
128          * @throws      InvalidArgumentException        If a paramter has an invalid value
129          */
130         public function addInstance (string $instanceKey, Registerable $objectInstance) {
131                 // Check parameter
132                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: instanceKey=%s,objectInstance=%s - CALLED!', $instanceKey, $objectInstance->__toString()));
133                 if (empty($instanceKey)) {
134                         // Throw IAE
135                         throw new InvalidArgumentExeption('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
136                 }
137
138                 // Set entry in generic array
139                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: Invoking this->setGenericArrayKey(registry,instance,%s,%s) ...', $instanceKey, $objectInstance->__toString()));
140                 $this->setGenericArrayKey('registry', 'instance', $instanceKey, $objectInstance);
141
142                 // Trace message
143                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('OBJECT-REGISTRY: EXIT!');
144         }
145
146         /**
147          * Gets a registered instance or null if not found
148          *
149          * @param       $instanceKey            The key to identify the instance
150          * @return      $objectInstance         An instance we shall store
151          * @throws      InvalidArgumentException        If a paramter has an invalid value
152          * @throws      NullPointerException    If the requested key is not found
153          */
154         public function getInstance (string $instanceKey) {
155                 // Check parameter
156                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: instanceKey=%s - CALLED!', $instanceKey));
157                 if (empty($instanceKey)) {
158                         // Throw IAE
159                         throw new InvalidArgumentException('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
160                 }
161
162                 // Is the instance there?
163                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: Invoking this->instanceExists(%s) ...', $instanceKey));
164                 if (!$this->instanceExists($instanceKey)) {
165                         // This might happen if a non-registered key was requested
166                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
167                 }
168
169                 // Get the instance
170                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: Invoking this->getGenericArrayKey(registry,instance,%s) ...', $instanceKey));
171                 $objectInstance = $this->getGenericArrayKey('registry', 'instance', $instanceKey);
172
173                 // Return the result
174                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: objectInstance=%s - EXIT!', $objectInstance->__toString()));
175                 return $objectInstance;
176         }
177
178 }