From cb2db812f0d8ce380b8ace226044f448467e7074 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 6 Mar 2018 22:49:02 +0100 Subject: [PATCH] Continued: - $databaseInstance is now moved to FrameworkBootstrap as the old getter/setter were using registry pattern, then a getter invocation to check if the instance has already been set will result to an NPE as getInstance() makes this sure MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../bootstrap/class_FrameworkBootstrap.php | 66 +++++++++++++------ .../database/class_BaseDatabaseWrapper.php | 19 +++--- .../frontend/class_UserDatabaseWrapper.php | 3 +- .../class_UserPointsDatabaseWrapper.php | 3 +- .../rng/class_RandomNumberGenerator.php | 4 +- 5 files changed, 62 insertions(+), 33 deletions(-) diff --git a/framework/bootstrap/class_FrameworkBootstrap.php b/framework/bootstrap/class_FrameworkBootstrap.php index cb8aaffd..26c419da 100644 --- a/framework/bootstrap/class_FrameworkBootstrap.php +++ b/framework/bootstrap/class_FrameworkBootstrap.php @@ -68,6 +68,11 @@ final class FrameworkBootstrap { */ private static $configurationInstance = NULL; + /** + * Database instance + */ + private static $databaseInstance = NULL; + /* * Includes applications may have. They will be tried in the given order, * some will become soon deprecated. @@ -117,24 +122,6 @@ final class FrameworkBootstrap { return self::$configurationInstance; } - /** - * Getter for request instance - * - * @return $requestInstance An instance of a Requestable class - */ - public static function getRequestInstance () { - return self::$requestInstance; - } - - /** - * Getter for response instance - * - * @return $responseInstance An instance of a Responseable class - */ - public static function getResponseInstance () { - return self::$responseInstance; - } - /** * "Getter" to get response/request type from analysis of the system. * @@ -398,7 +385,7 @@ final class FrameworkBootstrap { $applicationInstance = ApplicationHelper::getSelfInstance(); // Is the database instance already set? - if ($applicationInstance->getDatabaseInstance() instanceof DatabaseConnector) { + if (self::getDatabaseInstance() instanceof DatabaseConnector) { // Yes, then abort here throw new BadMethodCallException('Method called twice.'); } // END - if @@ -410,7 +397,7 @@ final class FrameworkBootstrap { $connectionInstance = DatabaseConnection::createDatabaseConnection(DebugMiddleware::getSelfInstance(), $databaseInstance); // Set it in application helper - $applicationInstance->setDatabaseInstance($connectionInstance); + self::setDatabaseInstance($connectionInstance); } /** @@ -656,6 +643,25 @@ final class FrameworkBootstrap { self::getConfigurationInstance()->setConfigEntry('detected_full_app_path', $applicationPath); self::getConfigurationInstance()->setConfigEntry('detected_app_name' , $application); } + + /** + * Getter for request instance + * + * @return $requestInstance An instance of a Requestable class + */ + public static function getRequestInstance () { + return self::$requestInstance; + } + + /** + * Getter for response instance + * + * @return $responseInstance An instance of a Responseable class + */ + public static function getResponseInstance () { + return self::$responseInstance; + } + /** * Setter for request instance * @@ -676,4 +682,24 @@ final class FrameworkBootstrap { self::$responseInstance = $responseInstance; } + /** + * Setter for database instance + * + * @param $databaseInstance An instance of a DatabaseConnection class + * @return void + */ + public static function setDatabaseInstance (DatabaseConnection $databaseInstance) { + self::$databaseInstance = $databaseInstance; + } + + /** + * Getter for database instance + * + * @return $databaseInstance An instance of a DatabaseConnection class + */ + public static function getDatabaseInstance () { + // Return instance + return self::$databaseInstance; + } + } diff --git a/framework/main/classes/database/class_BaseDatabaseWrapper.php b/framework/main/classes/database/class_BaseDatabaseWrapper.php index d8b47b58..4456a0d1 100644 --- a/framework/main/classes/database/class_BaseDatabaseWrapper.php +++ b/framework/main/classes/database/class_BaseDatabaseWrapper.php @@ -3,6 +3,7 @@ namespace Org\Mxchange\CoreFramework\Database\Frontend; // Import framework stuff +use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Criteria\Criteria; use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria; use Org\Mxchange\CoreFramework\Database\Backend\BaseDatabaseBackend; @@ -112,7 +113,7 @@ abstract class BaseDatabaseWrapper extends BaseFrameworkSystem { } // END - if // Handle it over to the middleware - $this->getDatabaseInstance()->queryInsertDataSet($dataSetInstance); + FrameworkBootstrap::getDatabaseInstance()->queryInsertDataSet($dataSetInstance); } /** @@ -140,7 +141,7 @@ abstract class BaseDatabaseWrapper extends BaseFrameworkSystem { } // END - if // Handle it over to the middleware - $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance); + FrameworkBootstrap::getDatabaseInstance()->queryUpdateDataSet($dataSetInstance); } /** @@ -149,7 +150,7 @@ abstract class BaseDatabaseWrapper extends BaseFrameworkSystem { * @return $indexKey Index key */ public final function getIndexKey () { - return $this->getDatabaseInstance()->getIndexKey(); + return FrameworkBootstrap::getDatabaseInstance()->getIndexKey(); } /** @@ -158,7 +159,7 @@ abstract class BaseDatabaseWrapper extends BaseFrameworkSystem { * @return $lastException Last exception or NULL if none occured */ public final function getLastException () { - return $this->getDatabaseInstance()->getLastException(); + return FrameworkBootstrap::getDatabaseInstance()->getLastException(); } /** @@ -191,7 +192,7 @@ abstract class BaseDatabaseWrapper extends BaseFrameworkSystem { //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: Quering database, cacheKey=' . $cacheKey); // Now it's time to ask the database layer for this select statement - $result = $this->getDatabaseInstance()->doSelectByTableCriteria($this->getTableName(), $criteriaInstance); + $result = FrameworkBootstrap::getDatabaseInstance()->doSelectByTableCriteria($this->getTableName(), $criteriaInstance); //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-WRAPPER: result[]=' . gettype($result)); // Cache the result if not null @@ -205,7 +206,7 @@ abstract class BaseDatabaseWrapper extends BaseFrameworkSystem { // This invalid result must be wrapped $result = array( BaseDatabaseBackend::RESULT_INDEX_STATUS => 'invalid', - BaseDatabaseBackend::RESULT_INDEX_EXCEPTION => $this->getDatabaseInstance()->getLastException() + BaseDatabaseBackend::RESULT_INDEX_EXCEPTION => FrameworkBootstrap::getDatabaseInstance()->getLastException() ); } } @@ -252,7 +253,7 @@ abstract class BaseDatabaseWrapper extends BaseFrameworkSystem { */ public final function getPrimaryKeyValue () { // Get the table name and a database instance and ask for it - $primaryKey = $this->getDatabaseInstance()->getPrimaryKeyOfTable($this->getTableName()); + $primaryKey = FrameworkBootstrap::getDatabaseInstance()->getPrimaryKeyOfTable($this->getTableName()); // Return value return $primaryKey; @@ -265,7 +266,7 @@ abstract class BaseDatabaseWrapper extends BaseFrameworkSystem { */ public final function countTotalRows () { // Get the table name and a database instance and ask for it - $count = $this->getDatabaseInstance()->countTotalRows($this->getTableName()); + $count = FrameworkBootstrap::getDatabaseInstance()->countTotalRows($this->getTableName()); // Return value return $count; @@ -279,7 +280,7 @@ abstract class BaseDatabaseWrapper extends BaseFrameworkSystem { */ public function removeNonPublicDataFromArray (array $data) { //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('WRAPPER[' . $this->__toString() . ']: Calling this->getDatabaseInstance()->removeNonPublicDataFromArray(data) ...'); - $data = $this->getDatabaseInstance()->removeNonPublicDataFromArray($data); + $data = FrameworkBootstrap::getDatabaseInstance()->removeNonPublicDataFromArray($data); //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('WRAPPER[' . $this->__toString() . ']: data[]=' . gettype($data)); return $data; diff --git a/framework/main/classes/database/frontend/class_UserDatabaseWrapper.php b/framework/main/classes/database/frontend/class_UserDatabaseWrapper.php index 9ed16737..2ef48322 100644 --- a/framework/main/classes/database/frontend/class_UserDatabaseWrapper.php +++ b/framework/main/classes/database/frontend/class_UserDatabaseWrapper.php @@ -3,6 +3,7 @@ namespace Org\Mxchange\CoreFramework\Database\Frontend\User; // Import framework stuff +use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Database\Frontend\BaseDatabaseWrapper; use Org\Mxchange\CoreFramework\Factory\ObjectFactory; use Org\Mxchange\CoreFramework\Generic\NullPointerException; @@ -138,7 +139,7 @@ class UserDatabaseWrapper extends BaseDatabaseWrapper implements ManageableAccou $resultInstance->addElementsToDataSet($dataSetInstance); // "Update" this request with the database - $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance); + FrameworkBootstrap::getDatabaseInstance()->queryUpdateDataSet($dataSetInstance); } } diff --git a/framework/main/classes/database/frontend/class_UserPointsDatabaseWrapper.php b/framework/main/classes/database/frontend/class_UserPointsDatabaseWrapper.php index d9242d8a..8a19464e 100644 --- a/framework/main/classes/database/frontend/class_UserPointsDatabaseWrapper.php +++ b/framework/main/classes/database/frontend/class_UserPointsDatabaseWrapper.php @@ -3,6 +3,7 @@ namespace Org\Mxchange\CoreFramework\Database\Frontend\Points; // Import framework stuff +use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Database\Frontend\BaseDatabaseWrapper; use Org\Mxchange\CoreFramework\Factory\ObjectFactory; use Org\Mxchange\CoreFramework\Registry\Registerable; @@ -112,7 +113,7 @@ class UserPointsDatabaseWrapper extends BaseDatabaseWrapper implements BookableP $dataSetInstance->setUniqueKey(self::DB_COLUMN_POINTS_UID); // "Update" this request with the database - $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance); + FrameworkBootstrap::getDatabaseInstance()->queryUpdateDataSet($dataSetInstance); } } diff --git a/framework/main/classes/rng/class_RandomNumberGenerator.php b/framework/main/classes/rng/class_RandomNumberGenerator.php index e1a34ea8..25c4c2c9 100644 --- a/framework/main/classes/rng/class_RandomNumberGenerator.php +++ b/framework/main/classes/rng/class_RandomNumberGenerator.php @@ -130,11 +130,11 @@ class RandomNumberGenerator extends BaseFrameworkSystem { $this->fixedSalt = sha1( $serverIp . ':' . $extraInstance->__toString() . ':' . - json_encode($this->getDatabaseInstance()->getConnectionData()) + json_encode(FrameworkBootstrap::getDatabaseInstance()->getConnectionData()) ); } else { // Without extra information - $this->fixedSalt = sha1($serverIp . ':' . json_encode($this->getDatabaseInstance()->getConnectionData())); + $this->fixedSalt = sha1($serverIp . ':' . json_encode(FrameworkBootstrap::getDatabaseInstance()->getConnectionData())); } // One-way data we need for "extra-salting" the random number -- 2.39.5