From 0c3d6448ad38ee6a42593982418f35fd43655c4c Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Thu, 13 Jan 2011 10:08:20 -0700 Subject: [PATCH] Inject DB object into Turba_Driver_Sql --- turba/lib/Driver/Sql.php | 20 ++++++++------------ turba/lib/Injector/Factory/Driver.php | 22 +++++++++++++++++++--- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/turba/lib/Driver/Sql.php b/turba/lib/Driver/Sql.php index 93c9c2fa7..dc18075e0 100644 --- a/turba/lib/Driver/Sql.php +++ b/turba/lib/Driver/Sql.php @@ -40,23 +40,19 @@ class Turba_Driver_Sql extends Turba_Driver protected $_db; /** + * Constructor. * - * @param string $name - * @param array $params + * @param array $params Additional parameters needed: + *
+     * 'db' - (Horde_Db_Adapter) A DB Adapter object.
+     * 
*/ public function __construct($name = '', $params = array()) { + $this->_db = $params['db']; + unset($params['db']); + parent::__construct($name, $params); - // TODO: Move to injector - if (empty($this->_params['sql'])) { - try { - $this->_db = $GLOBALS['injector']->getInstance('Horde_Db_Adapter'); - } catch (Horde_Db_Exception $e) { - throw new Turba_Exception($e); - } - } else { - $this->_db = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Db')->create('turba', $this->_params['sql']); - } } /** diff --git a/turba/lib/Injector/Factory/Driver.php b/turba/lib/Injector/Factory/Driver.php index 1ff319591..74ce00aa8 100644 --- a/turba/lib/Injector/Factory/Driver.php +++ b/turba/lib/Injector/Factory/Driver.php @@ -77,12 +77,28 @@ class Turba_Injector_Factory_Driver if (!isset($this->_instances[$key])) { $class = 'Turba_Driver_' . ucfirst(basename($srcConfig['type'])); - if (class_exists($class)) { - $driver = new $class($srcName, $srcConfig['params']); - } else { + if (!class_exists($class)) { throw new Turba_Exception(sprintf(_("Unable to load the definition of %s."), $class)); } + if (empty($srcConfig['params'])) { + $srcConfig['params'] = array(); + } + + switch ($class) { + case 'Turba_Driver_Sql': + try { + $srcConfig['params']['db'] = empty($srcConfig['params']['sql']) + ? $GLOBALS['injector']->getInstance('Horde_Db_Adapter') + : $GLOBALS['injector']->getInstance('Horde_Core_Factory_Db')->create('turba', $this->_params['sql']); + } catch (Horde_Db_Exception $e) { + throw new Turba_Exception($e); + } + break; + } + + $driver = new $class($srcName, $srcConfig['params']); + // Title $driver->title = $srcConfig['title']; -- 2.11.0