From 12e4c5a35325464e30bcfd223f121a105ebca91e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 28 Feb 2023 10:54:33 +0100 Subject: [PATCH] Continued: - ObjectRegistry is now "cached" here to avoid "expensive" calls on getRegisry() - more logging --- .../class_DatabaseFrontendFactory.php | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/framework/main/classes/factories/database/class_DatabaseFrontendFactory.php b/framework/main/classes/factories/database/class_DatabaseFrontendFactory.php index 2448c5b5..986166a8 100644 --- a/framework/main/classes/factories/database/class_DatabaseFrontendFactory.php +++ b/framework/main/classes/factories/database/class_DatabaseFrontendFactory.php @@ -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 . */ 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; } -- 2.39.5