PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Service/DeveloperGarden/SecurityTokenServer/Cache.php

https://bitbucket.org/bigstylee/zend-framework
PHP | 207 lines | 84 code | 18 blank | 105 comment | 7 complexity | 8fbf22fb5d90bcb971f69ecdf475c760 MD5 | raw file
  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-2012 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 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Service
  25. * @subpackage DeveloperGarden
  26. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @author Marco Kaiser
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_Service_DeveloperGarden_SecurityTokenServer_Cache
  31. {
  32. /**
  33. * array with stored tokens
  34. *
  35. * @var array
  36. */
  37. protected static $_storedToken = array(
  38. 'securityToken' => null,
  39. 'getTokens' => null
  40. );
  41. /**
  42. * Internal cache for token values
  43. *
  44. * @var Zend_Cache_Core
  45. * @access private
  46. */
  47. private static $_cache = null;
  48. /**
  49. * PHP SOAP wsdl cache constant
  50. *
  51. * @var integer
  52. */
  53. private static $_wsdlCache = null;
  54. // @codeCoverageIgnoreStart
  55. /**
  56. * Constructor overriding - make sure that a developer cannot instantiate
  57. */
  58. protected function __construct()
  59. {
  60. }
  61. // @codeCoverageIgnoreEnd
  62. /**
  63. * returns stored token from cache or null
  64. *
  65. * @param string $tokenId
  66. * @throws Zend_Service_DeveloperGarden_Exception
  67. * @return Zend_Service_DeveloperGarden_Response_SecurityTokenServer_Interface|null
  68. */
  69. public static function getTokenFromCache($tokenId)
  70. {
  71. if (!array_key_exists($tokenId, self::$_storedToken)) {
  72. require_once 'Zend/Service/DeveloperGarden/Exception.php';
  73. throw new Zend_Service_DeveloperGarden_Exception(
  74. 'tokenID ' . $tokenId . ' unknown.'
  75. );
  76. }
  77. if (self::hasCache() && self::$_storedToken[$tokenId] === null) {
  78. $cache = self::getCache();
  79. $token = $cache->load(md5($tokenId));
  80. if ($token !== false) {
  81. self::$_storedToken[$tokenId] = $token;
  82. }
  83. }
  84. return self::$_storedToken[$tokenId];
  85. }
  86. /**
  87. * set new value for the given tokenId
  88. *
  89. * @param string $tokenId
  90. * @throws Zend_Service_DeveloperGarden_Exception
  91. * @param Zend_Service_DeveloperGarden_Response_SecurityTokenServer_Interface $tokenValue
  92. * @return void
  93. */
  94. public static function setTokenToCache($tokenId,
  95. Zend_Service_DeveloperGarden_Response_SecurityTokenServer_Interface $tokenValue
  96. ) {
  97. if (!array_key_exists($tokenId, self::$_storedToken)) {
  98. require_once 'Zend/Service/DeveloperGarden/Exception.php';
  99. throw new Zend_Service_DeveloperGarden_Exception(
  100. 'tokenID ' . $tokenId . ' unknown.'
  101. );
  102. }
  103. if (self::hasCache()) {
  104. $cache = self::getCache();
  105. $cache->save($tokenValue, md5($tokenId));
  106. }
  107. self::$_storedToken[$tokenId] = $tokenValue;
  108. }
  109. /**
  110. * reset the internal cache structure
  111. *
  112. * @return void
  113. */
  114. public static function resetTokenCache()
  115. {
  116. foreach (self::$_storedToken as $key => $value) {
  117. $value = null;
  118. self::$_storedToken[$key] = $value;
  119. }
  120. }
  121. /**
  122. * Returns the cache
  123. *
  124. * @return Zend_Cache_Core
  125. */
  126. public static function getCache()
  127. {
  128. return self::$_cache;
  129. }
  130. /**
  131. * Set a cache for token
  132. *
  133. * @param Zend_Cache_Core $cache A cache frontend
  134. */
  135. public static function setCache(Zend_Cache_Core $cache)
  136. {
  137. self::$_cache = $cache;
  138. }
  139. /**
  140. * Returns true when a cache is set
  141. *
  142. * @return boolean
  143. */
  144. public static function hasCache()
  145. {
  146. return (self::$_cache !== null);
  147. }
  148. /**
  149. * Removes any cache
  150. *
  151. * @return void
  152. */
  153. public static function removeCache()
  154. {
  155. self::$_cache = null;
  156. }
  157. /**
  158. * Clears all cache data
  159. *
  160. * @return void
  161. */
  162. public static function clearCache()
  163. {
  164. $cache = self::getCache();
  165. if (method_exists($cache, 'clean')) {
  166. $cache->clean();
  167. }
  168. self::$_wsdlCache = null;
  169. }
  170. /**
  171. * Returns the wsdl cache
  172. *
  173. * @return integer
  174. */
  175. public static function getWsdlCache()
  176. {
  177. return self::$_wsdlCache;
  178. }
  179. /**
  180. * Set a cache for wsdl file
  181. *
  182. * @param integer $cache
  183. * @return void
  184. */
  185. public static function setWsdlCache($cache = null)
  186. {
  187. self::$_wsdlCache = $cache;
  188. }
  189. }