Move Turba_Driver::factory() functionality to the injector factory.
authorMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 13 Jan 2011 16:40:40 +0000 (11:40 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 13 Jan 2011 16:40:40 +0000 (11:40 -0500)
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.

turba/lib/Api.php
turba/lib/Driver.php
turba/lib/Driver/Imsp.php
turba/lib/Driver/Kolab.php
turba/lib/Driver/Ldap.php
turba/lib/Driver/Share.php
turba/lib/Driver/Sql.php
turba/lib/Driver/Vbook.php
turba/lib/Injector/Factory/Driver.php

index 2ed619e..f23e902 100644 (file)
@@ -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;
                     }
index a6d619c..bedaeaf 100644 (file)
@@ -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;
     }
 
index ad8bfaf..6766323 100644 (file)
@@ -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();
index 9acc76f..569edf6 100644 (file)
@@ -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'
index 8b9d7e6..fc1018d 100644 (file)
@@ -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."));
         }
index 4bba412..8860e2a 100644 (file)
@@ -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.
index a54ca9a..93c9c2f 100644 (file)
@@ -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');
index 4e7e740..8c600b2 100644 (file)
@@ -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.
      *
index 658e8b2..1ff3195 100644 (file)
@@ -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];