From e04c49d97b6baa6e357f6b941e8929fc14ed09cb Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Fri, 1 Mar 2013 10:40:14 -0500 Subject: [PATCH] Dependency injection for database adapter factory. --- module/VuFind/config/module.config.php | 7 +++- .../VuFind/Controller/InstallController.php | 10 ++--- .../VuFind/Controller/UpgradeController.php | 9 +++-- .../VuFind/src/VuFind/Db/AdapterFactory.php | 39 +++++++++++++------ .../VuFind/src/VuFindTest/Unit/DbTestCase.php | 5 +-- 5 files changed, 46 insertions(+), 24 deletions(-) diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php index 2de8018f95d..e75eb35462f 100644 --- a/module/VuFind/config/module.config.php +++ b/module/VuFind/config/module.config.php @@ -179,7 +179,12 @@ $config = array( ); }, 'VuFind\DbAdapter' => function ($sm) { - return \VuFind\Db\AdapterFactory::getAdapter(); + return $sm->get('VuFind\DbAdapterFactory')->getAdapter(); + }, + 'VuFind\DbAdapterFactory' => function ($sm) { + return new \VuFind\Db\AdapterFactory( + $sm->get('VuFind\Config')->get('config') + ); }, 'VuFind\Export' => function ($sm) { return new \VuFind\Export( diff --git a/module/VuFind/src/VuFind/Controller/InstallController.php b/module/VuFind/src/VuFind/Controller/InstallController.php index 0af2824c2c1..39a6ed6fd89 100644 --- a/module/VuFind/src/VuFind/Controller/InstallController.php +++ b/module/VuFind/src/VuFind/Controller/InstallController.php @@ -29,7 +29,6 @@ namespace VuFind\Controller; use VuFind\Config\Locator as ConfigLocator, VuFind\Config\Writer as ConfigWriter, VuFind\Connection\Manager as ConnectionManager, - VuFind\Db\AdapterFactory, Zend\Mvc\MvcEvent, Zend\Crypt\Password\Bcrypt; @@ -338,9 +337,8 @@ class InstallController extends AbstractBase . $this->params()->fromPost('dbrootpass') . '@' . $view->dbhost; try { - $db = AdapterFactory::getAdapterFromConnectionString( - $connection . '/mysql' - ); + $db = $this->getServiceLocator()->get('VuFind\DbAdapterFactory') + ->getAdapterFromConnectionString($connection . '/mysql'); } catch (\Exception $e) { $this->flashMessenger()->setNamespace('error') ->addMessage( @@ -370,7 +368,9 @@ class InstallController extends AbstractBase $db->query($query, $db::QUERY_MODE_EXECUTE); $db->query($grant, $db::QUERY_MODE_EXECUTE); $db->query('FLUSH PRIVILEGES', $db::QUERY_MODE_EXECUTE); - $db = AdapterFactory::getAdapterFromConnectionString( + $dbFactory = $this->getServiceLocator() + ->get('VuFind\DbAdapterFactory'); + $db = $dbFactory->getAdapterFromConnectionString( $connection . '/' . $view->dbname ); $statements = explode(';', $sql); diff --git a/module/VuFind/src/VuFind/Controller/UpgradeController.php b/module/VuFind/src/VuFind/Controller/UpgradeController.php index 0eb57ab6765..977599cd798 100644 --- a/module/VuFind/src/VuFind/Controller/UpgradeController.php +++ b/module/VuFind/src/VuFind/Controller/UpgradeController.php @@ -27,7 +27,7 @@ */ namespace VuFind\Controller; use ArrayObject, VuFind\Config\Locator as ConfigLocator, - VuFind\Cookie\Container as CookieContainer, VuFind\Db\AdapterFactory, + VuFind\Cookie\Container as CookieContainer, VuFind\Exception\RecordMissing as RecordMissingException, Zend\Session\Container as SessionContainer; @@ -171,7 +171,8 @@ class UpgradeController extends AbstractBase // subsequent calls. static $adapter = false; if (!$adapter) { - $adapter = AdapterFactory::getAdapter( + $factory = $this->getServiceLocator()->get('VuFind\DbAdapterFactory'); + $adapter = $factory->getAdapter( $this->session->dbRootUser, $this->session->dbRootPass ); } @@ -369,7 +370,9 @@ class UpgradeController extends AbstractBase // Test the connection: try { // Query a table known to exist - $db = AdapterFactory::getAdapter($dbrootuser, $pass); + $factory = $this->getServiceLocator() + ->get('VuFind\DbAdapterFactory'); + $db = $factory->getAdapter($dbrootuser, $pass); $db->query("SELECT * FROM user;"); $this->session->dbRootUser = $dbrootuser; $this->session->dbRootPass = $pass; diff --git a/module/VuFind/src/VuFind/Db/AdapterFactory.php b/module/VuFind/src/VuFind/Db/AdapterFactory.php index 513381b8c3a..feb880972ee 100644 --- a/module/VuFind/src/VuFind/Db/AdapterFactory.php +++ b/module/VuFind/src/VuFind/Db/AdapterFactory.php @@ -39,6 +39,23 @@ use VuFind\Config\Reader as ConfigReader, Zend\Db\Adapter\Adapter; */ class AdapterFactory { + /** + * VuFind configuration + * + * @var \Zend\Config\Config + */ + protected $config; + + /** + * Constructor + * + * @param \Zend\Config\Config $config VuFind configuration + */ + public function __construct(\Zend\Config\Config $config) + { + $this->config = $config; + } + /** * Obtain a Zend\DB connection using standard VuFind configuration. * @@ -49,12 +66,11 @@ class AdapterFactory * * @return object */ - public static function getAdapter($overrideUser = null, $overridePass = null) + public function getAdapter($overrideUser = null, $overridePass = null) { // Parse details from connection string: - $config = ConfigReader::getConfig(); - return static::getAdapterFromConnectionString( - $config->Database->database, $overrideUser, $overridePass + return $this->getAdapterFromConnectionString( + $this->config->Database->database, $overrideUser, $overridePass ); } @@ -65,7 +81,7 @@ class AdapterFactory * * @return string */ - public static function getDriverName($type) + public function getDriverName($type) { switch (strtolower($type)) { case 'mysql': @@ -85,14 +101,13 @@ class AdapterFactory * * @return object */ - public static function getAdapterFromOptions($options) + public function getAdapterFromOptions($options) { // Set up custom options by database type: switch (strtolower($options['driver'])) { case 'mysqli': - $config = ConfigReader::getConfig(); - $options['charset'] = isset($config->Database->charset) - ? $config->Database->charset : 'utf8'; + $options['charset'] = isset($this->config->Database->charset) + ? $this->config->Database->charset : 'utf8'; $options['options'] = array('buffer_results' => true); break; } @@ -113,7 +128,7 @@ class AdapterFactory * * @return object */ - public static function getAdapterFromConnectionString($connectionString, + public function getAdapterFromConnectionString($connectionString, $overrideUser = null, $overridePass = null ) { list($type, $details) = explode('://', $connectionString); @@ -132,13 +147,13 @@ class AdapterFactory // Set up default options: $options = array( - 'driver' => static::getDriverName($type), + 'driver' => $this->getDriverName($type), 'hostname' => $host, 'username' => $username, 'password' => $password, 'database' => $dbName ); - return static::getAdapterFromOptions($options); + return $this->getAdapterFromOptions($options); } } diff --git a/module/VuFind/src/VuFindTest/Unit/DbTestCase.php b/module/VuFind/src/VuFindTest/Unit/DbTestCase.php index b3a9a35769b..938074a3b22 100644 --- a/module/VuFind/src/VuFindTest/Unit/DbTestCase.php +++ b/module/VuFind/src/VuFindTest/Unit/DbTestCase.php @@ -52,9 +52,8 @@ abstract class DbTestCase extends TestCase // Add database service: if (!$sm->has('VuFind\DbTablePluginManager')) { - $sm->setService( - 'VuFind\DbAdapter', \VuFind\Db\AdapterFactory::getAdapter() - ); + $dbFactory = new \VuFind\Db\AdapterFactory($sm->get('VuFind\Config')); + $sm->setService('VuFind\DbAdapter', $dbFactory->getAdapter()); $factory = new \VuFind\Db\Table\PluginManager( new \Zend\ServiceManager\Config( array( -- GitLab