1: <?php
2: /**
3: * A Horde_Injector:: based IMP_Mailbox:: 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:: 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_Mailbox extends Horde_Core_Factory_Base
29: {
30: const STORAGE_KEY = 'mbox/';
31:
32: /**
33: * Instances.
34: *
35: * @var array
36: */
37: private $_instances = array();
38:
39: /**
40: * Return the IMP_Mailbox:: instance.
41: *
42: * @param string $mbox The IMAP mailbox name.
43: *
44: * @return IMP_Mailbox The singleton mailbox instance.
45: * @throws IMP_Exception
46: */
47: public function create($mbox)
48: {
49: if ($mbox instanceof IMP_Mailbox) {
50: return $mbox;
51: }
52:
53: if (!isset($this->_instances[$mbox])) {
54: if (empty($this->_instances)) {
55: register_shutdown_function(array($this, 'shutdown'));
56: }
57:
58: $ob = new IMP_Mailbox($mbox);
59: $ob->cache = $GLOBALS['session']->get('imp', self::STORAGE_KEY . $mbox, Horde_Session::TYPE_ARRAY);
60:
61: $this->_instances[$mbox] = $ob;
62: }
63:
64: return $this->_instances[$mbox];
65: }
66:
67: /**
68: * Saves IMP_Mailbox instances to the session.
69: *
70: * A bit hackish - it would theoretically be cleaner code if we just
71: * stored a serialized version of the object in the value. However, this
72: * incurs the overhead of 1) having to define the classname in each
73: * serialized string, and 2) the mailbox name will be duplicated inside
74: * of the object (since it is the same as the key). Since a user may
75: * have 100's of mailboxes, we need to pack info as tightly as possible
76: * in the session; thus, the slightly unorthodox way we store the
77: * mailbox data in the session.
78: */
79: public function shutdown()
80: {
81: foreach ($this->_instances as $ob) {
82: switch ($ob->changed) {
83: case IMP_Mailbox::CHANGED_YES:
84: $GLOBALS['session']->set('imp', self::STORAGE_KEY . $ob, $ob->cache);
85: break;
86:
87: case IMP_Mailbox::CHANGED_DELETE:
88: $GLOBALS['session']->remove('imp', self::STORAGE_KEY . $ob);
89: break;
90: }
91: }
92: }
93:
94: }
95: