1: <?php
2: /**
3: * Copyright 2014 Horde LLC (http://www.horde.org/)
4: *
5: * See the enclosed file COPYING for license information (GPL). If you
6: * did not receive this file, see http://www.horde.org/licenses/gpl.
7: *
8: * @category Horde
9: * @copyright 2014 Horde LLC
10: * @license http://www.horde.org/licenses/gpl GPL
11: * @package IMP
12: */
13:
14: /**
15: * A serializable wrapper for the IMAP cache backend. Ensures that IMAP object
16: * uses global Horde object for caching.
17: *
18: * @author Michael Slusarz <slusarz@horde.org>
19: * @category Horde
20: * @copyright 2014 Horde LLC
21: * @license http://www.horde.org/licenses/gpl GPL
22: * @package IMP
23: */
24: class IMP_Imap_Cache_Wrapper implements Serializable
25: {
26: /**
27: * Cache object.
28: *
29: * @var Horde_Imap_Client_Cache_Backend
30: */
31: public $backend;
32:
33: /**
34: * Cache parameters:
35: * - driver (string)
36: * - lifetime (integer)
37: *
38: * @var array
39: */
40: protected $_params = array();
41:
42: /**
43: * Cache lifetime.
44: */
45:
46: /**
47: * Constructor.
48: *
49: * @param string $driver Cache driver to use.
50: * @param integer $lifetime Cache lifetime.
51: */
52: public function __construct($driver, $lifetime = null)
53: {
54: $params = array('driver' => $driver);
55: if (!is_null($lifetime)) {
56: $params['lifetime'] = intval($lifetime);
57: }
58:
59: $this->_initOb($params);
60: }
61:
62: /**
63: */
64: protected function _initOb($params)
65: {
66: global $injector;
67:
68: $this->_params = $params;
69:
70: switch ($this->_params['driver']) {
71: case 'cache':
72: $ob = new Horde_Imap_Client_Cache_Backend_Cache(array_filter(array(
73: 'cacheob' => $injector->getInstance('Horde_Cache'),
74: 'lifetime' => (isset($this->_params['lifetime']) ? $this->_params['lifetime'] : null)
75: )));
76: break;
77:
78: case 'hashtable':
79: $ob = new Horde_Imap_Client_Cache_Backend_Hashtable(array_filter(array(
80: 'hashtable' => $injector->getInstance('Horde_HashTable'),
81: 'lifetime' => (isset($this->_params['lifetime']) ? $this->_params['lifetime'] : null)
82: )));
83: break;
84:
85: case 'none':
86: $ob = new Horde_Imap_Client_Cache_Backend_Null();
87: break;
88:
89: case 'nosql':
90: $ob = new Horde_Imap_Client_Cache_Backend_Mongo(array(
91: 'mongo_db' => $injector->getInstance('Horde_Nosql_Adapter')
92: ));
93: break;
94:
95: case 'sql':
96: $ob = new Horde_Imap_Client_Cache_Backend_Db(array(
97: 'db' => $injector->getInstance('Horde_Db_Adapter')
98: ));
99: break;
100:
101: default:
102: $this->_params['driver'] = 'none';
103: Horde::log(
104: 'IMAP caching has been disabled for this session due to an error',
105: 'WARN'
106: );
107: $ob = new Horde_Imap_Client_Cache_Backend_Null();
108: break;
109: }
110:
111: $this->backend = $ob;
112: }
113:
114: /**
115: * Redirects calls to the logger object.
116: */
117: public function __call($name, $arguments)
118: {
119: return call_user_func_array(array($this->backend, $name), $arguments);
120: }
121:
122: /* Serializable methods. */
123:
124: /**
125: */
126: public function serialize()
127: {
128: return json_encode($this->_params);
129: }
130:
131: /**
132: */
133: public function unserialize($data)
134: {
135: $this->_initOb(json_decode($data, true));
136: }
137:
138: }
139: