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 * Instance of this class
40 private static $registryInstance = NULL;
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 * @return $registryInstance Instance of this class
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();
73 // Return the instance
74 return self::$registryInstance;
78 * Checks whether an instance key was found
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
84 public function instanceExists (string $instanceKey) {
86 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: instanceKey=%s - CALLED!', $instanceKey));
87 if (empty($instanceKey)) {
89 throw new InvalidArgumentException('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
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);
97 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: exists=%d - EXIT!', intval($exists)));
102 * Getter for whole instance registry
104 * @return $instanceRegistry The whole instance registry array
106 public final function getInstanceRegistry () {
107 return $this->getGenericSubArray('registry', 'instance');
111 * Adds/overwrites a new instance to the registry at the given key
113 * @param $instanceKey The key to identify the instance
114 * @param $objectInstance An instance we shall store
116 * @throws InvalidArgumentException If a paramter has an invalid value
118 public function addInstance (string $instanceKey, Registerable $objectInstance) {
120 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: instanceKey=%s,objectInstance=%s - CALLED!', $instanceKey, $objectInstance->__toString()));
121 if (empty($instanceKey)) {
123 throw new InvalidArgumentExeption('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
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);
131 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('OBJECT-REGISTRY: EXIT!');
135 * Gets a registered instance or null if not found
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
142 public function getInstance (string $instanceKey) {
144 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: instanceKey=%s - CALLED!', $instanceKey));
145 if (empty($instanceKey)) {
147 throw new InvalidArgumentException('Parameter "instanceKey" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
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);
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);
162 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: objectInstance=%s - EXIT!', $objectInstance->__toString()));
163 return $objectInstance;