Have Horde_Imap_Client_Base implement Serializable
authorMichael M Slusarz <slusarz@curecanti.org>
Sat, 4 Sep 2010 08:03:57 +0000 (02:03 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Mon, 6 Sep 2010 20:31:15 +0000 (14:31 -0600)
Provides a more compact way of storing serialized data.
Reduces complexity as child functions do not need to extend the
serialization-related methods.
Implement versioning in order to invalidate cache when upgrading in
future.

framework/Imap_Client/lib/Horde/Imap/Client/Base.php
framework/Imap_Client/lib/Horde/Imap/Client/Cclient.php
framework/Imap_Client/lib/Horde/Imap/Client/Cclient/Pop3.php
framework/Imap_Client/lib/Horde/Imap/Client/Socket.php
framework/Imap_Client/lib/Horde/Imap/Client/Socket/Pop3.php

index cc19167..6c0ea64 100644 (file)
  * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
  * @package  Imap_Client
  */
-abstract class Horde_Imap_Client_Base
+abstract class Horde_Imap_Client_Base implements Serializable
 {
+    /* Serialized version. */
+    const VERSION = 1;
+
     /**
      * The Horde_Imap_Client_Utils object
      *
@@ -84,6 +87,14 @@ abstract class Horde_Imap_Client_Base
      */
     protected $_debug = null;
 
+
+    /**
+     * The list of variables to serialize.
+     *
+     * @var array
+     */
+    protected $_store = array('_init', '_params');
+
     /**
      * Temp array (destroyed at end of process).
      *
@@ -92,13 +103,14 @@ abstract class Horde_Imap_Client_Base
     protected $_temp = array();
 
     /**
-     * Constructs a new Horde_Imap_Client object.
+     * Constructs a new Horde_Imap_Client_Base object.
      *
      * @param array $params  A hash containing configuration parameters.
+     *                       See Horde_Imap_Client::factory().
      *
      * @throws Horde_Imap_Client_Exception
      */
-    public function __construct($params = array())
+    public function __construct(array $params = array())
     {
         if (!isset($params['username']) || !isset($params['password'])) {
             throw new Horde_Imap_Client_Exception('Horde_Imap_Client requires a username and password.');
@@ -142,52 +154,76 @@ abstract class Horde_Imap_Client_Base
 
         $this->_params = $params;
 
-        $this->utils = new Horde_Imap_Client_Utils();
-
-        // This will initialize debugging, if needed.
-        $this->__wakeup();
+        $this->_init();
     }
 
     /**
-     * Destructor.
+     * Do initialization tasks.
      */
-    public function __destruct()
+    protected function _init()
     {
-        $this->_closeDebug();
+        $this->utils = new Horde_Imap_Client_Utils();
+
+        if (!empty($this->_params['debug'])) {
+            $this->_debug = @fopen($this->_params['debug'], 'a');
+        }
     }
 
     /**
-     * Do cleanup prior to serialization.
+     * Destructor.
      */
-    public function __sleep()
+    public function __destruct()
     {
-        $this->_closeDebug();
+        $this->logout();
 
-        // Don't store Horde_Imap_Client_Cache object or temp data.
-        $this->cache = null;
-        $this->_temp = array();
+        /* Close debugging output. */
+        if (is_resource($this->_debug)) {
+            fflush($this->_debug);
+            fclose($this->_debug);
+            $this->_debug = null;
+        }
     }
 
     /**
-     * Do re-initialization on unserialize().
+     * Serialize.
+     *
+     * @return string  Serialized representation of this object.
      */
-    public function __wakeup()
+    public function serialize()
     {
-        if (!empty($this->_params['debug'])) {
-            $this->_debug = @fopen($this->_params['debug'], 'a');
+        $store = array(
+            '__version' => self::VERSION
+        );
+
+        foreach ($this->_store as $val) {
+            $store[$val] = $this->$val;
         }
+
+        return serialize($store);
     }
 
     /**
-     * Close debugging output.
+     * Unserialize.
+     *
+     * @param string $data  Serialized data.
+     *
+     * @throws Exception
      */
-    protected function _closeDebug()
+    public function unserialize($data)
     {
-        if (is_resource($this->_debug)) {
-            fflush($this->_debug);
-            fclose($this->_debug);
-            $this->_debug = null;
+        $data = @unserialize($data);
+        if (!is_array($data) ||
+            !isset($data['__version']) ||
+            ($data['__version'] != self::VERSION)) {
+            throw new Exception('Cache version change');
+        }
+        unset($data['__version']);
+
+        foreach ($data as $key => $val) {
+            $this->$key = $val;
         }
+
+        $this->_init();
     }
 
     /**
index 200e30c..7eafe5d 100644 (file)
@@ -107,23 +107,16 @@ class Horde_Imap_Client_Cclient extends Horde_Imap_Client_Base
      *
      * @param array $params  A hash containing configuration parameters.
      */
-    public function __construct($params)
+    public function __construct(array $params = array())
     {
         if (!isset($params['retries'])) {
             $params['retries'] = 3;
         }
-        parent::__construct($params);
-    }
 
-    /**
-     * Do cleanup prior to serialization and provide a list of variables
-     * to serialize.
-     */
-    public function __sleep()
-    {
-        $this->logout();
-        parent::__sleep();
-        return array_diff(array_keys(get_class_vars(__CLASS__)), array('encryptKey'));
+        $this->_store[] = '_cstring';
+        $this->_store[] = '_service';
+
+        parent::__construct($params);
     }
 
     /**
index 8e33ca1..daff1cf 100644 (file)
@@ -39,6 +39,21 @@ class Horde_Imap_Client_Cclient_Pop3 extends Horde_Imap_Client_Cclient
     }
 
     /**
+     * Unserialize.
+     *
+     * @param string $data  Serialized data.
+     *
+     * @throws Exception
+     */
+    public function unserialize($data)
+    {
+        parent::unserialize($data);
+
+        // Disable caching.
+        $this->_params['cache'] = array('fields' => array());
+    }
+
+    /**
      * Get CAPABILITY info from the IMAP server.
      *
      * @throws Horde_Imap_Client_Exception
index dfaa72f..262c93c 100644 (file)
@@ -106,27 +106,6 @@ class Horde_Imap_Client_Socket extends Horde_Imap_Client_Base
     protected $_stream = null;
 
     /**
-     * Destructor.
-     */
-    public function __destruct()
-    {
-        $this->logout();
-        parent::__destruct();
-    }
-
-    /**
-     * Do cleanup prior to serialization and provide a list of variables
-     * to serialize.
-     */
-    public function __sleep()
-    {
-        $this->logout();
-        $this->_tag = 0;
-        parent::__sleep();
-        return array_diff(array_keys(get_class_vars(__CLASS__)), array('encryptKey'));
-    }
-
-    /**
      * Get CAPABILITY info from the IMAP server.
      *
      * @return array  The capability array.
index cafec7c..4815d09 100644 (file)
@@ -84,7 +84,7 @@ class Horde_Imap_Client_Socket_Pop3 extends Horde_Imap_Client_Base
      *
      * @param array $params  A hash containing configuration parameters.
      */
-    public function __construct($params)
+    public function __construct(array $params = array())
     {
         if (empty($params['port'])) {
             $params['port'] = ($params['secure'] == 'ssl') ? 995 : 110;
@@ -97,14 +97,18 @@ class Horde_Imap_Client_Socket_Pop3 extends Horde_Imap_Client_Base
     }
 
     /**
-     * Do cleanup prior to serialization and provide a list of variables
-     * to serialize.
+     * Unserialize.
+     *
+     * @param string $data  Serialized data.
+     *
+     * @throws Exception
      */
-    public function __sleep()
+    public function unserialize($data)
     {
-        $this->logout();
-        parent::__sleep();
-        return array_diff(array_keys(get_class_vars(__CLASS__)), array('encryptKey'));
+        parent::unserialize($data);
+
+        // Disable caching.
+        $this->_params['cache'] = array('fields' => array());
     }
 
     /**