3 namespace Org\Mxchange\CoreFramework\Registry\Object;
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;
11 use \InvalidArgumentException;
14 * A registry for several data types and objects. Objects should be added by
15 * addInstance() and therefore must implement the interface Registerable.
17 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
36 class ObjectRegistry extends BaseRegistry implements ObjectRegister {
38 * Instances of this class
40 private static $registryInstances = [];
43 * Protected constructor
47 private function __construct () {
48 // Call parent constructor
49 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('OBJECT-REGISTRY: CONSTRUCTED!');
50 parent::__construct(__CLASS__);
53 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('OBJECT-REGISTRY: Invoking this->initGenericArrayGroup(registry,instance) ...');
54 $this->initGenericArrayGroup('registry', 'instance');
57 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('OBJECT-REGISTRY: EXIT!');
61 * Singleton getter for self instance. This class has no factory pattern
62 * because here is no need for special parameters.
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
68 public static final function getRegistry (string $key) {
70 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: key=%s - CALLED!', $key));
73 throw new InvalidArgumentException('Parameter "key" is empty');
76 // Is an instance there?
77 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('OBJECT-REGISTRY: self::registryInstance[%s][]=%s', $key, gettype(self::$registryInstances[$key])));
78 if (is_null(self::$registryInstances[$key])) {
79 // Not yet, so create one
80 self::$registryInstances[$key] = new ObjectRegistry();
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];
89 * Checks whether an instance key was found
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
95 public function instanceExists (string $instanceKey) {
97 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: instanceKey=%s - CALLED!', $instanceKey));
98 if (empty($instanceKey)) {
100 throw new InvalidArgumentException('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
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);
108 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: exists=%d - EXIT!', intval($exists)));
113 * Getter for whole instance registry
115 * @return $instanceRegistry The whole instance registry array
117 public final function getInstanceRegistry () {
118 return $this->getGenericSubArray('registry', 'instance');
122 * Adds/overwrites a new instance to the registry at the given key
124 * @param $instanceKey The key to identify the instance
125 * @param $objectInstance An instance we shall store
127 * @throws InvalidArgumentException If a paramter has an invalid value
129 public function addInstance (string $instanceKey, Registerable $objectInstance) {
131 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: instanceKey=%s,objectInstance=%s - CALLED!', $instanceKey, $objectInstance->__toString()));
132 if (empty($instanceKey)) {
134 throw new InvalidArgumentExeption('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
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);
142 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('OBJECT-REGISTRY: EXIT!');
146 * Gets a registered instance or null if not found
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
153 public function getInstance (string $instanceKey) {
155 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: instanceKey=%s - CALLED!', $instanceKey));
156 if (empty($instanceKey)) {
158 throw new InvalidArgumentException('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
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);
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);
173 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: objectInstance=%s - EXIT!', $objectInstance->__toString()));
174 return $objectInstance;