]> git.mxchange.org Git - core.git/blob - framework/main/classes/registry/generic/class_GenericRegistry.php
5c2b3a010c5b87180c1ed3ffaac369923b869c5d
[core.git] / framework / main / classes / registry / generic / class_GenericRegistry.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Registry;
4
5 /**
6  * A registry for several data types and objects. Objects should be added by
7  * addInstance() and therefore must implement the interface Registerable.
8  *
9  * @author              Roland Haeder <webmaster@shipsimu.org>
10  * @version             0.0.0
11  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
12  * @license             GNU GPL 3.0 or any newer version
13  * @link                http://www.shipsimu.org
14  *
15  * This program is free software: you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation, either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program. If not, see <http://www.gnu.org/licenses/>.
27  */
28 class GenericRegistry extends BaseRegistry implements Register {
29         /**
30          * Instance of this class
31          */
32         private static $registryInstance = NULL;
33
34         /**
35          * Protected constructor
36          *
37          * @return      void
38          */
39         private function __construct () {
40                 // Call parent constructor
41                 parent::__construct(__CLASS__);
42         }
43
44         /**
45          * Singleton getter for self instance. This class has no factory pattern
46          * because here is no need for special parameters.
47          *
48          * @param       $key    Key for for this instance
49          * @return      $registryInstance       Instance of this class
50          * @throws      InvalidArgumentException        If a parameter has an invalid value
51          */
52         public static final function getRegistry (string $key) {
53                 // Check parameter
54                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: key=%s - CALLED!', $key));
55                 if (empty($key)) {
56                         // Throw IAE
57                         throw new InvalidArgumentException('Parameter "key" is empty');
58                 }
59
60                 // Is an instance there?
61                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('OBJECT-REGISTRY: self::registryInstance[%s][]=%s', $key, gettype(self::$registryInstances[$key])));
62                 if (is_null(self::$registryInstances[$key])) {
63                         // Not yet, so create one
64                         self::$registryInstances[$key] = new ObjectRegistry();
65                 }
66
67                 // Return the instance
68                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('OBJECT-REGISTRY: self::registryInstance[%s]=%s - EXIT!', $key, self::$registryInstances[$key]->__toString()));
69                 return self::$registryInstances[$key];
70         }
71
72 }