* @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
*
*/
protected $_debug = null;
+
+ /**
+ * The list of variables to serialize.
+ *
+ * @var array
+ */
+ protected $_store = array('_init', '_params');
+
/**
* Temp array (destroyed at end of process).
*
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.');
$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();
}
/**
*
* @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;
}
/**
- * 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());
}
/**