1: <?php
2: /**
3: * Copyright 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 2014 Horde LLC
10: * @license http://www.horde.org/licenses/gpl GPL
11: * @package IMP
12: */
13:
14: /**
15: * A Horde_Injector based factory for the IMP_Contacts object.
16: *
17: * @author Michael Slusarz <slusarz@horde.org>
18: * @category Horde
19: * @copyright 2014 Horde LLC
20: * @license http://www.horde.org/licenses/gpl GPL
21: * @package IMP
22: */
23: class IMP_Factory_Contacts
24: extends Horde_Core_Factory_Injector
25: implements Horde_Shutdown_Task
26: {
27: const SESS_KEY = 'contacts';
28:
29: /**
30: * @var IMP_Contacts
31: */
32: private $_instance;
33:
34: /**
35: * Return the IMP_Contacts instance.
36: *
37: * @return IMP_Contacts The singleton instance.
38: */
39: public function create(Horde_Injector $injector)
40: {
41: try {
42: $this->_instance = $GLOBALS['session']->get('imp', self::SESS_KEY);
43: } catch (Exception $e) {
44: Horde::log('Could not unserialize stored IMP_Contacts object.', 'DEBUG');
45: }
46:
47: if (is_null($this->_instance)) {
48: $this->_instance = new IMP_Contacts();
49: }
50:
51: Horde_Shutdown::add($this);
52:
53: return $this->_instance;
54: }
55:
56: /**
57: * Store serialized version of object in the current session.
58: */
59: public function shutdown()
60: {
61: /* Only need to store the object if the object has changed. */
62: if ($this->_instance->changed) {
63: $GLOBALS['session']->set('imp', self::SESS_KEY, $this->_instance);
64: }
65: }
66:
67: }
68: