From: Michael M Slusarz Date: Sat, 4 Sep 2010 08:03:57 +0000 (-0600) Subject: Have Horde_Imap_Client_Base implement Serializable X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=bb1c6786b756869bc25361ec7ca792a7e12863a8;p=horde.git Have Horde_Imap_Client_Base implement Serializable 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. --- diff --git a/framework/Imap_Client/lib/Horde/Imap/Client/Base.php b/framework/Imap_Client/lib/Horde/Imap/Client/Base.php index cc191674a..6c0ea6450 100644 --- a/framework/Imap_Client/lib/Horde/Imap/Client/Base.php +++ b/framework/Imap_Client/lib/Horde/Imap/Client/Base.php @@ -15,8 +15,11 @@ * @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(); } /** diff --git a/framework/Imap_Client/lib/Horde/Imap/Client/Cclient.php b/framework/Imap_Client/lib/Horde/Imap/Client/Cclient.php index 200e30ca4..7eafe5d27 100644 --- a/framework/Imap_Client/lib/Horde/Imap/Client/Cclient.php +++ b/framework/Imap_Client/lib/Horde/Imap/Client/Cclient.php @@ -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); } /** diff --git a/framework/Imap_Client/lib/Horde/Imap/Client/Cclient/Pop3.php b/framework/Imap_Client/lib/Horde/Imap/Client/Cclient/Pop3.php index 8e33ca19c..daff1cffe 100644 --- a/framework/Imap_Client/lib/Horde/Imap/Client/Cclient/Pop3.php +++ b/framework/Imap_Client/lib/Horde/Imap/Client/Cclient/Pop3.php @@ -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 diff --git a/framework/Imap_Client/lib/Horde/Imap/Client/Socket.php b/framework/Imap_Client/lib/Horde/Imap/Client/Socket.php index dfaa72f17..262c93c73 100644 --- a/framework/Imap_Client/lib/Horde/Imap/Client/Socket.php +++ b/framework/Imap_Client/lib/Horde/Imap/Client/Socket.php @@ -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. diff --git a/framework/Imap_Client/lib/Horde/Imap/Client/Socket/Pop3.php b/framework/Imap_Client/lib/Horde/Imap/Client/Socket/Pop3.php index cafec7cea..4815d0984 100644 --- a/framework/Imap_Client/lib/Horde/Imap/Client/Socket/Pop3.php +++ b/framework/Imap_Client/lib/Horde/Imap/Client/Socket/Pop3.php @@ -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()); } /**