1: <?php
2: /**
3: * A Horde_Injector:: based Horde_Themes_Cache:: factory.
4: *
5: * PHP version 5
6: *
7: * @category Horde
8: * @package Core
9: * @author Michael Slusarz <slusarz@horde.org>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
11: * @link http://pear.horde.org/index.php?package=Core
12: */
13:
14: /**
15: * A Horde_Injector:: based Horde_Themes_Cache:: factory.
16: *
17: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you
20: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
21: *
22: * @category Horde
23: * @package Core
24: * @author Michael Slusarz <slusarz@horde.org>
25: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
26: * @link http://pear.horde.org/index.php?package=Core
27: */
28: class Horde_Core_Factory_ThemesCache extends Horde_Core_Factory_Base
29: {
30: /**
31: * Instances.
32: *
33: * @var array
34: */
35: private $_instances = array();
36:
37: /**
38: * Return the Horde_Themes_Cache:: instance.
39: *
40: * @param string $app The application name.
41: * @param string $theme The theme name.
42: *
43: * @return Horde_Themes_Cache The singleton instance.
44: */
45: public function create($app, $theme)
46: {
47: $sig = implode('|', array($app, $theme));
48:
49: if (!isset($this->_instances[$sig])) {
50: if (!empty($GLOBALS['conf']['cachethemes'])) {
51: $cache = $this->_injector->getInstance('Horde_Cache');
52: } else {
53: $cache = null;
54: }
55:
56: if (!$cache || ($cache instanceof Horde_Cache_Null)) {
57: $instance = new Horde_Themes_Cache($app, $theme);
58: } else {
59: try {
60: $instance = @unserialize($cache->get($sig, $GLOBALS['conf']['cachethemesparams']['lifetime']));
61: } catch (Exception $e) {
62: $instance = null;
63: }
64:
65: if (!($instance instanceof Horde_Themes_Cache)) {
66: $instance = new Horde_Themes_Cache($app, $theme);
67: $instance->build();
68: }
69:
70: if (empty($this->_instances)) {
71: register_shutdown_function(array($this, 'shutdown'));
72: }
73: }
74:
75: $this->_instances[$sig] = $instance;
76: }
77:
78: return $this->_instances[$sig];
79: }
80:
81: /**
82: * Expire cache entry.
83: *
84: * @param string $app The application name.
85: * @param string $theme The theme name.
86: *
87: * @return boolean True if cache entry existed and was deleted.
88: * @throws Horde_Exception
89: */
90: public function expireCache($app, $theme)
91: {
92: $sig = implode('|', array($app, $theme));
93:
94: $cache = $this->_injector->getInstance('Horde_Cache');
95:
96: if ($cache->exists($sig, $GLOBALS['conf']['cachethemesparams']['lifetime'])) {
97: if (!$cache->expire($sig)) {
98: throw new Horde_Exception('Could not delete cache entry.');
99: }
100:
101: unset($this->_instances[$sig]);
102: return true;
103: }
104:
105: return false;
106: }
107:
108: /**
109: * Store object in cache.
110: */
111: public function shutdown()
112: {
113: $cache = $this->_injector->getInstance('Horde_Cache');
114:
115: foreach ($this->_instances as $key => $val) {
116: if ($val->changed) {
117: $cache->set($key, serialize($val), $GLOBALS['conf']['cachethemesparams']['lifetime']);
118: }
119: }
120: }
121:
122: }
123: