Use injector to get Turba_Driver object
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 26 Aug 2010 19:26:00 +0000 (13:26 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 26 Aug 2010 19:40:51 +0000 (13:40 -0600)
Throw Turba_Exceptions if driver init fails.

38 files changed:
turba/add.php
turba/contact.php
turba/data.php
turba/delete.php
turba/deletefile.php
turba/edit.php
turba/lib/Api.php
turba/lib/Application.php
turba/lib/Driver.php
turba/lib/Driver/Facebook.php
turba/lib/Driver/Favourites.php
turba/lib/Driver/Group.php
turba/lib/Driver/Imsp.php
turba/lib/Driver/Kolab.php
turba/lib/Driver/Ldap.php
turba/lib/Driver/Prefs.php
turba/lib/Driver/Share.php
turba/lib/Driver/Sql.php
turba/lib/Driver/Vbook.php
turba/lib/Form/CreateAddressBook.php
turba/lib/Form/DeleteAddressBook.php
turba/lib/Injector/Binder/Driver.php [new file with mode: 0644]
turba/lib/Injector/Factory/Driver.php [new file with mode: 0644]
turba/lib/List.php
turba/lib/LoginTasks/SystemTask/UpgradeLists.php
turba/lib/Object/Group.php
turba/lib/Turba.php
turba/lib/View/Browse.php
turba/lib/View/List.php
turba/lib/tests/KolabTest.php
turba/merge.php
turba/minisearch.php
turba/scripts/import_squirrelmail_file_abook.php
turba/scripts/import_squirrelmail_sql_abook.php
turba/scripts/upgrades/public_to_horde_share.php
turba/search.php
turba/vcard.php
turba/view.php

index 2688b85..6303d3c 100644 (file)
@@ -33,10 +33,14 @@ if (!$addSources) {
 
 /* A source has been selected, connect and set up the fields. */
 if ($source) {
-    $driver = Turba_Driver::singleton($source);
-    if ($driver instanceof PEAR_Error) {
-        $notification->push(sprintf(_("Failed to access the address book: %s"), $driver->getMessage()), 'horde.error');
-    } else {
+    try {
+        $driver = $injector->getInstance('Turba_Driver')->getDriver($source);
+    } catch (Turba_Exception $e) {
+        $notification->push($e, 'horde.error');
+        $driver = null;
+    }
+
+    if (!is_null($driver)) {
         /* Check permissions. */
         $max_contacts = Turba::getExtendedPermission($driver, 'max_contacts');
         if ($max_contacts !== true &&
index dfa8959..08d3ed1 100644 (file)
@@ -21,9 +21,10 @@ if (!isset($GLOBALS['cfgSources'][$source])) {
 }
 
 /* Set the contact from the key requested. */
-$driver = Turba_Driver::singleton($source);
-if ($driver instanceof PEAR_Error) {
-    $notification->push($driver->getMessage(), 'horde.error');
+try {
+    $driver = $injector->getInstance('Turba_Driver')->getDriver($source);
+} catch (Turba_Exception $e) {
+    $notification->push($e, 'horde.error');
     Horde::applicationUrl($prefs->getValue('initial_page'), true)->redirect();
 }
 
index 166686a..01b7520 100644 (file)
@@ -241,9 +241,10 @@ case 'export':
     $all_fields = array();
     foreach ($sources as $source => $objectkeys) {
         /* Create a Turba storage instance. */
-        $driver = Turba_Driver::singleton($source);
-        if ($driver instanceof PEAR_Error) {
-            $notification->push(sprintf(_("Failed to access the address book: %s"), $driver->getMessage()), 'horde.error');
+        try {
+            $driver = $injector->getInstance('Turba_Driver')->getDriver($source);
+        } catch (Turba_Exception $e) {
+            $notification->push($e, 'horde.error');
             $error = true;
             break;
         }
@@ -337,9 +338,10 @@ case 'export':
 
 case Horde_Data::IMPORT_FILE:
     $dest = Horde_Util::getFormData('dest');
-    $driver = Turba_Driver::singleton($dest);
-    if ($driver instanceof PEAR_Error) {
-        $notification->push(sprintf(_("Failed to access the address book: %s"), $driver->getMessage()), 'horde.error');
+    try {
+        $driver = $injector->getInstance('Turba_Driver')->getDriver($source);
+    } catch (Turba_Exception $e) {
+        $notification->push($e, 'horde.error');
         $error = true;
         break;
     }
@@ -419,13 +421,17 @@ if (is_array($next_step)) {
 
     /* Create a Turba storage instance. */
     $dest = $_SESSION['import_data']['target'];
-    $driver = Turba_Driver::singleton($dest);
-    if ($driver instanceof PEAR_Error) {
-        $notification->push(sprintf(_("Failed to access the address book: %s"), $driver->getMessage()), 'horde.error');
-    } elseif (!count($next_step)) {
+    try {
+        $driver = $injector->getInstance('Turba_Driver')->getDriver($source);
+    } catch (Turba_Exception $e) {
+        $notification->push($e, 'horde.error');
+        $driver = null;
+    }
+
+    if (!count($next_step)) {
         $notification->push(sprintf(_("The %s file didn't contain any contacts."),
                                     $file_types[$_SESSION['import_data']['format']]), 'horde.error');
-    } else {
+    } elseif ($driver) {
         /* Purge old address book if requested. */
         if ($_SESSION['import_data']['purge']) {
             $result = $driver->deleteAll();
index 0ea481e..63c9519 100644 (file)
@@ -18,7 +18,7 @@ Horde_Registry::appInit('turba');
 
 $source = Horde_Util::getFormData('source');
 $key = Horde_Util::getFormData('key');
-$driver = Turba_Driver::singleton($source);
+$driver = $injector->getInstance('Turba_Driver')->getDriver($source);
 
 if ($conf['documents']['type'] != 'none') {
     $object = $driver->getObject($key);
index 775acc1..f49aa2e 100644 (file)
@@ -23,7 +23,7 @@ if ($source === null || !isset($cfgSources[$source])) {
     Horde::applicationUrl($prefs->getValue('initial_page'), true)->redirect();
 }
 
-$driver = Turba_Driver::singleton($source);
+$driver = $injector->getInstance('Turba_Driver')->getDriver($source);
 $contact = $driver->getObject(Horde_Util::getPost('key'));
 if (is_a($contact, 'PEAR_Error')) {
     $notification->push($contact, 'horde.error');
index d4ac050..899aae4 100644 (file)
@@ -45,7 +45,7 @@ if ($source === null || !isset($cfgSources[$source])) {
     $url->redirect();
 }
 
-$driver = Turba_Driver::singleton($source);
+$driver = $injector->getInstance('Turba_Driver')->getDriver($source);
 
 /* Set the contact from the requested key. */
 $contact = $driver->getObject($key);
index 270336c..8ab43e8 100644 (file)
@@ -50,13 +50,13 @@ class Turba_Api extends Horde_Registry_Api
 
         @list($source, $key) = explode('.', $id, 2);
         if (isset($GLOBALS['cfgSources'][$source]) && $key) {
-            $driver = Turba_Driver::singleton($source);
-            if (!($driver instanceof PEAR_Error)) {
+            try {
+                $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
                 $object = $driver->getObject($key);
                 if (!($object instanceof PEAR_Error)) {
                     return $object->getValue('name');
                 }
-            }
+            } catch (Turba_Exception $e) {}
         }
 
         return false;
@@ -322,10 +322,7 @@ class Turba_Api extends Horde_Registry_Api
             }
 
             // Load the Turba driver.
-            $driver = Turba_Driver::singleton($parts[1]);
-            if ($driver instanceof PEAR_Error) {
-                throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage()));
-            }
+            $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($parts[1]);
 
             $contacts = $driver->search(array());
             if ($contacts instanceof PEAR_Error) {
@@ -376,10 +373,7 @@ class Turba_Api extends Horde_Registry_Api
             }
 
             // Load the Turba driver.
-            $driver = Turba_Driver::singleton($parts[1]);
-            if ($driver instanceof PEAR_Error) {
-                throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage()));
-            }
+            $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($parts[1]);
 
             $contact = $driver->getObject($parts[2]);
             if ($contact instanceof PEAR_Error) {
@@ -429,10 +423,7 @@ class Turba_Api extends Horde_Registry_Api
         }
 
         // Load the Turba driver.
-        $driver = Turba_Driver::singleton($parts[1]);
-        if ($driveri instanceof PEAR_Error) {
-            throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage()));
-        }
+        $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($parts[1]);
 
         $ret = $driver->delete($parts[2]);
         if ($ret instanceof PEAR_Error) {
@@ -477,13 +468,9 @@ class Turba_Api extends Horde_Registry_Api
                 throw new Turba_Exception(sprintf(_("Invalid address book: %s"), $source));
             }
 
-            $storage = Turba_Driver::singleton($source);
-            if ($storage instanceof PEAR_Error) {
-                throw new Turba_Exception(sprintf(_("Connection failed: %s"), $storage->getMessage()));
-            }
+            $storage = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
 
             $results = $storage->search(array());
-
             if ($results instanceof PEAR_Error) {
                 throw new Turba_Exception(sprintf(_("Error searching the address book: %s"), $results->getMessage()));
             }
@@ -540,10 +527,7 @@ class Turba_Api extends Horde_Registry_Api
                 throw new Turba_Exception(sprintf(_("Invalid address book: %s"), $source));
             }
 
-            $driver = Turba_Driver::singleton($source);
-            if ($driver instanceof PEAR_Error) {
-                throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage()));
-            }
+            $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
 
             $histories = $history->getByTimestamp(
                 '>', $timestamp, $filter,
@@ -597,10 +581,7 @@ class Turba_Api extends Horde_Registry_Api
                 throw new Turba_Exception(sprintf(_("Invalid address book: %s"), $source));
             }
 
-            $driver = Turba_Driver::singleton($source);
-            if ($driver instanceof PEAR_Error) {
-                throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage()));
-            }
+            $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
 
             $ts = $history->getActionTimestamp('turba:' . $driver->getName()
                 . ':' . $uid,
@@ -646,10 +627,7 @@ class Turba_Api extends Horde_Registry_Api
             throw new Turba_Exception(sprintf(_("Invalid address book: %s"), $import_source));
         }
 
-        $driver = Turba_Driver::singleton($import_source);
-        if ($driver instanceof PEAR_Error) {
-            throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage()));
-        }
+        $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($import_source);
 
         if (!$driver->hasPermission(Horde_Perms::EDIT)) {
             throw new Turba_Exception(_("Permission denied"));
@@ -789,10 +767,7 @@ class Turba_Api extends Horde_Registry_Api
                 throw new Turba_Exception(_("Invalid ID"));
             }
 
-            $driver = Turba_Driver::singleton($source);
-            if ($driver instanceof PEAR_Error) {
-                throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage()));
-            }
+            $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
 
             if (!$driver->hasPermission(Horde_Perms::READ)) {
                 continue;
@@ -859,13 +834,8 @@ class Turba_Api extends Horde_Registry_Api
     public function ownVCard()
     {
         $contact = $this->getOwnContactObject();
-        if ($contact instanceof PEAR_Error) {
-            throw new Turba_Exception($contact);
-        }
-        $driver = Turba_Driver::singleton($contact['source']);
-        if ($driver instanceof PEAR_Error) {
-            throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage()));
-        }
+        $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($contact['source']);
+
         $vcard = $driver->tovCard($contact['contact'], '3.0', null, true);
         $vcard->setAttribute('VERSION', '3.0');
 
@@ -905,10 +875,7 @@ class Turba_Api extends Horde_Registry_Api
             throw new Turba_Exception(_("The address book with your own contact doesn't exist anymore."));
         }
 
-        $driver = Turba_Driver::singleton($source);
-        if ($driver instanceof PEAR_Error) {
-            throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage()));
-        }
+        $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
 
         if (!$driver->hasPermission(Horde_Perms::READ)) {
             throw new Turba_Exception(_("You don't have sufficient permissions to read the address book that contains your own contact."));
@@ -972,10 +939,7 @@ class Turba_Api extends Horde_Registry_Api
                 throw new Turba_Exception(_("Invalid ID"));
             }
 
-            $driver = Turba_Driver::singleton($source);
-            if ($driver instanceof PEAR_Error) {
-                throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage()));
-            }
+            $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
 
             if (!$GLOBALS['registry']->isAdmin() &&
                 !$driver->hasPermission(Horde_Perms::DELETE)) {
@@ -1040,10 +1004,7 @@ class Turba_Api extends Horde_Registry_Api
             }
 
             // Check permissions.
-            $driver = Turba_Driver::singleton($source);
-            if ($driver instanceof PEAR_Error) {
-                throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage()));
-            }
+            $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
             if (!$driver->hasPermission(Horde_Perms::EDIT)) {
                 continue;
             }
@@ -1170,10 +1131,7 @@ class Turba_Api extends Horde_Registry_Api
                     continue;
                 }
 
-            $driver = Turba_Driver::singleton($source);
-            if ($driver instanceof PEAR_Error) {
-                throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage()));
-            }
+            $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
 
             // Determine the name of the column to sort by.
             $columns = isset($sort_columns[$source])
@@ -1326,10 +1284,7 @@ class Turba_Api extends Horde_Registry_Api
         }
 
         if (isset($cfgSources[$source])) {
-            $driver = Turba_Driver::singleton($source);
-            if ($driver instanceof PEAR_Error) {
-                throw new Turba_Exception($driver);
-            }
+            $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
 
             $object = $driver->getObject($objectId);
             if ($object instanceof PEAR_Error) {
@@ -1368,14 +1323,11 @@ class Turba_Api extends Horde_Registry_Api
         }
 
         if (isset($cfgSources[$source])) {
-            $driver = Turba_Driver::singleton($source);
-            if ($driver instanceof PEAR_Error) {
-                throw new Turba_Exception($driver->getMessage());
-            }
+            $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
 
             $objects = $driver->getObjects($objectIds);
             if ($objects instanceof PEAR_Error) {
-                throw new Turba_Exception($objects->getMessage());
+                throw new Turba_Exception($objects);
             }
 
             foreach ($objects as $object) {
@@ -1414,10 +1366,7 @@ class Turba_Api extends Horde_Registry_Api
         $results = array();
         foreach ($sources as $source) {
             if (isset($cfgSources[$source])) {
-                $driver = Turba_Driver::singleton($source);
-                if ($driver instanceof PEAR_Error) {
-                    throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage()));
-                }
+                $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
 
                 $res = $driver->search(array());
                 if (!($res instanceof Turba_List)) {
@@ -1485,10 +1434,7 @@ class Turba_Api extends Horde_Registry_Api
         $objects = array();
         foreach ($time_categories as $category) {
             list($category, $source) = explode('/', $category, 2);
-            $driver = Turba_Driver::singleton($source);
-            if ($driver instanceof PEAR_Error) {
-                throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage()));
-            }
+            $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
             $new_objects = $driver->listTimeObjects($start, $end, $category);
             if ($new_objects instanceof PEAR_Error) {
                 throw new Turba_Exception($new_objects);
@@ -1632,10 +1578,7 @@ class Turba_Api extends Horde_Registry_Api
             throw new Turba_Exception(_("Invalid entry"));
         }
 
-        $driver = Turba_Driver::singleton($source);
-        if ($driver instanceof PEAR_Error) {
-            throw new Turba_Exception(sprintf(_("Connection failed: %s"), $driver->getMessage()));
-        }
+        $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
 
         if (!$driver->hasPermission(Horde_Perms::EDIT)) {
             throw new Turba_Exception(_("Permission denied"));
@@ -1724,10 +1667,7 @@ class Turba_Api extends Horde_Registry_Api
                 continue;
             }
 
-            $driver = Turba_Driver::singleton($source);
-            if ($driver instanceof PEAR_Error) {
-                throw new Turba_Exception($driver);
-            }
+            $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
 
             $list = $driver->search(array('email' => $address), null, 'AND', array(), $strict ? array('email') : array());
             if (!($list instanceof Turba_List)) {
@@ -1783,10 +1723,7 @@ class Turba_Api extends Horde_Registry_Api
 
         foreach ($sources as $source) {
             if (isset($cfgSources[$source])) {
-                $driver = Turba_Driver::singleton($source);
-                if ($driver instanceof PEAR_Error) {
-                    throw new Turba_Exception($driver->getMessage());
-                }
+                $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
                 if (!$driver->hasPermission(Horde_Perms::EDIT)) {
                     continue;
                 }
index e67bd48..57cabf1 100644 (file)
@@ -7,10 +7,12 @@
  *
  * Copyright 2010 The Horde Project (http://www.horde.org/)
  *
- * See the enclosed file COPYING for license information (GPL). If you
- * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ * See the enclosed file COPYING for license information (APL). If you
+ * did not receive this file, see http://www.horde.org/licenses/apl.html.
  *
- * @package Turba
+ * @category Horde
+ * @license  http://www.horde.org/licenses/apl.html APL
+ * @package  Turba
  */
 
 /* Determine the base directories. */
@@ -57,6 +59,15 @@ class Turba_Application extends Horde_Registry_Application
      */
     protected function _init()
     {
+        /* Add Turba-specific binders. */
+        $binders = array(
+            'Turba_Driver' => new Turba_Injector_Binder_Driver()
+        );
+
+        foreach ($binders as $key => $val) {
+            $GLOBALS['injector']->addBinder($key, $val);
+        }
+
         // Turba source and attribute configuration.
         $attributes = Horde::loadConfiguration('attributes.php', 'attributes', 'turba');
         include TURBA_BASE . '/config/sources.php';
@@ -392,15 +403,17 @@ class Turba_Application extends Horde_Registry_Application
         foreach ($cfgSources as $source) {
             if (empty($source['use_shares'])) {
                 // Shares not enabled for this source
-                $driver = Turba_Driver::singleton($source);
-                if (is_a($driver, 'PEAR_Error')) {
-                    Horde::logMessage($driver, 'ERR');
+                try {
+                    $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
+                } catch (Turba_Exception $e) {
+                    Horde::logMessage($e, 'ERR');
                     $hasError = true;
-                } else {
-                    $result = $driver->removeUserData($user);
-                    if (is_a($result, 'PEAR_Error')) {
-                        Horde::logMessage($result, 'ERR');
-                    }
+                    continue;
+                }
+
+                $result = $driver->removeUserData($user);
+                if ($result instanceof PEAR_Error) {
+                    Horde::logMessage($result, 'ERR');
                 }
             }
         }
@@ -416,9 +429,14 @@ class Turba_Application extends Horde_Registry_Application
                 /* Only attempt to delete the user's default share */
                 if (!empty($params['default'])) {
                     $config = Turba::getSourceFromShare($share);
-                    $driver = Turba_Driver::singleton($config);
+                    try {
+                        $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($config);
+                    } catch (Turba_Exception $e) {
+                        continue;
+                    }
+
                     $result = $driver->removeUserData($user);
-                    if (is_a($result, 'PEAR_Error')) {
+                    if ($result instanceof PEAR_Error) {
                         Horde::logMessage($result, 'ERR');
                         $hasError = true;
                     }
index 1d6b818..29faf27 100644 (file)
@@ -2593,28 +2593,24 @@ class Turba_Driver implements Countable
     }
 
     /**
-     * Static method to construct Turba_Driver objects. Use this so that we
-     * can return PEAR_Error objects if anything goes wrong.
-     *
-     * Should only be called by Turba_Driver::singleton().
-     *
-     * @see Turba_Driver::singleton()
-     * @access private
+     * Static method to construct Turba_Driver objects.
      *
      * @param string $name   String containing the internal name of this
      *                       source.
      * @param array $config  Array containing the configuration information for
      *                       this source.
+     *
+     * @return Turba_Driver  The concrete driver object.
+     * @throws Turba_Exception
      */
-    function &factory($name, $config)
+    static public function factory($name, $config)
     {
-        $class = 'Turba_Driver_' . ucfirst(basename($config['type']));
+        $class = __CLASS__ . '_' . ucfirst(basename($config['type']));
 
         if (class_exists($class)) {
             $driver = new $class($config['params']);
         } else {
-            $driver = PEAR::raiseError(sprintf(_("Unable to load the definition of %s."), $class));
-            return $driver;
+            throw new Turba_Exception(sprintf(_("Unable to load the definition of %s."), $class));
         }
 
         /* Store name and title. */
@@ -2622,11 +2618,7 @@ class Turba_Driver implements Countable
         $driver->title = $config['title'];
 
         /* Initialize */
-        $result = $driver->_init();
-        if (is_a($result, 'PEAR_Error')) {
-            $driver = PEAR::raiseError($result->getMessage());
-            return $driver;
-        }
+        $driver->_init();
 
         /* Store and translate the map at the Source level. */
         $driver->map = $config['map'];
@@ -2659,55 +2651,12 @@ class Turba_Driver implements Countable
     }
 
     /**
-     * Attempts to return a reference to a concrete Turba_Driver instance
-     * based on the $config array. It will only create a new instance if no
-     * Turba_Driver instance with the same parameters currently exists.
-     *
-     * This method must be invoked as:
-     *   $driver = &Turba_Driver::singleton()
-     *
-     * @param mixed $name  Either a string containing the internal name of this
-     *                     source, or a config array describing the source.
-     *
-     * @return Turba_Driver  The concrete Turba_Driver reference, or a
-     *                       PEAR_Error on error.
-     */
-    function &singleton($name)
-    {
-        static $instances = array();
-
-        if (is_array($name)) {
-            $key = md5(serialize($name));
-            $srcName = '';
-            $srcConfig = $name;
-        } else {
-            $key = $name;
-            $srcName = $name;
-            if (!empty($GLOBALS['cfgSources'][$name])) {
-                $srcConfig = $GLOBALS['cfgSources'][$name];
-            } else {
-                $error = PEAR::raiseError('Source not found');
-                return $error;
-            }
-        }
-
-        if (!isset($instances[$key])) {
-            if (!is_array($name) && !isset($GLOBALS['cfgSources'][$name])) {
-                $error = PEAR::raiseError(sprintf(_("The address book \"%s\" does not exist."), $name));
-                return $error;
-            }
-            $instances[$key] = Turba_Driver::factory($srcName, $srcConfig);
-        }
-
-        return $instances[$key];
-    }
-
-    /**
      * Initialize the driver.
+     *
+     * @throws Turba_Exception
      */
-    function _init()
+    protected function _init()
     {
-        return true;
     }
 
     /**
index e5ac8b3..bed0d74 100644 (file)
@@ -18,13 +18,6 @@ class Turba_Driver_Facebook extends Turba_Driver
     private $_facebook;
 
     /**
-     */
-    function _init()
-    {
-        return true;
-    }
-
-    /**
      * Checks if the current user has the requested permissions on this
      * source.
      *
index 149aac6..18bb85f 100644 (file)
@@ -9,13 +9,6 @@
 class Turba_Driver_Favourites extends Turba_Driver
 {
     /**
-     */
-    function _init()
-    {
-        return true;
-    }
-
-    /**
      * Checks if the current user has the requested permissions on this
      * source.
      *
index 2251b52..a1dedf8 100644 (file)
@@ -21,14 +21,6 @@ class Turba_Driver_Group extends Turba_Driver
     }
 
     /**
-     * Initialize the group driver.
-     */
-    function _init()
-    {
-        return true;
-    }
-
-    /**
      * Checks if the current user has the requested permissions on this
      * source.  This source is always read only.
      *
index 9f27cdc..7cbd4c8 100644 (file)
@@ -70,30 +70,31 @@ class Turba_Driver_Imsp extends Turba_Driver
 
     /**
      * Initialize the IMSP connection and check for error.
+     *
+     * @throws Turba_Exception
      */
-    function _init()
+    protected function _init()
     {
         global $conf;
 
         $this->_bookName = $this->getContactOwner();
         $this->_imsp = Net_IMSP::singleton('Book', $this->params);
         $result = $this->_imsp->init();
-        if (is_a($result, 'PEAR_Error')) {
+        if ($result instanceof PEAR_Error) {
             $this->_authenticated = false;
-            return $result;
+            throw new Turba_Exception($result);
         }
 
         if (!empty($conf['log'])) {
             $logParams = $conf['log'];
             $result = $this->_imsp->setLogger($conf['log']);
-            if (is_a($result, 'PEAR_Error')) {
-                return $result;
+            if ($result instanceof PEAR_Error) {
+                throw new Turba_Exception($result);
             }
         }
 
         Horde::logMessage('IMSP Driver initialized for ' . $this->_bookName, 'DEBUG');
         $this->_authenticated = true;
-        return true;
     }
 
     /**
index a4af43a..19e1ca1 100644 (file)
@@ -38,19 +38,15 @@ class Turba_Driver_Kolab extends Turba_Driver
 
     /**
      * Attempts to open a Kolab Groupware folder.
-     *
-     * @return boolean  True on success, PEAR_Error on failure.
      */
-    function _init()
+    protected function _init()
     {
         $this->_kolab = new Kolab();
-        if (empty($this->_kolab->version)) {
-            $wrapper = "Turba_Driver_kolab_wrapper_old";
-        } else {
-            $wrapper = "Turba_Driver_kolab_wrapper_new";
-        }
+        $wrapper = empty($this->_kolab->version)
+            ? 'Turba_Driver_kolab_wrapper_old'
+            : 'Turba_Driver_kolab_wrapper_new';
 
-        $this->_wrapper = &new $wrapper($this->name, $this->_kolab);
+        $this->_wrapper = new $wrapper($this->name, $this->_kolab);
     }
 
     /**
index 02c790c..0e20249 100644 (file)
@@ -56,14 +56,17 @@ class Turba_Driver_Ldap extends Turba_Driver
         parent::__construct($params);
     }
 
+    /**
+     * @throws Turba_Exception
+     */
     function _init()
     {
         if (!Horde_Util::extensionExists('ldap')) {
-            return PEAR::raiseError(_("LDAP support is required but the LDAP module is not available or not loaded."));
+            throw new Turba_Exception(_("LDAP support is required but the LDAP module is not available or not loaded."));
         }
 
         if (!($this->_ds = @ldap_connect($this->_params['server'], $this->_params['port']))) {
-            return PEAR::raiseError(_("Connection failure"));
+            throw new Turba_Exception(_("Connection failure"));
         }
 
         /* Set the LDAP protocol version. */
@@ -82,23 +85,22 @@ class Turba_Driver_Ldap extends Turba_Driver
         }
 
         /* Start TLS if we're using it. */
-        if (!empty($this->_params['tls'])) {
-            if (!@ldap_start_tls($this->_ds)) {
-                return PEAR::raiseError(sprintf(_("STARTTLS failed: (%s) %s"), ldap_errno($this->_ds), ldap_error($this->_ds)));
-            }
+        if (!empty($this->_params['tls']) &&
+            !@ldap_start_tls($this->_ds)) {
+            throw new Turba_Exception(sprintf(_("STARTTLS failed: (%s) %s"), ldap_errno($this->_ds), ldap_error($this->_ds)));
         }
 
         /* Bind to the server. */
         if (isset($this->_params['bind_dn']) &&
             isset($this->_params['bind_password'])) {
-            if (!@ldap_bind($this->_ds, $this->_params['bind_dn'], $this->_params['bind_password'])) {
-                return PEAR::raiseError(sprintf(_("Bind failed: (%s) %s"), ldap_errno($this->_ds), ldap_error($this->_ds)));
-            }
-        } elseif (!(@ldap_bind($this->_ds))) {
-            return PEAR::raiseError(sprintf(_("Bind failed: (%s) %s"), ldap_errno($this->_ds), ldap_error($this->_ds)));
+            $error = !@ldap_bind($this->_ds, $this->_params['bind_dn'], $this->_params['bind_password']);
+        } else {
+            $error = !(@ldap_bind($this->_ds));
         }
 
-        return true;
+        if ($error) {
+            throw new Turba_Exception(sprintf(_("Bind failed: (%s) %s"), ldap_errno($this->_ds), ldap_error($this->_ds)));
+        }
     }
 
     /**
index 7b9f1d0..1416134 100644 (file)
@@ -9,13 +9,6 @@
 class Turba_Driver_Prefs extends Turba_Driver
 {
     /**
-     */
-    function _init()
-    {
-        return true;
-    }
-
-    /**
      * Returns all entries - searching isn't implemented here for now. The
      * parameters are simply ignored.
      *
index bda4b12..0278e69 100644 (file)
@@ -76,15 +76,12 @@ class Turba_Driver_Share extends Turba_Driver
     }
 
     /**
-     * Initialize
+     * @throws Turba_Exception
      */
-    function _init()
+    protected function _init()
     {
         $this->_share = &$this->_params['config']['params']['share'];
-        $this->_driver = &Turba_Driver::factory($this->name, $this->_params['config']);
-        if (is_a($this->_driver, 'PEAR_Error')) {
-            return $this->_driver;
-        }
+        $this->_driver = Turba_Driver::factory($this->name, $this->_params['config']);
         $this->_driver->_contact_owner = $this->_getContactOwner();
     }
 
index 113ec13..6d26691 100644 (file)
@@ -33,56 +33,17 @@ class Turba_Driver_Sql extends Turba_Driver
      */
     var $_write_db;
 
-    function _init()
+    /**
+     * @throws Turba_Exception
+     */
+    protected function _init()
     {
-        $this->_write_db = &DB::connect($this->_params,
-                                        array('persistent' => !empty($this->_params['persistent']),
-                                              'ssl' => !empty($this->_params['ssl'])));
-        if (is_a($this->_write_db, 'PEAR_Error')) {
-            return $this->_write_db;
-        }
-
-        // Set DB portability options.
-        switch ($this->_write_db->phptype) {
-        case 'mssql':
-            $this->_write_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS | DB_PORTABILITY_RTRIM);
-            break;
-        default:
-            $this->_write_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS);
-        }
-
-        if ($this->_params['phptype'] == 'oci8') {
-            $this->_write_db->query('ALTER SESSION SET NLS_DATE_FORMAT = \'YYYY-MM-DD\'');
-        }
-
-        /* Check if we need to set up the read DB connection
-         * seperately. */
-        if (!empty($this->_params['splitread'])) {
-            $params = array_merge($this->_params, $this->_params['read']);
-            $this->_db = &DB::connect($params,
-                                      array('persistent' => !empty($params['persistent']),
-                                            'ssl' => !empty($params['ssl'])));
-            if (is_a($this->_db, 'PEAR_Error')) {
-                return $this->_db;
-            }
-
-            // Set DB portability options.
-            switch ($this->_db->phptype) {
-            case 'mssql':
-                $this->_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS | DB_PORTABILITY_RTRIM);
-                break;
-            default:
-                $this->_db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS);
-            }
-            if ($params['phptype'] == 'oci8') {
-                $this->_db->query('ALTER SESSION SET NLS_DATE_FORMAT = \'YYYY-MM-DD\'');
-            }
-        } else {
-            /* Default to the same DB handle for reads. */
-            $this->_db =& $this->_write_db;
+        try {
+            $this->_db = $GLOBALS['injector']->getInstance('Horde_Db_Pear')->getDb('read', 'turba', $this->_params);
+            $this->_write_db = $GLOBALS['injector']->getInstance('Horde_Db_Pear')->getDb('rw', 'turba', $this->_params);
+        } catch (Horde_Exception $e) {
+            throw new Turba_Exception($e);
         }
-
-        return true;
     }
 
     /**
index c8c83b2..d51dd59 100644 (file)
@@ -50,26 +50,22 @@ class Turba_Driver_Vbook extends Turba_Driver
     }
 
     /**
+     * @throws Turba_Exception
      */
-    function _init()
+    protected function _init()
     {
         /* Grab a reference to the share for this vbook. */
         $this->_share = $this->_params['share'];
 
         /* Load the underlying driver. */
-        $this->_driver = &Turba_Driver::singleton($this->_params['source']);
-        if (is_a($this->_driver, 'PEAR_Error')) {
-            return $this->_driver;
-        }
+        $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($this->_params['source']);
 
-        if (!empty($this->_params['criteria'])) {
-            $this->searchCriteria = $this->_params['criteria'];
-        } else {
-            $this->searchCriteria = array();
-        }
-        $this->searchType = count($this->searchCriteria) > 1 ? 'advanced' : 'basic';
-
-        return true;
+        $this->searchCriteria = empty($this->_params['criteria'])
+            ? array()
+            : $this->_params['criteria'];
+        $this->searchType = (count($this->searchCriteria) > 1)
+            ? 'advanced'
+            : 'basic';
     }
 
     /**
index fe682b2..3ffc497 100644 (file)
@@ -27,15 +27,15 @@ class Turba_Form_CreateAddressBook extends Horde_Form
         $this->setButtons(array(_("Create")));
     }
 
+    /**
+     * @throws Turba_Exception
+     */
     function execute()
     {
         // Need a clean cfgSources array
         include TURBA_BASE . '/config/sources.php';
 
-        $driver = Turba_Driver::singleton($cfgSources[$GLOBALS['conf']['shares']['source']]);
-        if ($driver instanceof PEAR_Error) {
-            return $driver;
-        }
+        $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($cfgSources[$GLOBALS['conf']['shares']['source']]);
 
         $params = array(
             'params' => array('source' => $GLOBALS['conf']['shares']['source']),
index e01c36e..3075da2 100644 (file)
@@ -35,6 +35,8 @@ class Turba_Form_DeleteAddressBook extends Horde_Form
 
     /**
      * @TODO Remove share from 'addressbooks' pref
+     *
+     * @throws Turba_Exception
      */
     function execute()
     {
@@ -45,18 +47,15 @@ class Turba_Form_DeleteAddressBook extends Horde_Form
 
         if (!$GLOBALS['registry']->getAuth() ||
             $this->_addressbook->get('owner') != $GLOBALS['registry']->getAuth()) {
-            return PEAR::raiseError(_("You do not have permissions to delete this address book."));
+            throw new Turba_Exception(_("You do not have permissions to delete this address book."));
         }
 
-        $driver = &Turba_Driver::singleton($this->_addressbook->getName());
-        if (is_a($driver, 'PEAR_Error')) {
-            return $driver;
-        }
+        $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($this->_addressbook->getName());
 
         // We have a Turba_Driver, try to delete the address book.
         $result = $driver->deleteAll();
         if (is_a($result, 'PEAR_Error')) {
-            return $result;
+            throw new Turba_Exception($result);
         }
 
         // Address book successfully deleted from backend, remove the
diff --git a/turba/lib/Injector/Binder/Driver.php b/turba/lib/Injector/Binder/Driver.php
new file mode 100644 (file)
index 0000000..51e1f3f
--- /dev/null
@@ -0,0 +1,31 @@
+<?php
+/**
+ * Binder for Turba_Driver::.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (ASL). If you
+ * did not receive this file, see http://www.horde.org/licenses/asl.html.
+ *
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @license  http://www.horde.org/licenses/asl.html ASL
+ * @package  Turba
+ */
+class Turba_Injector_Binder_Driver implements Horde_Injector_Binder
+{
+    /**
+     */
+    public function create(Horde_Injector $injector)
+    {
+        return new Turba_Injector_Factory_Driver($injector);
+    }
+
+    /**
+     */
+    public function equals(Horde_Injector_Binder $binder)
+    {
+        return false;
+    }
+
+}
diff --git a/turba/lib/Injector/Factory/Driver.php b/turba/lib/Injector/Factory/Driver.php
new file mode 100644 (file)
index 0000000..43dcf66
--- /dev/null
@@ -0,0 +1,85 @@
+<?php
+/**
+ * A Horde_Injector:: based Turba_Driver:: factory.
+ *
+ * PHP version 5
+ *
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @license  http://www.horde.org/licenses/apl.html APL
+ * @link     http://pear.horde.org/index.php?package=Turba
+ * @package  Turba
+ */
+
+/**
+ * A Horde_Injector:: based Turba_Driver:: factory.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (APL). If you
+ * did not receive this file, see http://www.horde.org/licenses/apl.html.
+ *
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @license  http://www.horde.org/licenses/apl.html APL
+ * @link     http://pear.horde.org/index.php?package=Turba
+ * @package  Turba
+ */
+class Turba_Injector_Factory_Driver
+{
+    /**
+     * Instances.
+     *
+     * @var array
+     */
+    private $_instances = array();
+
+    /**
+     * The injector.
+     *
+     * @var Horde_Injector
+     */
+    private $_injector;
+
+    /**
+     * Constructor.
+     *
+     * @param Horde_Injector $injector  The injector to use.
+     */
+    public function __construct(Horde_Injector $injector)
+    {
+        $this->_injector = $injector;
+    }
+
+    /**
+     * Return the Turba_Driver:: instance.
+     *
+     * @param mixed $name  Either a string containing the internal name of this
+     *                     source, or a config array describing the source.
+     *
+     * @return Turba_Driver  The singleton instance.
+     * @throws Turba_Exception
+     */
+    public function getDriver($name)
+    {
+        if (is_array($name)) {
+            $key = md5(serialize($name));
+            $srcName = '';
+            $srcConfig = $name;
+        } else {
+            $key = $name;
+            $srcName = $name;
+            if (empty($GLOBALS['cfgSources'][$name])) {
+                throw new Turba_Exception(sprintf(_("The address book \"%s\" does not exist."), $name));
+            }
+            $srcConfig = $GLOBALS['cfgSources'][$name];
+        }
+
+        if (!isset($this->_instances[$key])) {
+            $this->_instances[$key] = Turba_Driver::factory($srcName, $srcConfig);
+        }
+
+        return $this->_instances[$key];
+    }
+
+}
index 31be0ff..4b616e7 100644 (file)
@@ -37,10 +37,10 @@ class Turba_List implements Countable
     {
         foreach ($ids as $value) {
             list($source, $key) = explode(':', $value);
-            $driver = Turba_Driver::singleton($source);
-            if ($driver instanceof Turba_Driver) {
+            try {
+                $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
                 $this->insert($driver->getObject($key));
-            }
+            } catch (Turba_Exception $e) {}
         }
     }
 
index 45bad2f..6a29868 100644 (file)
@@ -31,7 +31,7 @@ class Turba_LoginTasks_SystemTask_UpgradeLists extends Horde_LoginTasks_SystemTa
     /**
      * Perform all functions for this task.
      *
-     * @return mixed True | PEAR_Error
+     * @return boolean  Success.
      */
     public function execute()
     {
@@ -39,10 +39,15 @@ class Turba_LoginTasks_SystemTask_UpgradeLists extends Horde_LoginTasks_SystemTa
             $criteria = array('__type' => 'Group');
             $sources = array_keys($GLOBALS['cfgSources']);
             foreach ($sources as $sourcekey) {
-                $driver = Turba_Driver::singleton($sourcekey);
+                try {
+                    $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($sourcekey);
+                } catch (Turba_Exception $e) {
+                    return false;
+                }
+
                 $lists = $driver->search($criteria);
-                if (is_a($lists, 'PEAR_Error')) {
-                    return $lists;
+                if ($lists instanceof PEAR_Error) {
+                    return false;
                 }
 
                 for ($j = 0, $cnt = count($lists); $j < $cnt; ++$j) {
index 4e9b914..3594253 100644 (file)
@@ -48,6 +48,8 @@ class Turba_Object_Group extends Turba_Object {
      *
      * @param string $contactId  The id of the contact to add.
      * @param string $sourceId   The source $contactId is from.
+     *
+     * @throws Turba_Exception
      */
     function addMember($contactId, $sourceId = null)
     {
@@ -58,24 +60,21 @@ class Turba_Object_Group extends Turba_Object {
 
         // Can't add a group to itself.
         if ($contactId == $this->attributes['__key']) {
-            return PEAR::raiseError(_("Can't add a group to itself."));
+            throw new Turba_Exception(_("Can't add a group to itself."));
         }
 
         // Try to find the contact being added.
         if ($sourceId == $this->getSource()) {
             $contact = $this->driver->getObject($contactId);
         } else {
-            $driver = Turba_Driver::singleton($sourceId);
-            if (is_a($driver, 'PEAR_Error')) {
-                return $driver;
-            }
+            $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($sourceId);
             $contact = $driver->getObject($contactId);
         }
 
         // Bail out if the contact being added doesn't exist or can't
         // be retrieved.
         if (is_a($contact, 'PEAR_Error')) {
-            return $contact;
+            throw new Turba_Exception($contact);
         }
 
         // Explode members.
@@ -174,16 +173,18 @@ class Turba_Object_Group extends Turba_Object {
                     list($owner, $contactId) = explode(':', $contactId, 2);
                     $sourceId .= ':' . $owner;
                 }
-                $driver = Turba_Driver::singleton($sourceId);
-                if (!is_a($driver, 'PEAR_Error')) {
-                    $contact = $driver->getObject($contactId);
-                    if (is_a($contact, 'PEAR_Error')) {
-                        // Remove the contact if it no longer exists
-                        $this->removeMember($member);
-                        $modified = true;
-                        continue;
-                    }
-                } else {
+
+                try {
+                    $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($sourceId);
+                } catch (Turba_Exception $e) {
+                    continue;
+                }
+
+                $contact = $driver->getObject($contactId);
+                if (is_a($contact, 'PEAR_Error')) {
+                    // Remove the contact if it no longer exists
+                    $this->removeMember($member);
+                    $modified = true;
                     continue;
                 }
             }
index 63faa61..e233229 100644 (file)
@@ -342,9 +342,10 @@ class Turba {
         $out = array();
 
         foreach ($in as $sourceId => $source) {
-            $driver = Turba_Driver::singleton($sourceId);
-            if (is_a($driver, 'PEAR_Error')) {
-                Horde::logMessage(sprintf("Could not instantiate the %s source: %s", $sourceId, $driver->getMessage()), 'ERR');
+            try {
+                $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($sourceId);
+            } catch (Turba_Exception $e) {
+                Horde::logMessage($e, 'ERR');
                 continue;
             }
 
@@ -406,14 +407,16 @@ class Turba {
 
                 // Default share?
                 if (empty($defaults[$params['source']])) {
-                    $driver = Turba_Driver::singleton($params['source']);
-                    if (!is_a($driver, 'PEAR_Error') && $driver->hasPermission(Horde_Perms::EDIT)) {
-                        $defaults[$params['source']] =
-                            $driver->checkDefaultShare(
+                    try {
+                        $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($params['source']);
+                        if ($driver->hasPermission(Horde_Perms::EDIT)) {
+                            $defaults[$params['source']] = $driver->checkDefaultShare(
                                 $shares[$name],
-                                $sources[$params['source']]);
-                    } else {
-                        $notification->push($driver, 'horde.error');
+                                $sources[$params['source']]
+                            );
+                        }
+                    } catch (Turba_Exception $e) {
+                        $notification->push($e, 'horde.error');
                     }
                 }
 
@@ -440,26 +443,33 @@ class Turba {
             }
             if ($GLOBALS['registry']->getAuth() && empty($defaults[$source])) {
                 // User's default share is missing.
-                $driver = Turba_Driver::singleton($source);
-                if (!($driver instanceof PEAR_Error)) {
-                    $sourceKey = md5(mt_rand());
-                    try {
-                        $share = $driver->createShare(
-                            $sourceKey,
-                            array('params' => array('source' => $source,
-                                                    'default' => true,
-                                                    'name' => $GLOBALS['registry']->getAuth())));
-                    } catch (Horde_Share_Exception $e) {
-                        Horde::logMessage($e, 'ERR');
-                        continue;
-                    }
-
-                    $source_config = $sources[$source];
-                    $source_config['params']['share'] = $share;
-                    $newSources[$sourceKey] = $source_config;
-                } else {
+                try {
+                    $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($sourceId);
+                } catch (Turba_Exception $e) {
                     $notification->push($driver, 'horde.error');
+                    continue;
                 }
+
+                $sourceKey = md5(mt_rand());
+                try {
+                    $share = $driver->createShare(
+                        $sourceKey,
+                        array(
+                            'params' => array(
+                                'source' => $source,
+                                'default' => true,
+                                'name' => $GLOBALS['registry']->getAuth()
+                            )
+                        )
+                    );
+                } catch (Horde_Share_Exception $e) {
+                    Horde::logMessage($e, 'ERR');
+                    continue;
+                }
+
+                $source_config = $sources[$source];
+                $source_config['params']['share'] = $share;
+                $newSources[$sourceKey] = $source_config;
             }
         }
 
index 9fda375..4e43386 100644 (file)
@@ -68,9 +68,10 @@ class Turba_View_Browse {
         if (!$browse_source_count && $vars->get('key') != '**search') {
             $notification->push(_("There are no browseable address books."), 'horde.warning');
         } else {
-            $driver = Turba_Driver::singleton($source);
-            if (is_a($driver, 'PEAR_Error')) {
-                $notification->push(sprintf(_("Failed to access the address book: %s"), $driver->getMessage()), 'horde.error');
+            try {
+                $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($source);
+            } catch (Turba_Exception $e) {
+                $notification->push($e, 'horde.error');
                 unset($driver);
             }
         }
@@ -136,9 +137,11 @@ class Turba_View_Browse {
 
                 // If we have data, try loading the target address book driver.
                 $targetSource = $vars->get('targetAddressbook');
-                $targetDriver = Turba_Driver::singleton($targetSource);
-                if (is_a($targetDriver, 'PEAR_Error')) {
-                    $notification->push(sprintf(_("Failed to access the address book: %s"), $targetDriver->getMessage()), 'horde.error');
+
+                try {
+                    $targetDriver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($targetSource);
+                } catch (Turba_Exception $e) {
+                    $notification->push($e, 'horde.error');
                     break;
                 }
 
@@ -165,9 +168,10 @@ class Turba_View_Browse {
                     }
 
                     // Try and load the driver for the source.
-                    $sourceDriver = Turba_Driver::singleton($objectSource);
-                    if (is_a($sourceDriver, 'PEAR_Error')) {
-                        $notification->push(sprintf(_("Failed to access the address book: %s"), $sourceDriver->getMessage()), 'horde.error');
+                    try {
+                        $sourceDriver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($objectSource);
+                    } catch (Turba_Exception $e) {
+                        $notification->push($e, 'horde.error');
                         continue;
                     }
 
@@ -243,9 +247,11 @@ class Turba_View_Browse {
                     if (!isset($cfgSources[$targetSource])) {
                         break;
                     }
-                    $targetDriver = Turba_Driver::singleton($targetSource);
-                    if ($targetDriver instanceof PEAR_Error) {
-                        $notification->push(sprintf_("Could not load driver for %s", $targetSource), 'horde.error');
+
+                    try {
+                        $targetDriver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($targetSource);
+                    } catch (Turba_Exception $e) {
+                        $notification->push($e, 'horde.error');
                         break;
                     }
                     $target = &$targetDriver->getObject($targetKey);
@@ -255,9 +261,10 @@ class Turba_View_Browse {
                     }
                 } else {
                     $targetSource = $vars->get('targetAddressbook');
-                    $targetDriver = Turba_Driver::singleton($targetSource);
-                    if ($targetDriver instanceof PEAR_Error) {
-                        $notification->push(sprintf_("Could not load driver for %s", $targetSource), 'horde.error');
+                    try {
+                        $targetDriver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($targetSource);
+                    } catch (Turba_Exception $e) {
+                        $notification->push($e, 'horde.error');
                         break;
                     }
                 }
index 7807fc2..5611d0f 100644 (file)
@@ -145,22 +145,12 @@ class Turba_View_List implements Countable
     {
         global $prefs, $default_source, $copymove_source_options;
 
-        $driver = Turba_Driver::singleton($default_source);
-        $hasDelete = false;
-        $hasEdit = false;
-        $hasExport = false;
-        if (!is_a($driver, 'PEAR_Error')) {
-            if ($driver->hasPermission(Horde_Perms::DELETE)) {
-                $hasDelete = true;
-            }
-            if ($driver->hasPermission(Horde_Perms::EDIT)) {
-                $hasEdit = true;
-            }
-            if ($GLOBALS['conf']['menu']['import_export']
-                && !empty($GLOBALS['cfgSources'][$default_source]['export'])) {
-                $hasExport = true;
-            }
-        }
+        $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($default_source);
+
+        $hasDelete = $driver->hasPermission(Horde_Perms::DELETE);
+        $hasEdit = $driver->hasPermission(Horde_Perms::EDIT);
+        $hasExport = ($GLOBALS['conf']['menu']['import_export'] && !empty($GLOBALS['cfgSources'][$default_source]['export']));
+
         list($addToList, $addToListSources) = $this->getAddSources();
 
         $viewurl = Horde::applicationUrl('browse.php')->add(array(
@@ -404,7 +394,7 @@ class Turba_View_List implements Countable
                                             'name' => '&nbsp;&nbsp;' . htmlspecialchars($srcConfig['title']),
                                             'source' => htmlspecialchars($src));
 
-                $srcDriver = &Turba_Driver::singleton($src);
+                $srcDriver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($src);
                 $listList = $srcDriver->search(array('__type' => 'Group'),
                                                array(array('field' => 'name',
                                                            'ascending' => true)),
index a1fb2f5..0635f48 100644 (file)
@@ -47,14 +47,14 @@ class Turba_KolabTest extends Turba_KolabTestBase {
         $this->_kolab->_storage->save($object);
 
         // Check that the driver can be created
-        $turba = Turba_Driver::singleton('wrobel@example.org');
-        $this->assertNoError($turba);
+        $turba = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver('wrobel@example.org');
+        //$this->assertNoError($turba);
 
         $result = $turba->search(array(), array('last-name'));
         $this->assertNoError($result);
         $this->assertEquals(2, count($result));
 
-        $turba = Turba_Driver::singleton('INBOX%2Ftest2');
+        $turba = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver('INBOX%2Ftest2');
         $result = $turba->search(array(), array('last-name'));
 
         $this->assertEquals(0, count($result));
@@ -75,8 +75,8 @@ class Turba_KolabTest extends Turba_KolabTestBase {
         );
 
         // Save the contact
-        $turba = Turba_Driver::singleton('wrobel@example.org');
-        $this->assertNoError($turba);
+        $turba = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver('wrobel@example.org');
+        //$this->assertNoError($turba);
 
         $this->assertNoError($turba->_add($object));
 
@@ -102,8 +102,8 @@ class Turba_KolabTest extends Turba_KolabTestBase {
         );
 
         // Save the contact
-        $turba = Turba_Driver::singleton('wrobel@example.org');
-        $this->assertNoError($turba);
+        $turba = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver('wrobel@example.org');
+        //$this->assertNoError($turba);
 
         $this->assertNoError($turba->_add($object));
 
index 10a2852..ece6344 100644 (file)
@@ -16,7 +16,7 @@ Horde_Registry::appInit('turba');
 $source = Horde_Util::getFormData('source');
 $key = Horde_Util::getFormData('key');
 $mergeInto = Horde_Util::getFormData('merge_into');
-$driver = Turba_Driver::singleton($source);
+$driver = $injector->getInstance('Turba_Driver')->getDriver($source);
 
 if ($url = Horde_Util::getFormData('url')) {
     $url = new Horde_Url($url, true)->unique();
index 542a482..2e43101 100644 (file)
@@ -21,11 +21,16 @@ $source = Horde_Util::getFormData('source', Turba::getDefaultAddressBook());
 
 // Do the search if we have one.
 if (!is_null($search)) {
-    $driver = Turba_Driver::singleton($source);
-    if (!is_a($driver, 'PEAR_Error')) {
+    try {
+        $driver = $injector->getInstance('Turba_Driver')->getDriver($source);
+    } catch (Turba_Exception $e) {
+        $driver = null;
+    }
+
+    if ($driver) {
         $criteria['name'] = trim($search);
         $res = $driver->search($criteria);
-        if (is_a($res, 'Turba_List')) {
+        if ($res instanceof Turba_List) {
             while ($ob = $res->next()) {
                 if ($ob->isGroup()) {
                     continue;
index 789e398..f8dc023 100755 (executable)
@@ -84,9 +84,10 @@ foreach($files as $file) {
     }
 
     // Initiate driver
-    $driver = &Turba_Driver::singleton($import_source);
-    if (is_a($driver, 'PEAR_Error')) {
-        PEAR::raiseError(sprintf(_("Connection failed: %s"), $driver->getMessage()), 'horde.error', null, null, $import_source);
+    try {
+        $driver = $GLOBALS['injector']->getInstance('Turba_Driver')->getDriver($import_source);
+    } catch (Turba_Exception $e) {
+        PEAR::raiseError(sprintf(_("Connection failed: %s"), $e->getMessage()), 'horde.error', null, null, $import_source);
         continue;
     }
 
index dd36683..b7882fa 100755 (executable)
@@ -93,9 +93,10 @@ while ($row = $handle->fetchRow(DB_FETCHMODE_ASSOC)) {
         }
 
         // Initiate driver
-        $driver = &Turba_Driver::singleton($import_source);
-        if (is_a($driver, 'PEAR_Error')) {
-            $cli->message('  ' . sprintf(_("Connection failed: %s"), $driver->getMessage()), 'cli.error');
+        try {
+            $driver = $injector->getInstance('Turba_Driver')->getDriver($import_source);
+        } catch (Turba_Exception $e) {
+            $cli->message('  ' . sprintf(_("Connection failed: %s"), $e->getMessage()), 'cli.error');
             continue;
         }
     }
index 7771b3f..8ab075f 100755 (executable)
@@ -76,12 +76,9 @@ $share->save();
 $CLI->message('Created new Horde_Share object for the shared address book.', 'cli.success');
 
 // Share created, now get a Turba_Driver and make the changes.
-$driver = &Turba_Driver::singleton($sourceKey);
-if (is_a($driver, 'PEAR_Error')) {
-    var_dump($driver);
-    exit;
-}
-$db = & $driver->_db;
+$driver = $injector->getInstance('Turba_Driver')->getDriver($sourceKey);
+
+$db = &$driver->_db;
 if (is_a($db, 'PEAR_Error')) {
     var_dump($db);
 }
index 4e3608e..cb84fd2 100644 (file)
@@ -106,11 +106,16 @@ if (!isset($addressBooks[$source])) {
 $criteria = Horde_Util::getFormData('criteria');
 $val = Horde_Util::getFormData('val');
 $action = Horde_Util::getFormData('actionID');
-$driver = Turba_Driver::singleton($source);
-if (is_a($driver, 'PEAR_Error')) {
-    $notification->push(sprintf(_("Failed to access the address book: %s"), $driver->getMessage()), 'horde.error');
+
+try {
+    $driver = $injector->getInstance('Turba_Driver')->getDriver($source);
+} catch (Turba_Exception $e) {
+    $notification->push($e, 'horde.error');
+    $driver = null;
     $map = array();
-} else {
+}
+
+if ($driver) {
     $map = $driver->getCriteria();
     if ($_SESSION['turba']['search_mode'] == 'advanced') {
         $criteria = array();
index e2168da..745268c 100644 (file)
@@ -19,7 +19,7 @@ if (!isset($cfgSources[$source])) {
     Horde::applicationUrl($prefs->getValue('initial_page'), true)->redirect();
 }
 
-$driver = Turba_Driver::singleton($source);
+$driver = $injector->getInstance('Turba_Driver')->getDriver($source);
 
 /* Set the contact from the key requested. */
 $key = Horde_Util::getFormData('key');
index 25390cc..51f039b 100644 (file)
@@ -27,7 +27,8 @@ $type = Horde_Util::getFormData('type');
 if (!isset($cfgSources[$source])) {
     throw new Turba_Exception(_("The contact you requested does not exist."));
 }
-$driver = Turba_Driver::singleton($source);
+
+$driver = $injector->getInstance('Turba_Driver')->getDriver($source);
 $object = $driver->getObject($key);
 if (is_a($object, 'PEAR_Error')) {
     throw new Turba_Exception($object);