3 namespace Org\Mxchange\CoreFramework\Registry\Object;
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;
12 use \InvalidArgumentException;
15 * A registry for several data types and objects. Objects should be added by
16 * addInstance() and therefore must implement the interface Registerable.
18 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
37 class ObjectRegistry extends BaseRegistry implements ObjectRegister {
39 * Instances of this class
41 private static $registryInstances = [];
44 * Protected constructor
48 private function __construct () {
49 // Call parent constructor
50 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('OBJECT-REGISTRY: CONSTRUCTED!');
51 parent::__construct(__CLASS__);
54 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('OBJECT-REGISTRY: Invoking this->initGenericArrayGroup(registry,instance) ...');
55 $this->initGenericArrayGroup('registry', 'instance');
58 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('OBJECT-REGISTRY: EXIT!');
62 * Singleton getter for self instance. This class has no factory pattern
63 * because here is no need for special parameters.
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
69 public static final function getRegistry (string $key) {
71 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: key=%s - CALLED!', $key));
74 throw new InvalidArgumentException('Parameter "key" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
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();
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];
90 * Checks whether an instance key was found
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
96 public function instanceExists (string $instanceKey) {
98 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: instanceKey=%s - CALLED!', $instanceKey));
99 if (empty($instanceKey)) {
101 throw new InvalidArgumentException('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
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);
109 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: exists=%d - EXIT!', intval($exists)));
114 * Getter for whole instance registry
116 * @return $instanceRegistry The whole instance registry array
118 public final function getInstanceRegistry () {
119 return $this->getGenericSubArray('registry', 'instance');
123 * Adds/overwrites a new instance to the registry at the given key
125 * @param $instanceKey The key to identify the instance
126 * @param $objectInstance An instance we shall store
128 * @throws InvalidArgumentException If a paramter has an invalid value
130 public function addInstance (string $instanceKey, Registerable $objectInstance) {
132 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: instanceKey=%s,objectInstance=%s - CALLED!', $instanceKey, $objectInstance->__toString()));
133 if (empty($instanceKey)) {
135 throw new InvalidArgumentExeption('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
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);
143 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('OBJECT-REGISTRY: EXIT!');
147 * Gets a registered instance or null if not found
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
154 public function getInstance (string $instanceKey) {
156 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: instanceKey=%s - CALLED!', $instanceKey));
157 if (empty($instanceKey)) {
159 throw new InvalidArgumentException('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
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, FrameworkInterface::EXCEPTION_IS_NULL_POINTER);
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);
174 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: objectInstance=%s - EXIT!', $objectInstance->__toString()));
175 return $objectInstance;