]> git.mxchange.org Git - core.git/blob - framework/main/classes/factories/database/class_DatabaseFrontendFactory.php
986166a8f32348836447958e04a75261c99ee7c6
[core.git] / framework / main / classes / factories / database / class_DatabaseFrontendFactory.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Factory\Database\Frontend;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\BaseFactory;
7 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
9 use Org\Mxchange\CoreFramework\Registry\Object\ObjectRegistry;
10
11 // Import SPL stuff
12 use \InvalidArgumentException;
13
14 /**
15  * A factory class for socket registries
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
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
22  *
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.
27  *
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.
32  *
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/>.
35  */
36 class DatabaseFrontendFactory extends BaseFactory {
37         /**
38          * "Cache" for frontend factory
39          */
40         private static $registryInstance = NULL;
41
42         /**
43          * Protected constructor
44          *
45          * @return      void
46          */
47         private function __construct () {
48                 // Call parent constructor
49                 parent::__construct(__CLASS__);
50         }
51
52         /**
53          * Some "static initializer
54          *
55          * @return      void
56          */
57         public final static function staticInitializer () {
58                 // Is it initialized?
59                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('frontend-FACTORY: self::registryInstance[]=%s - CALLED!', gettype(self::$registryInstance)));
60                 if (is_null(self::$registryInstance)) {
61                         // No, then initialize it
62                         self::$registryInstance = ObjectRegistry::getRegistry('factory');
63                 }
64
65                 // Trace message
66                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('frontend-FACTORY: self::registryInstance=%s - EXIT!', self::$registryInstance));
67         }
68
69         /**
70          * Returns a singleton socket registry instance. If an instance is found in
71          * the registry it will be returned, else a new instance is created and
72          * stored in the same registry entry.
73          *
74          * @return      $frontendInstance       A database frontend instance
75          */
76         public static final function createFrontendByConfiguredName (string $frontendName) {
77                 // Check parameter
78                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATABASE-FRONTEND-FACTORY: frontendName=%s - CALLED!', $frontendName));
79                 if (empty($frontendName)) {
80                         // Throw IAE
81                         throw new InvalidArgumentException('Parameter "frontendName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
82                 }
83
84                 // Invoke "static initializer"
85                 self::staticInitializer();
86
87                 // Do we have an instance in the registry?
88                 if (self::$registryInstance->instanceExists($frontendName)) {
89                         // Then use this instance
90                         $frontendInstance = self::$registryInstance->getInstance($frontendName);
91                 } else {
92                         // Get the registry instance
93                         $frontendInstance = ObjectFactory::createObjectByConfiguredName($frontendName);
94
95                         // Set the instance in registry for further use
96                         self::$registryInstance->addInstance($frontendName, $frontendInstance);
97                 }
98
99                 // Return the instance
100                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATABASE-FRONTEND-FACTORY: frontendInstance=%s - EXIT!', $frontendInstance->__toString()));
101                 return $frontendInstance;
102         }
103
104 }