1: <?php
2: /**
3: * A Horde_Injector based factory for the IMP_Imap_Tree object.
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 factory for the IMP_Imap_Tree object.
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_Imaptree extends Horde_Core_Factory_Injector
29: {
30: /**
31: * Indicates that the tree object is being initialized.
32: *
33: * @var boolean
34: */
35: static private $_isInit = false;
36:
37: /**
38: * Return the IMP_Imap_Tree object.
39: *
40: * @return IMP_Imap_Tree The singleton instance.
41: */
42: public function create(Horde_Injector $injector)
43: {
44: global $session;
45:
46: $instance = null;
47:
48: /* If an IMP_Imap_Tree object is currently stored in the cache,
49: * re-create that object. Else, create a new instance. */
50: if ($session->exists('imp', 'treeob')) {
51: /* Since IMAP tree generation is so expensive/time-consuming,
52: * fallback to storing in the session even if no permanent cache
53: * backend is setup. */
54: $cache = $injector->getInstance('Horde_Cache');
55: if ($cache instanceof Horde_Cache_Null) {
56: $instance = $session->retrieve('imp_imaptree');
57: } else {
58: try {
59: $instance = @unserialize($cache->get($session->get('imp', 'treeob'), 86400));
60: } catch (Exception $e) {
61: Horde::logMessage('Could not unserialize stored IMP_Imap_Tree object.', 'DEBUG');
62: }
63: }
64: } else {
65: $session->set('imp', 'treeob', strval(new Horde_Support_Randomid()));
66: }
67:
68: if (!($instance instanceof IMP_Imap_Tree)) {
69: self::$_isInit = true;
70: $instance = new IMP_Imap_Tree();
71: self::$_isInit = false;
72: }
73:
74: register_shutdown_function(array($this, 'shutdown'), $instance, $injector);
75:
76: return $instance;
77: }
78:
79: /**
80: * Store serialized version of object in the current session.
81: *
82: * @param IMP_Imap_Tree $instance Tree object.
83: * @param Horde_Injector $injector Injector object.
84: */
85: public function shutdown($instance, $injector)
86: {
87: global $session;
88:
89: /* Only need to store the object if the tree has changed. */
90: if ($instance->changed) {
91: $cache = $injector->getInstance('Horde_Cache');
92: if ($cache instanceof Horde_Cache_Null) {
93: $session->store($instance, true, 'imp_imaptree');
94: } else {
95: $cache->set($GLOBALS['session']->get('imp', 'treeob'), serialize($instance), 86400);
96: }
97: }
98: }
99:
100: /**
101: * Static method: determines if IMP_Imap_Tree has already been initialized
102: * in this session.
103: *
104: * @return boolean True if the tree object has been initialized.
105: */
106: static public function initialized()
107: {
108: return (!self::$_isInit &&
109: $GLOBALS['session']->exists('imp', 'treeob'));
110: }
111:
112: }
113: