1: <?php
2: /**
3: * A Horde_Injector:: based IMP_Mailbox_List:: factory.
4: *
5: * PHP version 5
6: *
7: * @author Michael Slusarz <slusarz@horde.org>
8: * @category Horde
9: * @license http://www.horde.org/licenses/gpl GPL
10: * @link http://pear.horde.org/index.php?package=IMP
11: * @package IMP
12: */
13:
14: /**
15: * A Horde_Injector:: based IMP_Mailbox_List:: factory.
16: *
17: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (GPL). If you
20: * did not receive this file, see http://www.horde.org/licenses/gpl.
21: *
22: * @author Michael Slusarz <slusarz@horde.org>
23: * @category Horde
24: * @license http://www.horde.org/licenses/gpl GPL
25: * @link http://pear.horde.org/index.php?package=IMP
26: * @package IMP
27: */
28: class IMP_Factory_MailboxList extends Horde_Core_Factory_Base
29: {
30: const STORAGE_KEY = 'mboxlist/';
31:
32: /**
33: * Instances.
34: *
35: * @var array
36: */
37: private $_instances = array();
38:
39: /**
40: */
41: public function __construct(Horde_Injector $injector)
42: {
43: parent::__construct($injector);
44:
45: register_shutdown_function(array($this, 'shutdown'));
46: }
47:
48: /**
49: * Return the mailbox list instance.
50: * For IMP/MIMP, returns an IMP_Mailbox_List_Track object.
51: * For DIMP/Mobile, returns an IMP_Mailbox_List object.
52: *
53: * @param string $mailbox The mailbox name.
54: * @param IMP_Indices $indices An indices object. Only used for 'imp' and
55: * 'mimp' views.
56: *
57: * @return IMP_Mailbox_List The singleton instance.
58: * @throws IMP_Exception
59: */
60: public function create($mailbox, $indices = null)
61: {
62: $mbox_key = strval($mailbox);
63: $mode = IMP::getViewMode();
64:
65: if (!isset($this->_instances[$mbox_key])) {
66: switch ($mode) {
67: case 'dimp':
68: case 'mobile':
69: $ob = new IMP_Mailbox_List($mailbox);
70: break;
71:
72: case 'imp':
73: case 'mimp':
74: try {
75: $ob = $GLOBALS['session']->get('imp', self::STORAGE_KEY . $mailbox);
76: } catch (Exception $e) {
77: $ob = null;
78: }
79:
80: if (is_null($ob) ||
81: !($ob instanceof IMP_Mailbox_List_Track)) {
82: $ob = new IMP_Mailbox_List_Track($mailbox);
83: }
84: break;
85: }
86:
87: $this->_instances[$mbox_key] = $ob;
88: }
89:
90: switch ($mode) {
91: case 'imp':
92: case 'mimp':
93: /* 'checkcache' needs to be set before setIndex(). */
94: $this->_instances[$mbox_key]->checkcache = is_null($indices);
95: $this->_instances[$mbox_key]->setIndex($indices);
96: break;
97: }
98:
99: return $this->_instances[$mbox_key];
100: }
101:
102: /**
103: * Tasks to perform on shutdown.
104: */
105: public function shutdown()
106: {
107: switch (IMP::getViewMode()) {
108: case 'imp':
109: case 'mimp':
110: /* Cache mailbox information if viewing in standard (IMP) message
111: * mode. Needed to keep navigation consistent when moving through
112: * the message list, and to ensure messages aren't marked as
113: * missing in search mailboxes (e.g. if search is dependent on
114: * unseen flag). */
115: foreach ($this->_instances as $key => $val) {
116: if ($val->changed) {
117: $GLOBALS['session']->set('imp', self::STORAGE_KEY . $key, $val);
118: }
119: }
120: }
121: }
122:
123: /**
124: * Expires cached entries.
125: */
126: public function expireAll()
127: {
128: $GLOBALS['session']->remove('imp', self::STORAGE_KEY);
129: $this->_instances = array();
130: }
131:
132: }
133: