|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Service |
|
17 * @subpackage DeveloperGarden |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: Cache.php 20166 2010-01-09 19:00:17Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @category Zend |
|
25 * @package Zend_Service |
|
26 * @subpackage DeveloperGarden |
|
27 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
28 * @author Marco Kaiser |
|
29 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
30 */ |
|
31 class Zend_Service_DeveloperGarden_SecurityTokenServer_Cache |
|
32 { |
|
33 /** |
|
34 * array with stored tokens |
|
35 * |
|
36 * @var array |
|
37 */ |
|
38 protected static $_storedToken = array( |
|
39 'securityToken' => null, |
|
40 'getTokens' => null |
|
41 ); |
|
42 |
|
43 /** |
|
44 * Internal cache for token values |
|
45 * |
|
46 * @var Zend_Cache_Core |
|
47 * @access private |
|
48 */ |
|
49 private static $_cache = null; |
|
50 |
|
51 /** |
|
52 * PHP SOAP wsdl cache constant |
|
53 * |
|
54 * @var integer |
|
55 */ |
|
56 private static $_wsdlCache = null; |
|
57 |
|
58 // @codeCoverageIgnoreStart |
|
59 /** |
|
60 * Constructor overriding - make sure that a developer cannot instantiate |
|
61 */ |
|
62 protected function __construct() |
|
63 { |
|
64 } |
|
65 // @codeCoverageIgnoreEnd |
|
66 |
|
67 /** |
|
68 * returns stored token from cache or null |
|
69 * |
|
70 * @param string $tokenId |
|
71 * @throws Zend_Service_DeveloperGarden_Exception |
|
72 * @return Zend_Service_DeveloperGarden_Response_SecurityTokenServer_Interface|null |
|
73 */ |
|
74 public static function getTokenFromCache($tokenId) |
|
75 { |
|
76 if (!array_key_exists($tokenId, self::$_storedToken)) { |
|
77 require_once 'Zend/Service/DeveloperGarden/Exception.php'; |
|
78 throw new Zend_Service_DeveloperGarden_Exception( |
|
79 'tokenID ' . $tokenId . ' unknown.' |
|
80 ); |
|
81 } |
|
82 |
|
83 if (self::hasCache() && self::$_storedToken[$tokenId] === null) { |
|
84 $cache = self::getCache(); |
|
85 $token = $cache->load(md5($tokenId)); |
|
86 if ($token !== false) { |
|
87 self::$_storedToken[$tokenId] = $token; |
|
88 } |
|
89 } |
|
90 |
|
91 return self::$_storedToken[$tokenId]; |
|
92 } |
|
93 |
|
94 /** |
|
95 * set new value for the given tokenId |
|
96 * |
|
97 * @param string $tokenId |
|
98 * @throws Zend_Service_DeveloperGarden_Exception |
|
99 * @param Zend_Service_DeveloperGarden_Response_SecurityTokenServer_Interface $tokenValue |
|
100 * @return void |
|
101 */ |
|
102 public static function setTokenToCache($tokenId, |
|
103 Zend_Service_DeveloperGarden_Response_SecurityTokenServer_Interface $tokenValue |
|
104 ) { |
|
105 if (!array_key_exists($tokenId, self::$_storedToken)) { |
|
106 require_once 'Zend/Service/DeveloperGarden/Exception.php'; |
|
107 throw new Zend_Service_DeveloperGarden_Exception( |
|
108 'tokenID ' . $tokenId . ' unknown.' |
|
109 ); |
|
110 } |
|
111 |
|
112 if (self::hasCache()) { |
|
113 $cache = self::getCache(); |
|
114 $cache->save($tokenValue, md5($tokenId)); |
|
115 } |
|
116 |
|
117 self::$_storedToken[$tokenId] = $tokenValue; |
|
118 } |
|
119 |
|
120 /** |
|
121 * reset the internal cache structure |
|
122 * |
|
123 * @return void |
|
124 */ |
|
125 public static function resetTokenCache() |
|
126 { |
|
127 foreach (self::$_storedToken as $key => $value) { |
|
128 $value = null; |
|
129 self::$_storedToken[$key] = $value; |
|
130 } |
|
131 } |
|
132 |
|
133 /** |
|
134 * Returns the cache |
|
135 * |
|
136 * @return Zend_Cache_Core |
|
137 */ |
|
138 public static function getCache() |
|
139 { |
|
140 return self::$_cache; |
|
141 } |
|
142 |
|
143 /** |
|
144 * Set a cache for token |
|
145 * |
|
146 * @param Zend_Cache_Core $cache A cache frontend |
|
147 */ |
|
148 public static function setCache(Zend_Cache_Core $cache) |
|
149 { |
|
150 self::$_cache = $cache; |
|
151 } |
|
152 |
|
153 /** |
|
154 * Returns true when a cache is set |
|
155 * |
|
156 * @return boolean |
|
157 */ |
|
158 public static function hasCache() |
|
159 { |
|
160 return (self::$_cache !== null); |
|
161 } |
|
162 |
|
163 /** |
|
164 * Removes any cache |
|
165 * |
|
166 * @return void |
|
167 */ |
|
168 public static function removeCache() |
|
169 { |
|
170 self::$_cache = null; |
|
171 } |
|
172 |
|
173 /** |
|
174 * Clears all cache data |
|
175 * |
|
176 * @return void |
|
177 */ |
|
178 public static function clearCache() |
|
179 { |
|
180 $cache = self::getCache(); |
|
181 if (method_exists($cache, 'clean')) { |
|
182 $cache->clean(); |
|
183 } |
|
184 self::$_wsdlCache = null; |
|
185 } |
|
186 |
|
187 /** |
|
188 * Returns the wsdl cache |
|
189 * |
|
190 * @return integer |
|
191 */ |
|
192 public static function getWsdlCache() |
|
193 { |
|
194 return self::$_wsdlCache; |
|
195 } |
|
196 |
|
197 /** |
|
198 * Set a cache for wsdl file |
|
199 * |
|
200 * @param integer $cache |
|
201 * @return void |
|
202 */ |
|
203 public static function setWsdlCache($cache = null) |
|
204 { |
|
205 self::$_wsdlCache = $cache; |
|
206 } |
|
207 } |