]> 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          * Instance of this class
39          */
40         private static $registryInstance = NULL;
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          * @return      $registryInstance       Instance of this class
65          */
66         public static final function getRegistry () {
67                 // Is an instance there?
68                 if (is_null(self::$registryInstance)) {
69                         // Not yet, so create one
70                         self::$registryInstance = new ObjectRegistry();
71                 }
72
73                 // Return the instance
74                 return self::$registryInstance;
75         }
76
77         /**
78          * Checks whether an instance key was found
79          *
80          * @param       $instanceKey    The key holding an instance in registry
81          * @return      $exists                 Whether the key exists in registry
82          * @throws      InvalidArgumentException        If a paramter has an invalid value
83          */
84         public function instanceExists (string $instanceKey) {
85                 // Check parameter
86                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: instanceKey=%s - CALLED!', $instanceKey));
87                 if (empty($instanceKey)) {
88                         // Throw IAE
89                         throw new InvalidArgumentException('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
90                 }
91
92                 // Does this key exists?
93                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: Invoking this->isGenericArrayKeySet(registry, instance, %s) ...', $instanceKey));
94                 $exists = $this->isGenericArrayKeySet('registry', 'instance', $instanceKey);
95
96                 // Return the result
97                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: exists=%d - EXIT!', intval($exists)));
98                 return $exists;
99         }
100
101         /**
102          * Getter for whole instance registry
103          *
104          * @return      $instanceRegistry       The whole instance registry array
105          */
106         public final function getInstanceRegistry () {
107                 return $this->getGenericSubArray('registry', 'instance');
108         }
109
110         /**
111          * Adds/overwrites a new instance to the registry at the given key
112          *
113          * @param       $instanceKey            The key to identify the instance
114          * @param       $objectInstance         An instance we shall store
115          * @return      void
116          * @throws      InvalidArgumentException        If a paramter has an invalid value
117          */
118         public function addInstance (string $instanceKey, Registerable $objectInstance) {
119                 // Check parameter
120                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: instanceKey=%s,objectInstance=%s - CALLED!', $instanceKey, $objectInstance->__toString()));
121                 if (empty($instanceKey)) {
122                         // Throw IAE
123                         throw new InvalidArgumentExeption('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
124                 }
125
126                 // Set entry in generic array
127                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: Invoking this->setGenericArrayKey(registry,instance,%s,%s) ...', $instanceKey, $objectInstance->__toString()));
128                 $this->setGenericArrayKey('registry', 'instance', $instanceKey, $objectInstance);
129
130                 // Trace message
131                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('OBJECT-REGISTRY: EXIT!');
132         }
133
134         /**
135          * Gets a registered instance or null if not found
136          *
137          * @param       $instanceKey            The key to identify the instance
138          * @return      $objectInstance         An instance we shall store
139          * @throws      InvalidArgumentException        If a paramter has an invalid value
140          * @throws      NullPointerException    If the requested key is not found
141          */
142         public function getInstance (string $instanceKey) {
143                 // Check parameter
144                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: instanceKey=%s - CALLED!', $instanceKey));
145                 if (empty($instanceKey)) {
146                         // Throw IAE
147                         throw new InvalidArgumentException('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
148                 }
149
150                 // Is the instance there?
151                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: Invoking this->instanceExists(%s) ...', $instanceKey));
152                 if (!$this->instanceExists($instanceKey)) {
153                         // This might happen if a non-registered key was requested
154                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
155                 }
156
157                 // Get the instance
158                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: Invoking this->getGenericArrayKey(registry,instance,%s) ...', $instanceKey));
159                 $objectInstance = $this->getGenericArrayKey('registry', 'instance', $instanceKey);
160
161                 // Return the result
162                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: objectInstance=%s - EXIT!', $objectInstance->__toString()));
163                 return $objectInstance;
164         }
165
166 }