Lazy construction of the backend drivers.
authorGunnar Wrobel <p@rdus.de>
Thu, 6 Jan 2011 09:11:41 +0000 (10:11 +0100)
committerGunnar Wrobel <p@rdus.de>
Thu, 6 Jan 2011 09:11:41 +0000 (10:11 +0100)
15 files changed:
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Driver.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Driver/Base.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Driver/Cclient.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Driver/Decorator/Base.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Driver/Decorator/Log.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Driver/Decorator/Timer.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Driver/Imap.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Driver/Mock.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Driver/Pear.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Driver/Rcube.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Factory.php
framework/Kolab_Storage/test/Horde/Kolab/Storage/AclTest.php
framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/Driver/ImapTest.php
framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/Driver/PearTest.php
framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/FactoryTest.php

index fd36e67..4f5f72b 100644 (file)
 interface Horde_Kolab_Storage_Driver
 {
     /**
+     * Create the backend driver.
+     *
+     * @return mixed The backend driver.
+     */
+    public function createBackend();
+
+    /**
+     * Returns the actual backend driver.
+     *
+     * If there is no driver set the driver should be constructed within this
+     * method.
+     *
+     * @return mixed The backend driver.
+     */
+    public function getBackend();
+
+    /**
+     * Set the backend driver.
+     *
+     * @param mixed $backend The driver that should be used.
+     *
+     * @return NULL
+     */
+    public function setBackend($backend);
+
+    /**
      * Return the id of the user currently authenticated.
      *
      * @return string The id of the user that opened the connection.
index a264881..b0d15db 100644 (file)
@@ -50,6 +50,13 @@ implements Horde_Kolab_Storage_Driver
     protected $_namespace;
 
     /**
+     * The backend to use.
+     *
+     * @var mixed
+     */
+    private $_backend;
+
+    /**
      * Constructor.
      *
      * @param Horde_Kolab_Storage_Factory $factory A factory for helper objects.
@@ -60,10 +67,51 @@ implements Horde_Kolab_Storage_Driver
         $params = array()
     ) {
         $this->_factory = $factory;
+        if (isset($params['backend'])) {
+            $this->setBackend($params['backend']);
+        }
         $this->_params  = $params;
     }
 
     /**
+     * Returns the actual backend driver.
+     *
+     * If there is no driver set the driver should be constructed within this
+     * method.
+     *
+     * @return mixed The backend driver.
+     */
+    public function getBackend()
+    {
+        if ($this->_backend === null) {
+            $this->_backend = $this->createBackend();
+        }
+        return $this->_backend;
+    }
+
+    /**
+     * Set the backend driver.
+     *
+     * @param mixed $backend The driver that should be used.
+     *
+     * @return NULL
+     */
+    public function setBackend($backend)
+    {
+        $this->_backend = $backend;
+    }
+
+    /**
+     * Return all parameter settings for this connection.
+     *
+     * @return array The parameters.
+     */
+    public function getParams()
+    {
+        return $this->_params;
+    }
+
+    /**
      * Return a parameter setting for this connection.
      *
      * @param string $key     The parameter key.
@@ -77,6 +125,28 @@ implements Horde_Kolab_Storage_Driver
     }
 
     /**
+     * Return the id of the user currently authenticated.
+     *
+     * @return string The id of the user that opened the IMAP connection.
+     */
+    public function getAuth()
+    {
+        return $this->getParam('username');
+    }
+
+    /**
+     * Return the unique connection id.
+     *
+     * @return string The connection id.
+     */
+    public function getId()
+    {
+        return $this->getAuth() . '@'
+            . $this->getParam('host') . ':'
+            . $this->getParam('port');
+    }
+
+    /**
      * Return the factory.
      *
      * @return Horde_Kolab_Storage_Factory The factory.
index 9c7369b..b3496ff 100644 (file)
@@ -29,13 +29,6 @@ class Horde_Kolab_Storage_Driver_Cclient
 extends Horde_Kolab_Storage_Driver_Base
 {
     /**
-     * IMAP resource.
-     *
-     * @var resource
-     */
-    private $_imap;
-
-    /**
      * Server name.
      *
      * @var string
@@ -50,35 +43,29 @@ extends Horde_Kolab_Storage_Driver_Base
     private $_base_mbox;
 
     /**
-     * Lazy connect to the IMAP server.
+     * Create the backend driver.
      *
-     * @return resource The IMAP connection.
-     *
-     * @throws Horde_Kolab_Storage_Exception In case the connection failed.
+     * @return mixed The backend driver.
      */
-    private function _getImap()
+    public function createBackend()
     {
-        if (!isset($this->_imap)) {
-            $result = @imap_open(
-                $this->_getBaseMbox(),
-                $this->getParam('username'),
-                $this->getParam('password'),
-                OP_HALFOPEN
+        $result = @imap_open(
+            $this->_getBaseMbox(),
+            $this->getParam('username'),
+            $this->getParam('password'),
+            OP_HALFOPEN
+        );
+        if (!$result) {
+            throw new Horde_Kolab_Storage_Exception(
+                sprintf(
+                    Horde_Kolab_Storage_Translation::t(
+                        "Connecting to server %s failed. Error: %s"
+                    ),
+                    $this->_getHost(),
+                    @imap_last_error()
+                )
             );
-            if (!$result) {
-                throw new Horde_Kolab_Storage_Exception(
-                    sprintf(
-                        Horde_Kolab_Storage_Translation::t(
-                            "Connecting to server %s failed. Error: %s"
-                        ),
-                        $this->_getHost(),
-                        @imap_last_error()
-                    )
-                );
-            }
-            $this->_imap = $result;
         }
-        return $this->_imap;
     }
 
     /**
@@ -95,26 +82,6 @@ extends Horde_Kolab_Storage_Driver_Base
     }
 
     /**
-     * Return the id of the user currently authenticated.
-     *
-     * @return string The id of the user that opened the IMAP connection.
-     */
-    public function getAuth()
-    {
-        return $this->getParam('username');
-    }
-
-    /**
-     * Return the unique connection id.
-     *
-     * @return string The connection id.
-     */
-    public function getId()
-    {
-        return $this->getAuth() . '@' . $this->getParam('host');
-    }
-
-    /**
      * Return the root mailbox of the current user.
      *
      * @return string The id of the user that opened the IMAP connection.
@@ -145,7 +112,7 @@ extends Horde_Kolab_Storage_Driver_Base
     {
         $folders = array();
 
-        $result = @imap_list($this->_getImap(), $this->_getBaseMbox(), '*');
+        $result = @imap_list($this->getBackend(), $this->_getBaseMbox(), '*');
         if (!$result) {
             throw new Horde_Kolab_Storage_Exception(
                 sprintf(
@@ -188,7 +155,7 @@ extends Horde_Kolab_Storage_Driver_Base
         list($entry, $value) = $this->_getAnnotateMoreEntry($annotation);
         $list = array();
         foreach ($this->getMailboxes() as $mailbox) {
-            $result = imap_getannotation($this->_getImap(), $mailbox, $entry, $value);
+            $result = imap_getannotation($this->getBackend(), $mailbox, $entry, $value);
             if (isset($result[$value])) {
                 $list[$mailbox] = $result[$value];
             }
index ec308da..d78276c 100644 (file)
@@ -56,6 +56,41 @@ implements Horde_Kolab_Storage_Driver
     }
 
     /**
+     * Create the backend driver.
+     *
+     * @return mixed The backend driver.
+     */
+    public function createBackend()
+    {
+        return $this->_driver->createBackend();
+    }
+
+    /**
+     * Returns the actual backend driver.
+     *
+     * If there is no driver set the driver should be constructed within this
+     * method.
+     *
+     * @return mixed The backend driver.
+     */
+    public function getBackend()
+    {
+        return $this->_driver->getBackend();
+    }
+
+    /**
+     * Set the backend driver.
+     *
+     * @param mixed $backend The driver that should be used.
+     *
+     * @return NULL
+     */
+    public function setBackend($backend)
+    {
+        $this->_driver->setBackend($backend);
+    }
+
+    /**
      * Return the id of the user currently authenticated.
      *
      * @return string The id of the user that opened the connection.
index 896731c..ed409fa 100644 (file)
@@ -49,6 +49,25 @@ extends Horde_Kolab_Storage_Driver_Decorator_Base
     }
 
     /**
+     * Create the backend driver.
+     *
+     * @return mixed The backend driver.
+     */
+    public function createBackend()
+    {
+        $this->_logger->info(
+            sprintf('Driver "%s": Creating backend.', $this->getDriverName())
+        );
+        $result = $this->_driver->createBackend();
+        $this->_logger->info(
+            sprintf(
+                'Driver "%s": Backend successfully created', $this->getDriverName()
+            )
+        );
+        return $result;
+    }
+
+    /**
      * Retrieves a list of mailboxes from the server.
      *
      * @return array The list of mailboxes.
index fed2966..10d5979 100644 (file)
@@ -61,6 +61,24 @@ extends Horde_Kolab_Storage_Driver_Decorator_Base
     }
 
     /**
+     * Create the backend driver.
+     *
+     * @return mixed The backend driver.
+     */
+    public function createBackend()
+    {
+        $this->_timer->push();
+        $result = parent::createBackend();
+        $this->_logger->info(
+            sprintf(
+                'REQUEST OUT IMAP: %s ms [construct]',
+                floor($this->_timer->pop() * 1000)
+            )
+        );
+        return $result;
+    }
+
+    /**
      * Retrieves a list of mailboxes from the server.
      *
      * @return array The list of mailboxes.
index 9305716..e3d4a89 100644 (file)
@@ -29,48 +29,21 @@ class Horde_Kolab_Storage_Driver_Imap
 extends Horde_Kolab_Storage_Driver_Base
 {
     /**
-     * The IMAP connection
+     * Create the backend driver.
      *
-     * @var Horde_Imap_Client
+     * @return mixed The backend driver.
      */
-    private $_imap;
-
-    /**
-     * Constructor.
-     *
-     * @param Horde_Imap_Client_Base      $imap    The IMAP connection handler.
-     * @param Horde_Kolab_Storage_Factory $factory A factory for helper objects.
-     * @param array                       $params  Connection parameters.
-     */
-    public function __construct(
-        Horde_Imap_Client_Base $imap,
-        Horde_Kolab_Storage_Factory $factory,
-        $params = array()
-    ) {
-        $this->_imap = $imap;
-        parent::__construct($factory, $params);
-    }
-
-    /**
-     * Return the id of the user currently authenticated.
-     *
-     * @return string The id of the user that opened the IMAP connection.
-     */
-    public function getAuth()
+    public function createBackend()
     {
-        return $this->_imap->getParam('username');
-    }
-
-    /**
-     * Return the unique connection id.
-     *
-     * @return string The connection id.
-     */
-    public function getId()
-    {
-        return $this->getAuth() . '@'
-            . $this->_imap->getParam('hostspec') . ':'
-            . $this->_imap->getParam('port');
+        $config = $this->getParams();
+        $config['hostspec'] = $config['host'];
+        unset($config['host']);
+        //$config['debug'] = '/tmp/imap.log';
+        if ($config['driver'] = 'horde') {
+            return new Horde_Imap_Client_Socket($config);
+        } else {
+            return new Horde_Imap_Client_Cclient($config);
+        }
     }
 
     /**
@@ -80,7 +53,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function getMailboxes()
     {
-        return $this->_imap->listMailboxes('*', Horde_Imap_Client::MBOX_ALL, array('flat' => true));
+        return $this->getBackend()->listMailboxes('*', Horde_Imap_Client::MBOX_ALL, array('flat' => true));
     }
 
     /**
@@ -93,7 +66,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function listAnnotation($annotation)
     {
-        $result = $this->_imap->getMetadata('*', $annotation);
+        $result = $this->getBackend()->getMetadata('*', $annotation);
         $data = array();
         foreach ($result as $folder => $annotations) {
             if (isset($annotations[$annotation])) {
@@ -113,7 +86,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function select($folder)
     {
-        $this->_imap->openMailbox($folder, Horde_Imap_Client::OPEN_AUTO);
+        $this->getBackend()->openMailbox($folder, Horde_Imap_Client::OPEN_AUTO);
         return true;
     }
 
@@ -142,7 +115,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function status($folder)
     {
-        return $this->_imap->status($folder,
+        return $this->getBackend()->status($folder,
                                     Horde_Imap_Client::STATUS_UIDNEXT
                                     | Horde_Imap_Client::STATUS_UIDVALIDITY);
     }
@@ -158,7 +131,7 @@ extends Horde_Kolab_Storage_Driver_Base
     {
         $search_query = new Horde_Imap_Client_Search_Query();
         $search_query->flag('DELETED', false);
-        $uidsearch = $this->_imap->search($folder, $search_query);
+        $uidsearch = $this->getBackend()->search($folder, $search_query);
         $uids = $uidsearch['match'];
         return $uids;
     }
@@ -173,7 +146,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function create($folder)
     {
-        return $this->_imap->createMailbox($folder);
+        return $this->getBackend()->createMailbox($folder);
     }
 
     /**
@@ -186,7 +159,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function delete($folder)
     {
-        return $this->_imap->deleteMailbox($folder);
+        return $this->getBackend()->deleteMailbox($folder);
     }
 
     /**
@@ -200,7 +173,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function rename($old, $new)
     {
-        return $this->_imap->renameMailbox($old, $new);
+        return $this->getBackend()->renameMailbox($old, $new);
     }
 
     /**
@@ -214,7 +187,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function appendMessage($mailbox, $msg)
     {
-        return $this->_imap->append($mailbox, array(array('data' => $msg)));
+        return $this->getBackend()->append($mailbox, array(array('data' => $msg)));
     }
 
     /**
@@ -229,7 +202,7 @@ extends Horde_Kolab_Storage_Driver_Base
         if (!is_array($uids)) {
             $uids = array($uids);
         }
-        return $this->_imap->store($mailbox, array('add' => array('\\deleted'), 'ids' => $uids));
+        return $this->getBackend()->store($mailbox, array('add' => array('\\deleted'), 'ids' => $uids));
     }
 
     /**
@@ -243,7 +216,7 @@ extends Horde_Kolab_Storage_Driver_Base
     public function moveMessage($old_folder, $uid, $new_folder)
     {
         $options = array('ids' => array($uid), 'move' => true);
-        return $this->_imap->copy($old_folder, $new_folder, $options);
+        return $this->getBackend()->copy($old_folder, $new_folder, $options);
     }
 
     /**
@@ -256,7 +229,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function expunge($mailbox)
     {
-        return $this->_imap->expunge($mailbox);
+        return $this->getBackend()->expunge($mailbox);
     }
 
     /**
@@ -278,7 +251,7 @@ extends Horde_Kolab_Storage_Driver_Base
                 )
             )
         );
-        $result = $this->_imap->fetch($mailbox, $criteria, $options);
+        $result = $this->getBackend()->fetch($mailbox, $criteria, $options);
         return $result[$uid]['headertext'][0];
     }
 
@@ -300,7 +273,7 @@ extends Horde_Kolab_Storage_Driver_Base
                 )
             )
         );
-        $result = $this->_imap->fetch($mailbox, $criteria, $options);
+        $result = $this->getBackend()->fetch($mailbox, $criteria, $options);
         return $result[$uid]['bodytext'][0];
     }
 
@@ -314,7 +287,7 @@ extends Horde_Kolab_Storage_Driver_Base
     public function getAcl(Horde_Kolab_Storage_Folder $folder)
     {
         //@todo: Separate driver class
-        if ($this->_imap->queryCapability('ACL') === true) {
+        if ($this->getBackend()->queryCapability('ACL') === true) {
             if ($folder->getOwner() == $this->getAuth()) {
                 try {
                     return $this->_getAcl($folder->getPath());
@@ -345,7 +318,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     private function _getAcl($folder)
     {
-        $acl = $this->_imap->getACL($folder);
+        $acl = $this->getBackend()->getACL($folder);
         $result = array();
         foreach ($acl as $user => $rights) {
             $result[$user] = join('', $rights);
@@ -362,7 +335,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     private function _getMyAcl($folder)
     {
-        return $this->_imap->getMyACLRights($folder);
+        return $this->getBackend()->getMyACLRights($folder);
     }
 
     /**
@@ -377,8 +350,8 @@ extends Horde_Kolab_Storage_Driver_Base
     public function setAcl($folder, $user, $acl)
     {
         //@todo: Separate driver class
-        if ($this->_imap->queryCapability('ACL') === true) {
-            $this->_imap->setACL($folder, $user, array('rights' => $acl));
+        if ($this->getBackend()->queryCapability('ACL') === true) {
+            $this->getBackend()->setACL($folder, $user, array('rights' => $acl));
         }
     }
 
@@ -393,8 +366,8 @@ extends Horde_Kolab_Storage_Driver_Base
     public function deleteAcl($folder, $user)
     {
         //@todo: Separate driver class
-        if ($this->_imap->queryCapability('ACL') === true) {
-            $this->_imap->setACL($folder, $user, array('remove' => true));
+        if ($this->getBackend()->queryCapability('ACL') === true) {
+            $this->getBackend()->setACL($folder, $user, array('remove' => true));
         }
     }
 
@@ -409,7 +382,7 @@ extends Horde_Kolab_Storage_Driver_Base
     public function getAnnotation($entry, $mailbox_name)
     {
         try {
-            $result = $this->_imap->getMetadata($mailbox_name, $entry);
+            $result = $this->getBackend()->getMetadata($mailbox_name, $entry);
         } catch (Exception $e) {
             return '';
         }
@@ -427,7 +400,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function setAnnotation($entry, $value, $mailbox_name)
     {
-        return $this->_imap->setMetadata($mailbox_name,
+        return $this->getBackend()->setMetadata($mailbox_name,
                                          array($entry => $value));
     }
 
@@ -440,10 +413,10 @@ extends Horde_Kolab_Storage_Driver_Base
     public function getNamespace()
     {
         if ($this->_namespace === null
-            && $this->_imap->queryCapability('NAMESPACE') === true) {
+            && $this->getBackend()->queryCapability('NAMESPACE') === true) {
             $c = array();
             $configuration = $this->getParam('namespaces', array());
-            foreach ($this->_imap->getNamespaces() as $namespace) {
+            foreach ($this->getBackend()->getNamespaces() as $namespace) {
                 if (in_array($namespace['name'], array_keys($configuration))) {
                     $namespace = array_merge($namespace, $configuration[$namespace['name']]);
                 }
index f5cdc70..8394726 100644 (file)
@@ -108,13 +108,12 @@ extends Horde_Kolab_Storage_Driver_Base
     }
 
     /**
-     * Return the id of the user currently authenticated.
+     * Create the backend driver.
      *
-     * @return string The id of the user that opened the IMAP connection.
+     * @return mixed The backend driver.
      */
-    public function getAuth()
+    public function createBackend()
     {
-        return $this->getParam('username');
     }
 
     /**
index b08e70f..f03d59a 100644 (file)
@@ -29,48 +29,18 @@ class Horde_Kolab_Storage_Driver_Pear
 extends Horde_Kolab_Storage_Driver_Base
 {
     /**
-     * The IMAP client.
+     * Create the backend driver.
      *
-     * @var Net_IMAP
+     * @return mixed The backend driver.
      */
-    private $_imap;
-
-    /**
-     * Constructor.
-     *
-     * @param Net_IMAP                    $imap    The IMAP connection handler.
-     * @param Horde_Kolab_Storage_Factory $factory A factory for helper objects.
-     * @param array                       $params  Connection parameters.
-     */
-    public function __construct(
-        Net_IMAP $imap,
-        Horde_Kolab_Storage_Factory $factory,
-        $params = array()
-    ) {
-        $this->_imap = $imap;
-        parent::__construct($factory, $params);
-    }
-
-    /**
-     * Return the id of the user currently authenticated.
-     *
-     * @return string The id of the user that opened the IMAP connection.
-     */
-    public function getAuth()
-    {
-        return $this->getParam('username');
-    }
-
-    /**
-     * Return the unique connection id.
-     *
-     * @return string The connection id.
-     */
-    public function getId()
+    public function createBackend()
     {
-        return $this->getAuth() . '@'
-            . $this->getParam('host') . ':'
-            . $this->getParam('port');
+        $config = $this->getParams();
+        $client = new Net_IMAP($config['host'], $config['port']);
+        Horde_Kolab_Storage_Exception_Pear::catchError(
+            $client->login($config['username'], $config['password'])
+        );
+        return $client;
     }
 
     /**
@@ -80,7 +50,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function getMailboxes()
     {
-        return $this->_imap->getMailboxes();
+        return $this->getBackend()->getMailboxes();
     }
 
     /**
@@ -95,7 +65,7 @@ extends Horde_Kolab_Storage_Driver_Base
     {
         list($entry, $value) = $this->_getAnnotateMoreEntry($annotation);
         $list = array();
-        $result = $this->_imap->getAnnotation($entry, $value, '*');
+        $result = $this->getBackend()->getAnnotation($entry, $value, '*');
         foreach ($result as $element) {
             if (isset($element['ATTRIBUTES'][$value])) {
                 $list[$element['MAILBOX']] = $element['ATTRIBUTES'][$value];
@@ -114,7 +84,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function select($folder)
     {
-        $this->_imap->openMailbox($folder, Horde_Imap_Client::OPEN_AUTO);
+        $this->getBackend()->openMailbox($folder, Horde_Imap_Client::OPEN_AUTO);
         return true;
     }
 
@@ -143,7 +113,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function status($folder)
     {
-        return $this->_imap->status($folder,
+        return $this->getBackend()->status($folder,
                                     Horde_Imap_Client::STATUS_UIDNEXT
                                     | Horde_Imap_Client::STATUS_UIDVALIDITY);
     }
@@ -159,7 +129,7 @@ extends Horde_Kolab_Storage_Driver_Base
     {
         $search_query = new Horde_Imap_Client_Search_Query();
         $search_query->flag('DELETED', false);
-        $uidsearch = $this->_imap->search($folder, $search_query);
+        $uidsearch = $this->getBackend()->search($folder, $search_query);
         $uids = $uidsearch['match'];
         return $uids;
     }
@@ -174,7 +144,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function create($folder)
     {
-        return $this->_imap->createMailbox($folder);
+        return $this->getBackend()->createMailbox($folder);
     }
 
     /**
@@ -187,7 +157,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function delete($folder)
     {
-        return $this->_imap->deleteMailbox($folder);
+        return $this->getBackend()->deleteMailbox($folder);
     }
 
     /**
@@ -201,7 +171,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function rename($old, $new)
     {
-        return $this->_imap->renameMailbox($old, $new);
+        return $this->getBackend()->renameMailbox($old, $new);
     }
 
     /**
@@ -215,7 +185,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function appendMessage($mailbox, $msg)
     {
-        return $this->_imap->append($mailbox, array(array('data' => $msg)));
+        return $this->getBackend()->append($mailbox, array(array('data' => $msg)));
     }
 
     /**
@@ -230,7 +200,7 @@ extends Horde_Kolab_Storage_Driver_Base
         if (!is_array($uids)) {
             $uids = array($uids);
         }
-        return $this->_imap->store($mailbox, array('add' => array('\\deleted'), 'ids' => $uids));
+        return $this->getBackend()->store($mailbox, array('add' => array('\\deleted'), 'ids' => $uids));
     }
 
     /**
@@ -244,7 +214,7 @@ extends Horde_Kolab_Storage_Driver_Base
     public function moveMessage($old_folder, $uid, $new_folder)
     {
         $options = array('ids' => array($uid), 'move' => true);
-        return $this->_imap->copy($old_folder, $new_folder, $options);
+        return $this->getBackend()->copy($old_folder, $new_folder, $options);
     }
 
     /**
@@ -257,7 +227,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function expunge($mailbox)
     {
-        return $this->_imap->expunge($mailbox);
+        return $this->getBackend()->expunge($mailbox);
     }
 
     /**
@@ -279,7 +249,7 @@ extends Horde_Kolab_Storage_Driver_Base
                 )
             )
         );
-        $result = $this->_imap->fetch($mailbox, $criteria, $options);
+        $result = $this->getBackend()->fetch($mailbox, $criteria, $options);
         return $result[$uid]['headertext'][0];
     }
 
@@ -301,7 +271,7 @@ extends Horde_Kolab_Storage_Driver_Base
                 )
             )
         );
-        $result = $this->_imap->fetch($mailbox, $criteria, $options);
+        $result = $this->getBackend()->fetch($mailbox, $criteria, $options);
         return $result[$uid]['bodytext'][0];
     }
 
@@ -315,7 +285,7 @@ extends Horde_Kolab_Storage_Driver_Base
     public function getAcl(Horde_Kolab_Storage_Folder $folder)
     {
         //@todo: Separate driver class
-        if ($this->_imap->queryCapability('ACL') === true) {
+        if ($this->getBackend()->queryCapability('ACL') === true) {
             if ($folder->getOwner() == $this->getAuth()) {
                 try {
                     return $this->_getAcl($folder->getName());
@@ -346,7 +316,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     private function _getAcl($folder)
     {
-        $acl = $this->_imap->getACL($folder);
+        $acl = $this->getBackend()->getACL($folder);
         $result = array();
         foreach ($acl as $user => $rights) {
             $result[$user] = join('', $rights);
@@ -363,7 +333,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     private function _getMyAcl($folder)
     {
-        return $this->_imap->getMyACLRights($folder);
+        return $this->getBackend()->getMyACLRights($folder);
     }
 
     /**
@@ -378,8 +348,8 @@ extends Horde_Kolab_Storage_Driver_Base
     public function setAcl($folder, $user, $acl)
     {
         //@todo: Separate driver class
-        if ($this->_imap->queryCapability('ACL') === true) {
-            $this->_imap->setACL($folder, $user, array('rights' => $acl));
+        if ($this->getBackend()->queryCapability('ACL') === true) {
+            $this->getBackend()->setACL($folder, $user, array('rights' => $acl));
         }
     }
 
@@ -394,8 +364,8 @@ extends Horde_Kolab_Storage_Driver_Base
     public function deleteAcl($folder, $user)
     {
         //@todo: Separate driver class
-        if ($this->_imap->queryCapability('ACL') === true) {
-            $this->_imap->setACL($folder, $user, array('remove' => true));
+        if ($this->getBackend()->queryCapability('ACL') === true) {
+            $this->getBackend()->setACL($folder, $user, array('remove' => true));
         }
     }
 
@@ -410,7 +380,7 @@ extends Horde_Kolab_Storage_Driver_Base
     public function getAnnotation($entry, $mailbox_name)
     {
         try {
-            $result = $this->_imap->getMetadata($mailbox_name, $entry);
+            $result = $this->getBackend()->getMetadata($mailbox_name, $entry);
         } catch (Exception $e) {
             return '';
         }
@@ -428,7 +398,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function setAnnotation($entry, $value, $mailbox_name)
     {
-        return $this->_imap->setMetadata($mailbox_name,
+        return $this->getBackend()->setMetadata($mailbox_name,
                                          array($entry => $value));
     }
 
@@ -440,9 +410,9 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function getNamespace()
     {
-        if ($this->_imap->hasCapability('NAMESPACE') === true) {
+        if ($this->getBackend()->hasCapability('NAMESPACE') === true) {
             $namespaces = array();
-            foreach ($this->_imap->getNamespace() as $type => $elements) {
+            foreach ($this->getBackend()->getNamespace() as $type => $elements) {
                 foreach ($elements as $namespace) {
                     switch ($type) {
                     case 'personal':
index 68ac512..2b9d27f 100644 (file)
@@ -29,48 +29,26 @@ class Horde_Kolab_Storage_Driver_Rcube
 extends Horde_Kolab_Storage_Driver_Base
 {
     /**
-     * The IMAP client.
+     * Create the backend driver.
      *
-     * @var rcube_imap_generic
+     * @return mixed The backend driver.
      */
-    private $_imap;
-
-    /**
-     * Constructor.
-     *
-     * @param rcube_imap_generic          $imap    The IMAP connection handler.
-     * @param Horde_Kolab_Storage_Factory $factory A factory for helper objects.
-     * @param array                       $params  Connection parameters.
-     */
-    public function __construct(
-        rcube_imap_generic $imap,
-        Horde_Kolab_Storage_Factory $factory,
-        $params = array()
-    ) {
-        $this->_imap = $imap;
-        parent::__construct($factory, $params);
-    }
-
-    /**
-     * Return the id of the user currently authenticated.
-     *
-     * @return string The id of the user that opened the IMAP connection.
-     */
-    public function getAuth()
+    public function createBackend()
     {
-        return $this->getParam('username');
-    }
-
-    /**
-     * Return the unique connection id.
-     *
-     * @return string The connection id.
-     */
-    public function getId()
-    {
-        return $this->getAuth() . '@'
-            . $this->getParam('host') . ':'
-            . $this->getParam('port');
+        $config = $this->getParams();
+        $client = new rcube_imap_generic();
+        //$client->setDebug(true);
+        $client->connect(
+            $config['host'], $config['username'], $config['password'],
+            array(
+                'debug_mode' => false,
+                'ssl_mode' => false,
+                'port' => $config['port'],
+                'timeout' => 0,
+                'force_caps' => false,
+            )
+        );
+        return $client;
     }
 
     /**
@@ -80,7 +58,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function getMailboxes()
     {
-        return $this->_imap->listMailboxes('', '*');
+        return $this->getBackend()->listMailboxes('', '*');
     }
 
     /**
@@ -94,7 +72,7 @@ extends Horde_Kolab_Storage_Driver_Base
     public function listAnnotation($annotation)
     {
         list($entry, $value) = $this->_getAnnotateMoreEntry($annotation);
-        $result = $this->_imap->getAnnotation('*', $entry, $value);
+        $result = $this->getBackend()->getAnnotation('*', $entry, $value);
         if (empty($result)) {
             return array();
         }
@@ -117,7 +95,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function select($folder)
     {
-        $this->_imap->openMailbox($folder, Horde_Imap_Client::OPEN_AUTO);
+        $this->getBackend()->openMailbox($folder, Horde_Imap_Client::OPEN_AUTO);
         return true;
     }
 
@@ -146,7 +124,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function status($folder)
     {
-        return $this->_imap->status($folder,
+        return $this->getBackend()->status($folder,
                                     Horde_Imap_Client::STATUS_UIDNEXT
                                     | Horde_Imap_Client::STATUS_UIDVALIDITY);
     }
@@ -162,7 +140,7 @@ extends Horde_Kolab_Storage_Driver_Base
     {
         $search_query = new Horde_Imap_Client_Search_Query();
         $search_query->flag('DELETED', false);
-        $uidsearch = $this->_imap->search($folder, $search_query);
+        $uidsearch = $this->getBackend()->search($folder, $search_query);
         $uids = $uidsearch['match'];
         return $uids;
     }
@@ -177,7 +155,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function create($folder)
     {
-        return $this->_imap->createMailbox($folder);
+        return $this->getBackend()->createMailbox($folder);
     }
 
     /**
@@ -190,7 +168,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function delete($folder)
     {
-        return $this->_imap->deleteMailbox($folder);
+        return $this->getBackend()->deleteMailbox($folder);
     }
 
     /**
@@ -204,7 +182,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function rename($old, $new)
     {
-        return $this->_imap->renameMailbox($old, $new);
+        return $this->getBackend()->renameMailbox($old, $new);
     }
 
     /**
@@ -218,7 +196,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function appendMessage($mailbox, $msg)
     {
-        return $this->_imap->append($mailbox, array(array('data' => $msg)));
+        return $this->getBackend()->append($mailbox, array(array('data' => $msg)));
     }
 
     /**
@@ -233,7 +211,7 @@ extends Horde_Kolab_Storage_Driver_Base
         if (!is_array($uids)) {
             $uids = array($uids);
         }
-        return $this->_imap->store($mailbox, array('add' => array('\\deleted'), 'ids' => $uids));
+        return $this->getBackend()->store($mailbox, array('add' => array('\\deleted'), 'ids' => $uids));
     }
 
     /**
@@ -247,7 +225,7 @@ extends Horde_Kolab_Storage_Driver_Base
     public function moveMessage($old_folder, $uid, $new_folder)
     {
         $options = array('ids' => array($uid), 'move' => true);
-        return $this->_imap->copy($old_folder, $new_folder, $options);
+        return $this->getBackend()->copy($old_folder, $new_folder, $options);
     }
 
     /**
@@ -260,7 +238,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function expunge($mailbox)
     {
-        return $this->_imap->expunge($mailbox);
+        return $this->getBackend()->expunge($mailbox);
     }
 
     /**
@@ -282,7 +260,7 @@ extends Horde_Kolab_Storage_Driver_Base
                 )
             )
         );
-        $result = $this->_imap->fetch($mailbox, $criteria, $options);
+        $result = $this->getBackend()->fetch($mailbox, $criteria, $options);
         return $result[$uid]['headertext'][0];
     }
 
@@ -304,7 +282,7 @@ extends Horde_Kolab_Storage_Driver_Base
                 )
             )
         );
-        $result = $this->_imap->fetch($mailbox, $criteria, $options);
+        $result = $this->getBackend()->fetch($mailbox, $criteria, $options);
         return $result[$uid]['bodytext'][0];
     }
 
@@ -318,7 +296,7 @@ extends Horde_Kolab_Storage_Driver_Base
     public function getAcl(Horde_Kolab_Storage_Folder $folder)
     {
         //@todo: Separate driver class
-        if ($this->_imap->queryCapability('ACL') === true) {
+        if ($this->getBackend()->queryCapability('ACL') === true) {
             if ($folder->getOwner() == $this->getAuth()) {
                 try {
                     return $this->_getAcl($folder->getName());
@@ -349,7 +327,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     private function _getAcl($folder)
     {
-        $acl = $this->_imap->getACL($folder);
+        $acl = $this->getBackend()->getACL($folder);
         $result = array();
         foreach ($acl as $user => $rights) {
             $result[$user] = join('', $rights);
@@ -366,7 +344,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     private function _getMyAcl($folder)
     {
-        return $this->_imap->getMyACLRights($folder);
+        return $this->getBackend()->getMyACLRights($folder);
     }
 
     /**
@@ -381,8 +359,8 @@ extends Horde_Kolab_Storage_Driver_Base
     public function setAcl($folder, $user, $acl)
     {
         //@todo: Separate driver class
-        if ($this->_imap->queryCapability('ACL') === true) {
-            $this->_imap->setACL($folder, $user, array('rights' => $acl));
+        if ($this->getBackend()->queryCapability('ACL') === true) {
+            $this->getBackend()->setACL($folder, $user, array('rights' => $acl));
         }
     }
 
@@ -397,8 +375,8 @@ extends Horde_Kolab_Storage_Driver_Base
     public function deleteAcl($folder, $user)
     {
         //@todo: Separate driver class
-        if ($this->_imap->queryCapability('ACL') === true) {
-            $this->_imap->setACL($folder, $user, array('remove' => true));
+        if ($this->getBackend()->queryCapability('ACL') === true) {
+            $this->getBackend()->setACL($folder, $user, array('remove' => true));
         }
     }
 
@@ -413,7 +391,7 @@ extends Horde_Kolab_Storage_Driver_Base
     public function getAnnotation($entry, $mailbox_name)
     {
         try {
-            $result = $this->_imap->getMetadata($mailbox_name, $entry);
+            $result = $this->getBackend()->getMetadata($mailbox_name, $entry);
         } catch (Exception $e) {
             return '';
         }
@@ -431,7 +409,7 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function setAnnotation($entry, $value, $mailbox_name)
     {
-        return $this->_imap->setMetadata($mailbox_name,
+        return $this->getBackend()->setMetadata($mailbox_name,
                                          array($entry => $value));
     }
 
@@ -443,9 +421,9 @@ extends Horde_Kolab_Storage_Driver_Base
      */
     public function getNamespace()
     {
-        if ($this->_imap->hasCapability('NAMESPACE') === true) {
+        if ($this->getBackend()->hasCapability('NAMESPACE') === true) {
             $namespaces = array();
-            foreach ($this->_imap->getNamespace() as $type => $elements) {
+            foreach ($this->getBackend()->getNamespace() as $type => $elements) {
                 foreach ($elements as $namespace) {
                     switch ($type) {
                     case 'personal':
index 682bf86..e5c22f3 100644 (file)
@@ -123,54 +123,17 @@ class Horde_Kolab_Storage_Factory
             $driver = new Horde_Kolab_Storage_Driver_Mock($this, $config);
             break;
         case 'horde':
-            $config['hostspec'] = $config['host'];
-            unset($config['host']);
-            //$config['debug'] = '/tmp/imap.log';
-            $driver = new Horde_Kolab_Storage_Driver_Imap(
-                new Horde_Imap_Client_Socket(
-                    $config
-                ),
-                $this
-            );
-            break;
         case 'horde-php':
-            $config['hostspec'] = $config['host'];
-            unset($config['host']);
-            $driver = new Horde_Kolab_Storage_Driver_Imap(
-                new Horde_Imap_Client_Cclient(
-                    $config
-                ),
-                $this
-            );
+            $driver = new Horde_Kolab_Storage_Driver_Imap($this, $config);
             break;
         case 'php':
             $driver = new Horde_Kolab_Storage_Driver_Cclient($this, $config);
             break;
         case 'pear':
-            $client = new Net_IMAP($config['host'], $config['port']);
-            Horde_Kolab_Storage_Exception_Pear::catchError(
-                $client->login($config['username'], $config['password'])
-            );
-            $driver = new Horde_Kolab_Storage_Driver_Pear(
-                $client, $this, $config
-            );
+            $driver = new Horde_Kolab_Storage_Driver_Pear($this, $config);
             break;
         case 'roundcube':
-            $client = new rcube_imap_generic();
-            //$client->setDebug(true);
-            $client->connect(
-                $config['host'], $config['username'], $config['password'],
-                array(
-                    'debug_mode' => false,
-                    'ssl_mode' => false,
-                    'port' => $config['port'],
-                    'timeout' => 0,
-                    'force_caps' => false,
-                )
-            );
-            $driver = new Horde_Kolab_Storage_Driver_Rcube(
-                $client, $this, $config
-            );
+            $driver = new Horde_Kolab_Storage_Driver_Rcube($this, $config);
             break;
         default:
             throw new Horde_Kolab_Storage_Exception(
@@ -188,12 +151,6 @@ class Horde_Kolab_Storage_Factory
             );
         }
         if (!empty($params['timelog'])) {
-            $params['timelog']->info(
-                sprintf(
-                    'REQUEST OUT IMAP: %s ms [construct]',
-                    floor($timer->pop() * 1000)
-                )
-            );
             $driver = new Horde_Kolab_Storage_Driver_Decorator_Timer(
                 $driver, $timer, $params['timelog']
             );
index 0a9b8ca..1345203 100644 (file)
@@ -37,8 +37,8 @@ class Horde_Kolab_Storage_AclTest extends PHPUnit_Framework_TestCase
         $this->_storage = $this->getMock('Horde_Kolab_Storage', array(), array(), '', false, false);
         $this->_imap = $this->getMock('Horde_Imap_Client_Socket', array(), array(), '', false, false);
         $this->_connection = new Horde_Kolab_Storage_Driver_Imap(
-            $this->_imap,
-            new Horde_Kolab_Storage_Factory()
+            new Horde_Kolab_Storage_Factory(),
+            array('backend' => $this->_imap, 'username' => 'user')
         );
         $this->_imap->expects($this->any())
             ->method('getNamespaces')
@@ -93,10 +93,6 @@ class Horde_Kolab_Storage_AclTest extends PHPUnit_Framework_TestCase
             ->method('getMyACLRights')
             ->with('INBOX')
             ->will($this->returnValue('lr'));
-        $this->_imap->expects($this->any())
-            ->method('getParam')
-            ->with('username')
-            ->will($this->returnValue('user'));
         $folder = $this->_getFolder('INBOX');
         $this->assertEquals(array('user' => 'lr'), $folder->getAcl());
     }
@@ -109,14 +105,10 @@ class Horde_Kolab_Storage_AclTest extends PHPUnit_Framework_TestCase
             ->will($this->returnValue(array('INBOX')));
         $this->_imap->expects($this->once())
             ->method('getMyACLRights')
-            ->with('user/test')
+            ->with('user/user')
             ->will($this->returnValue('lr'));
-        $this->_imap->expects($this->any())
-            ->method('getParam')
-            ->with('username')
-            ->will($this->returnValue('test'));
-        $folder = $this->_getFolder('user/test');
-        $this->assertEquals(array('test' => 'lr'), $folder->getAcl());
+        $folder = $this->_getFolder('user/user');
+        $this->assertEquals(array('user' => 'lr'), $folder->getAcl());
     }
 
     public function testGetaclRetrievesAllFolderAclOnForeignFolderWithAdminRights()
@@ -127,18 +119,14 @@ class Horde_Kolab_Storage_AclTest extends PHPUnit_Framework_TestCase
             ->will($this->returnValue(array('INBOX')));
         $this->_imap->expects($this->once())
             ->method('getMyACLRights')
-            ->with('user/test')
+            ->with('user/user')
             ->will($this->returnValue('lra'));
         $this->_imap->expects($this->once())
             ->method('getAcl')
-            ->with('user/test')
-            ->will($this->returnValue(array('test' => 'lra')));
-        $this->_imap->expects($this->any())
-            ->method('getParam')
-            ->with('username')
-            ->will($this->returnValue('test'));
-        $folder = $this->_getFolder('user/test');
-        $this->assertEquals(array('test' => 'lra'), $folder->getAcl());
+            ->with('user/user')
+            ->will($this->returnValue(array('user' => 'lra')));
+        $folder = $this->_getFolder('user/user');
+        $this->assertEquals(array('user' => 'lra'), $folder->getAcl());
     }
 
     public function testSetacletsFolderAcl()
@@ -170,10 +158,6 @@ class Horde_Kolab_Storage_AclTest extends PHPUnit_Framework_TestCase
         $this->_imap->expects($this->once())
             ->method('listMailboxes')
             ->will($this->returnValue(array('INBOX')));
-        $this->_imap->expects($this->any())
-            ->method('getParam')
-            ->with('username')
-            ->will($this->returnValue('user'));
         $folder = $this->_getFolder('INBOX');
         $this->assertEquals(array('user' => 'lrid'), $folder->getAcl());
     }
index 8e06088..2e7d6e9 100644 (file)
@@ -38,9 +38,8 @@ extends PHPUnit_Framework_TestCase
     public function testGetNamespaceReturnsNamespaceHandler()
     {
         $driver = new Horde_Kolab_Storage_Driver_Imap(
-            $this->_getNamespaceMock(),
             new Horde_Kolab_Storage_Factory(),
-            array()
+            array('backend' => $this->_getNamespaceMock())
         );
         $this->assertType(
             'Horde_Kolab_Storage_Folder_Namespace',
@@ -51,9 +50,8 @@ extends PHPUnit_Framework_TestCase
     public function testGetNamespaceReturnsExpectedNamespaces()
     {
         $driver = new Horde_Kolab_Storage_Driver_Imap(
-            $this->_getNamespaceMock(),
             new Horde_Kolab_Storage_Factory(),
-            array()
+            array('backend' => $this->_getNamespaceMock())
         );
         $namespaces = array();
         foreach ($driver->getNamespace() as $namespace) {
index 050881b..945b5b6 100644 (file)
@@ -38,9 +38,8 @@ extends PHPUnit_Framework_TestCase
     public function testGetNamespaceReturnsNamespaceHandler()
     {
         $driver = new Horde_Kolab_Storage_Driver_Pear(
-            $this->_getNamespaceMock(),
             new Horde_Kolab_Storage_Factory(),
-            array()
+            array('backend' => $this->_getNamespaceMock())
         );
         $this->assertType(
             'Horde_Kolab_Storage_Folder_Namespace',
@@ -51,9 +50,8 @@ extends PHPUnit_Framework_TestCase
     public function testGetNamespaceReturnsExpectedNamespaces()
     {
         $driver = new Horde_Kolab_Storage_Driver_Pear(
-            $this->_getNamespaceMock(),
             new Horde_Kolab_Storage_Factory(),
-            array()
+            array('backend' => $this->_getNamespaceMock())
         );
         $namespaces = array();
         foreach ($driver->getNamespace() as $namespace) {
index d7da93c..25a6588 100644 (file)
@@ -152,20 +152,6 @@ extends Horde_Kolab_Storage_TestCase
         );
     }
 
-    public function testTimedDriverConstruction()
-    {
-        $factory = new Horde_Kolab_Storage_Factory();
-        $logger = $this->getMockLogger();
-        $factory->createDriverFromParams(
-            array(
-                'driver' => 'mock',
-                'logger' => $logger,
-                'timelog' => $logger,
-            )
-        );
-        $this->assertLogRegExp('/REQUEST OUT IMAP:.*construct.*/');
-    }
-
     public function testCreateTypeReturnsType()
     {
         $factory = new Horde_Kolab_Storage_Factory();