1: <?php
2: /**
3: * Copyright 2010-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 2010-2014 Horde LLC
10: * @license http://www.horde.org/licenses/gpl GPL
11: * @package IMP
12: */
13:
14: /**
15: * A Horde_Injector based IMP_Mailbox_List factory.
16: *
17: * @author Michael Slusarz <slusarz@horde.org>
18: * @category Horde
19: * @copyright 2010-2014 Horde LLC
20: * @license http://www.horde.org/licenses/gpl GPL
21: * @package IMP
22: */
23: class IMP_Factory_MailboxList
24: extends Horde_Core_Factory_Base
25: implements Horde_Shutdown_Task
26: {
27: /* Storage key for list data. */
28: const STORAGE_KEY = 'mboxlist';
29:
30: /**
31: * Cache instances.
32: *
33: * @var array
34: */
35: private $_cache = array();
36:
37: /**
38: * Instances.
39: *
40: * @var array
41: */
42: private $_instances = array();
43:
44: /**
45: * Return the mailbox list instance.
46: *
47: * @param string $mailbox The mailbox name.
48: *
49: * @return IMP_Mailbox_List The singleton instance.
50: * @throws IMP_Exception
51: */
52: public function create($mailbox)
53: {
54: $key = strval($mailbox);
55:
56: if (!isset($this->_instances[$key])) {
57: $mailbox = IMP_Mailbox::get($mailbox);
58:
59: if ($ob = $this->_getCache($mailbox)->get($key)) {
60: $ob = @unserialize($ob);
61: }
62:
63: if (!$ob) {
64: if ($mailbox->search) {
65: $ob = new IMP_Mailbox_List_Virtual($mailbox);
66: } else {
67: $ob = $mailbox->is_imap
68: ? new IMP_Mailbox_List($mailbox)
69: : new IMP_Mailbox_List_Pop3($mailbox);
70: }
71: }
72:
73: $this->_instances[$key] = $ob;
74: }
75:
76: return $this->_instances[$key];
77: }
78:
79: /**
80: * Tasks to perform on shutdown.
81: */
82: public function shutdown()
83: {
84: foreach ($this->_instances as $key => $val) {
85: if ($val->changed) {
86: $this->_getCache(IMP_Mailbox::get($key))->set($key, serialize($val));
87: }
88: }
89: }
90:
91: /**
92: * Expires cached entries.
93: */
94: public function expireAll()
95: {
96: foreach ($this->_cache as $val) {
97: $val->clear();
98: }
99: $this->_instances = array();
100: }
101:
102: /**
103: * Return the proper cache object based on the mailbox type.
104: *
105: * @param IMP_Mailbox $mbox Mailbox object.
106: *
107: * @return Horde_Core_Cache_Session Session cache object.
108: */
109: protected function _getCache(IMP_Mailbox $mbox)
110: {
111: global $injector;
112:
113: $key = intval($mbox->search || !$mbox->is_imap);
114:
115: if (!isset($this->_cache[$key])) {
116: if (empty($this->_cache)) {
117: Horde_Shutdown::add($this);
118: }
119:
120: /* Need to treat search/POP3 mailboxes differently than IMAP
121: * mailboxes since BUIDs may NOT be the same if the mailbox is
122: * regenerated on a cache miss (they are unique generated within a
123: * session on-demand). */
124: if ($key) {
125: $cache = new Horde_Cache(
126: new Horde_Cache_Storage_Hashtable(array(
127: 'hashtable' => new Horde_Core_HashTable_PersistentSession()
128: )),
129: array(
130: 'compress' => true,
131: 'logger' => $injector->getInstance('Horde_Core_Log_Wrapper')
132: )
133: );
134: } else {
135: $cache = $injector->getInstance('Horde_Cache');
136: }
137:
138: $this->_cache[$key] = new Horde_Core_Cache_Session(array(
139: 'app' => 'imp',
140: 'cache' => $cache,
141: 'storage_key' => self::STORAGE_KEY
142: ));
143: }
144:
145: return $this->_cache[$key];
146: }
147:
148: }
149: