]> 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\NullPointerException;
7 use Org\Mxchange\CoreFramework\Registry\BaseRegistry;
8 use Org\Mxchange\CoreFramework\Registry\Registerable;
9
10 // Import SPL stuff
11 use \InvalidArgumentException;
12
13 /**
14  * A registry for several data types and objects. Objects should be added by
15  * addInstance() and therefore must implement the interface Registerable.
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
19  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
20  * @license             GNU GPL 3.0 or any newer version
21  * @link                http://www.shipsimu.org
22  *
23  * This program is free software: you can redistribute it and/or modify
24  * it under the terms of the GNU General Public License as published by
25  * the Free Software Foundation, either version 3 of the License, or
26  * (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  * GNU General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program. If not, see <http://www.gnu.org/licenses/>.
35  */
36 class ObjectRegistry extends BaseRegistry implements ObjectRegister {
37         /**
38          * Instances of this class
39          */
40         private static $registryInstances = [];
41
42         /**
43          * Protected constructor
44          *
45          * @return      void
46          */
47         private function __construct () {
48                 // Call parent constructor
49                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('OBJECT-REGISTRY: CONSTRUCTED!');
50                 parent::__construct(__CLASS__);
51
52                 // Init own array
53                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('OBJECT-REGISTRY: Invoking this->initGenericArrayGroup(registry,instance) ...');
54                 $this->initGenericArrayGroup('registry', 'instance');
55
56                 // Trace message
57                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('OBJECT-REGISTRY: EXIT!');
58         }
59
60         /**
61          * Singleton getter for self instance. This class has no factory pattern
62          * because here is no need for special parameters.
63          *
64          * @param       $key    Key for for this instance
65          * @return      $registryInstance       Instance of this class
66          * @throws      InvalidArgumentException        If a parameter has an invalid value
67          */
68         public static final function getRegistry (string $key) {
69                 // Check parameter
70                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: key=%s - CALLED!', $key));
71                 if (empty($key)) {
72                         // Throw IAE
73                         throw new InvalidArgumentException('Parameter "key" is empty');
74                 }
75
76                 // Is an instance there?
77                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('OBJECT-REGISTRY: self::registryInstance[%s]?=%d', $key, intval(isset(self::$registryInstances[$key]))));
78                 if (!isset(self::$registryInstances[$key])) {
79                         // Not yet, so create one
80                         self::$registryInstances[$key] = new ObjectRegistry();
81                 }
82
83                 // Return the instance
84                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: self::registryInstance[%s]=%s - EXIT!', $key, self::$registryInstances[$key]->__toString()));
85                 return self::$registryInstances[$key];
86         }
87
88         /**
89          * Checks whether an instance key was found
90          *
91          * @param       $instanceKey    The key holding an instance in registry
92          * @return      $exists                 Whether the key exists in registry
93          * @throws      InvalidArgumentException        If a paramter has an invalid value
94          */
95         public function instanceExists (string $instanceKey) {
96                 // Check parameter
97                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: instanceKey=%s - CALLED!', $instanceKey));
98                 if (empty($instanceKey)) {
99                         // Throw IAE
100                         throw new InvalidArgumentException('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
101                 }
102
103                 // Does this key exists?
104                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: Invoking this->isGenericArrayKeySet(registry, instance, %s) ...', $instanceKey));
105                 $exists = $this->isGenericArrayKeySet('registry', 'instance', $instanceKey);
106
107                 // Return the result
108                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: exists=%d - EXIT!', intval($exists)));
109                 return $exists;
110         }
111
112         /**
113          * Getter for whole instance registry
114          *
115          * @return      $instanceRegistry       The whole instance registry array
116          */
117         public final function getInstanceRegistry () {
118                 return $this->getGenericSubArray('registry', 'instance');
119         }
120
121         /**
122          * Adds/overwrites a new instance to the registry at the given key
123          *
124          * @param       $instanceKey            The key to identify the instance
125          * @param       $objectInstance         An instance we shall store
126          * @return      void
127          * @throws      InvalidArgumentException        If a paramter has an invalid value
128          */
129         public function addInstance (string $instanceKey, Registerable $objectInstance) {
130                 // Check parameter
131                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: instanceKey=%s,objectInstance=%s - CALLED!', $instanceKey, $objectInstance->__toString()));
132                 if (empty($instanceKey)) {
133                         // Throw IAE
134                         throw new InvalidArgumentExeption('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
135                 }
136
137                 // Set entry in generic array
138                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: Invoking this->setGenericArrayKey(registry,instance,%s,%s) ...', $instanceKey, $objectInstance->__toString()));
139                 $this->setGenericArrayKey('registry', 'instance', $instanceKey, $objectInstance);
140
141                 // Trace message
142                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('OBJECT-REGISTRY: EXIT!');
143         }
144
145         /**
146          * Gets a registered instance or null if not found
147          *
148          * @param       $instanceKey            The key to identify the instance
149          * @return      $objectInstance         An instance we shall store
150          * @throws      InvalidArgumentException        If a paramter has an invalid value
151          * @throws      NullPointerException    If the requested key is not found
152          */
153         public function getInstance (string $instanceKey) {
154                 // Check parameter
155                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: instanceKey=%s - CALLED!', $instanceKey));
156                 if (empty($instanceKey)) {
157                         // Throw IAE
158                         throw new InvalidArgumentException('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
159                 }
160
161                 // Is the instance there?
162                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: Invoking this->instanceExists(%s) ...', $instanceKey));
163                 if (!$this->instanceExists($instanceKey)) {
164                         // This might happen if a non-registered key was requested
165                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
166                 }
167
168                 // Get the instance
169                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: Invoking this->getGenericArrayKey(registry,instance,%s) ...', $instanceKey));
170                 $objectInstance = $this->getGenericArrayKey('registry', 'instance', $instanceKey);
171
172                 // Return the result
173                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: objectInstance=%s - EXIT!', $objectInstance->__toString()));
174                 return $objectInstance;
175         }
176
177 }