1: <?php
2: /**
3: * A Horde_Injector:: based IMP_Compose:: 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_Compose:: 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_Compose extends Horde_Core_Factory_Base
29: {
30: /**
31: * Instances.
32: *
33: * @var array
34: */
35: private $_instances = array();
36:
37: /**
38: */
39: public function __construct(Horde_Injector $injector)
40: {
41: parent::__construct($injector);
42:
43: register_shutdown_function(array($this, 'shutdown'));
44: }
45:
46: /**
47: * Return the IMP_Compose:: instance.
48: *
49: * @param string $cacheid The cache ID string.
50: *
51: * @return IMP_Compose The singleton compose instance.
52: * @throws IMP_Exception
53: */
54: public function create($cacheid = null)
55: {
56: if (empty($cacheid)) {
57: $cacheid = strval(new Horde_Support_Randomid());
58: } elseif (!isset($this->_instances[$cacheid])) {
59: $this->_instances[$cacheid] = $GLOBALS['session']->retrieve($cacheid);
60: }
61:
62: if (empty($this->_instances[$cacheid])) {
63: $this->_instances[$cacheid] = new IMP_Compose($cacheid);
64: }
65:
66: return $this->_instances[$cacheid];
67: }
68:
69: /**
70: * Tasks to perform on shutdown.
71: */
72: public function shutdown()
73: {
74: global $session;
75:
76: $cache = $session->get('imp', 'compose_cache', Horde_Session::TYPE_ARRAY);
77: $changed = false;
78:
79: foreach ($this->_instances as $key => $val) {
80: switch ($val->changed) {
81: case 'changed':
82: $session->store($val, false, $key);
83: $cache[$key] = 1;
84: $changed = true;
85: break;
86:
87: case 'deleted':
88: unset($cache[$key]);
89: $session->purge($key);
90: $changed = true;
91: break;
92: }
93:
94: }
95:
96: if ($changed) {
97: $session->set('imp', 'compose_cache', $cache);
98: }
99: }
100:
101: }
102: