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