From: Michael J. Rubinsky Date: Thu, 13 Jan 2011 16:40:40 +0000 (-0500) Subject: Move Turba_Driver::factory() functionality to the injector factory. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=1a0dbbc298f77dea13d86d8c11045998703e4a9d;p=horde.git Move Turba_Driver::factory() functionality to the injector factory. All code should be using the injector to get the driver anyway, no need for extra factory method. Remove _init() methods from driver subclasses, move functionality into the const'r instead. --- diff --git a/turba/lib/Api.php b/turba/lib/Api.php index 2ed619e54..f23e902df 100644 --- a/turba/lib/Api.php +++ b/turba/lib/Api.php @@ -134,7 +134,7 @@ class Turba_Api extends Horde_Registry_Api } try { - $driver = Turba_Driver::factory($params['source'], $cfgSources[$params['source']]); + $driver = $GLOBALS['injector']->getInstance('Turba_Injector_Factory_Driver')->create($params['source']); if ($driver->checkDefaultShare($share, $cfgSources[$params['source']])) { return $uid; } diff --git a/turba/lib/Driver.php b/turba/lib/Driver.php index a6d619c44..bedaeafeb 100644 --- a/turba/lib/Driver.php +++ b/turba/lib/Driver.php @@ -118,8 +118,9 @@ class Turba_Driver implements Countable * @param array $params Hash containing additional configuration * parameters. */ - public function __construct($params = array()) + public function __construct($name = '', $params = array()) { + $this->_name = $name; $this->_params = $params; } diff --git a/turba/lib/Driver/Imsp.php b/turba/lib/Driver/Imsp.php index ad8bfafe2..6766323fb 100644 --- a/turba/lib/Driver/Imsp.php +++ b/turba/lib/Driver/Imsp.php @@ -74,20 +74,14 @@ class Turba_Driver_Imsp extends Turba_Driver */ public function __construct($params) { + parent::__construct($name, $params); + $this->params = $params; $this->_groupField = $params['group_id_field']; $this->_groupValue = $params['group_id_value']; $this->_myRights = $params['my_rights']; $this->_perms = $this->_aclToHordePerms($params['my_rights']); - } - /** - * Initialize the IMSP connection and check for error. - * - * @throws Turba_Exception - */ - protected function _init() - { global $conf; $this->_bookName = $this->getContactOwner(); diff --git a/turba/lib/Driver/Kolab.php b/turba/lib/Driver/Kolab.php index 9acc76f33..569edf62d 100644 --- a/turba/lib/Driver/Kolab.php +++ b/turba/lib/Driver/Kolab.php @@ -41,8 +41,9 @@ class Turba_Driver_Kolab extends Turba_Driver /** * Attempts to open a Kolab Groupware folder. */ - protected function _init() + public function __construct($name = '', $params = array()) { + parent::__construct($name, $params); $this->_kolab = new Kolab(); $wrapper = empty($this->_kolab->version) ? 'Turba_Driver_Kolab_Wrapper_old' diff --git a/turba/lib/Driver/Ldap.php b/turba/lib/Driver/Ldap.php index 8b9d7e62b..fc1018dd1 100644 --- a/turba/lib/Driver/Ldap.php +++ b/turba/lib/Driver/Ldap.php @@ -54,13 +54,6 @@ class Turba_Driver_Ldap extends Turba_Driver ), $params); parent::__construct($params); - } - - /** - * @throws Turba_Exception - */ - protected function _init() - { if (!Horde_Util::extensionExists('ldap')) { throw new Turba_Exception(_("LDAP support is required but the LDAP module is not available or not loaded.")); } diff --git a/turba/lib/Driver/Share.php b/turba/lib/Driver/Share.php index 4bba41223..8860e2afe 100644 --- a/turba/lib/Driver/Share.php +++ b/turba/lib/Driver/Share.php @@ -32,6 +32,21 @@ class Turba_Driver_Share extends Turba_Driver protected $_driver; /** + * Constructor + * + * @param string $name The source name + * @param array $params The parameter array describing the source + * + * @return Turba_Driver + */ + public function __construct($name = '', $params = array()) + { + parent::__construct($name, $params); + $this->_share = $this->_params['config']['params']['share']; + $this->_driver = $GLOBALS['injector']->getInstance('Turba_Injector_Factory_Driver')->create($this->_params['config']); + } + + /** * Checks if this backend has a certain capability. * * @param string $capability The capability to check for. @@ -85,15 +100,6 @@ class Turba_Driver_Share extends Turba_Driver } /** - * @throws Turba_Exception - */ - protected function _init() - { - $this->_share = &$this->_params['config']['params']['share']; - $this->_driver = Turba_Driver::factory($this->_name, $this->_params['config']); - } - - /** * Searches the address book with the given criteria and returns a * filtered list of results. If the criteria parameter is an empty array, * all records will be returned. diff --git a/turba/lib/Driver/Sql.php b/turba/lib/Driver/Sql.php index a54ca9aa8..93c9c2fa7 100644 --- a/turba/lib/Driver/Sql.php +++ b/turba/lib/Driver/Sql.php @@ -40,11 +40,14 @@ class Turba_Driver_Sql extends Turba_Driver protected $_db; /** - * @throws Turba_Exception + * + * @param string $name + * @param array $params */ - protected function _init() + public function __construct($name = '', $params = array()) { - // TODO: Move to injector + parent::__construct($name, $params); + // TODO: Move to injector if (empty($this->_params['sql'])) { try { $this->_db = $GLOBALS['injector']->getInstance('Horde_Db_Adapter'); diff --git a/turba/lib/Driver/Vbook.php b/turba/lib/Driver/Vbook.php index 4e7e7401e..8c600b266 100644 --- a/turba/lib/Driver/Vbook.php +++ b/turba/lib/Driver/Vbook.php @@ -29,21 +29,12 @@ class Turba_Driver_Vbook extends Turba_Driver public $searchCriteria; /** - * Return the owner to use when searching or creating contacts in - * this address book. - * - * @return string - */ - protected function _getContactOwner() - { - return $this->_driver->getContactOwner(); - } - - /** * @throws Turba_Exception */ - protected function _init() + public function __construct($name = '', $params = array()) { + parent::__construct($name, $params); + /* Grab a reference to the share for this vbook. */ $this->_share = $this->_params['share']; @@ -59,6 +50,17 @@ class Turba_Driver_Vbook extends Turba_Driver } /** + * Return the owner to use when searching or creating contacts in + * this address book. + * + * @return string + */ + protected function _getContactOwner() + { + return $this->_driver->getContactOwner(); + } + + /** * Return all entries matching the combined searches represented by * $criteria and the vitural address book's search criteria. * diff --git a/turba/lib/Injector/Factory/Driver.php b/turba/lib/Injector/Factory/Driver.php index 658e8b256..1ff319591 100644 --- a/turba/lib/Injector/Factory/Driver.php +++ b/turba/lib/Injector/Factory/Driver.php @@ -76,7 +76,46 @@ class Turba_Injector_Factory_Driver } if (!isset($this->_instances[$key])) { - $this->_instances[$key] = Turba_Driver::factory($srcName, $srcConfig); + $class = 'Turba_Driver_' . ucfirst(basename($srcConfig['type'])); + if (class_exists($class)) { + $driver = new $class($srcName, $srcConfig['params']); + } else { + throw new Turba_Exception(sprintf(_("Unable to load the definition of %s."), $class)); + } + + // Title + $driver->title = $srcConfig['title']; + + /* Initialize */ + //$driver->_init(); + + /* Store and translate the map at the Source level. */ + $driver->map = $srcConfig['map']; + foreach ($driver->map as $key => $val) { + if (!is_array($val)) { + $driver->fields[$key] = $val; + } + } + + /* Store tabs. */ + if (isset($srcConfig['tabs'])) { + $driver->tabs = $srcConfig['tabs']; + } + + /* Store remaining fields. */ + if (isset($srcConfig['strict'])) { + $driver->strict = $srcConfig['strict']; + } + if (isset($srcConfig['approximate'])) { + $driver->approximate = $srcConfig['approximate']; + } + if (isset($srcConfig['list_name_field'])) { + $driver->listNameField = $srcConfig['list_name_field']; + } + if (isset($srcConfig['alternative_name'])) { + $driver->alternativeName = $srcConfig['alternative_name']; + } + $this->_instances[$key] = $driver; } return $this->_instances[$key];