]> git.mxchange.org Git - core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Tue, 28 Feb 2023 09:54:33 +0000 (10:54 +0100)
committerRoland Häder <roland@mxchange.org>
Tue, 28 Feb 2023 09:54:33 +0000 (10:54 +0100)
- ObjectRegistry is now "cached" here to avoid "expensive" calls on getRegisry()
- more logging

framework/main/classes/factories/database/class_DatabaseFrontendFactory.php

index 2448c5b56282ae10c63a7c8c05763cb9e30f1501..986166a8f32348836447958e04a75261c99ee7c6 100644 (file)
@@ -5,8 +5,12 @@ namespace Org\Mxchange\CoreFramework\Factory\Database\Frontend;
 // Import framework stuff
 use Org\Mxchange\CoreFramework\Factory\BaseFactory;
 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
 use Org\Mxchange\CoreFramework\Registry\Object\ObjectRegistry;
 
+// Import SPL stuff
+use \InvalidArgumentException;
+
 /**
  * A factory class for socket registries
  *
@@ -30,6 +34,11 @@ use Org\Mxchange\CoreFramework\Registry\Object\ObjectRegistry;
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 class DatabaseFrontendFactory extends BaseFactory {
+       /**
+        * "Cache" for frontend factory
+        */
+       private static $registryInstance = NULL;
+
        /**
         * Protected constructor
         *
@@ -40,6 +49,23 @@ class DatabaseFrontendFactory extends BaseFactory {
                parent::__construct(__CLASS__);
        }
 
+       /**
+        * Some "static initializer
+        *
+        * @return      void
+        */
+       public final static function staticInitializer () {
+               // Is it initialized?
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('frontend-FACTORY: self::registryInstance[]=%s - CALLED!', gettype(self::$registryInstance)));
+               if (is_null(self::$registryInstance)) {
+                       // No, then initialize it
+                       self::$registryInstance = ObjectRegistry::getRegistry('factory');
+               }
+
+               // Trace message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('frontend-FACTORY: self::registryInstance=%s - EXIT!', self::$registryInstance));
+       }
+
        /**
         * Returns a singleton socket registry instance. If an instance is found in
         * the registry it will be returned, else a new instance is created and
@@ -48,22 +74,30 @@ class DatabaseFrontendFactory extends BaseFactory {
         * @return      $frontendInstance       A database frontend instance
         */
        public static final function createFrontendByConfiguredName (string $frontendName) {
-               // Get registry instance
-               $registryInstance = ObjectRegistry::getRegistry('factory');
+               // Check parameter
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATABASE-FRONTEND-FACTORY: frontendName=%s - CALLED!', $frontendName));
+               if (empty($frontendName)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "frontendName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               }
+
+               // Invoke "static initializer"
+               self::staticInitializer();
 
                // Do we have an instance in the registry?
-               if ($registryInstance->instanceExists($frontendName)) {
+               if (self::$registryInstance->instanceExists($frontendName)) {
                        // Then use this instance
-                       $frontendInstance = $registryInstance->getInstance($frontendName);
+                       $frontendInstance = self::$registryInstance->getInstance($frontendName);
                } else {
                        // Get the registry instance
                        $frontendInstance = ObjectFactory::createObjectByConfiguredName($frontendName);
 
                        // Set the instance in registry for further use
-                       $registryInstance->addInstance($frontendName, $frontendInstance);
+                       self::$registryInstance->addInstance($frontendName, $frontendInstance);
                }
 
                // Return the instance
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DATABASE-FRONTEND-FACTORY: frontendInstance=%s - EXIT!', $frontendInstance->__toString()));
                return $frontendInstance;
        }