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

/library/Zend/Service/DeveloperGarden/SecurityTokenServer/Cache.php

http://github.com/zendframework/zf2
PHP | 205 lines | 82 code | 18 blank | 105 comment | 7 complexity | 4ebb3721c0220bf3641c4eafd3893a69 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. */
  21. /**
  22. * @uses Zend_Service_DeveloperGarden_Exception
  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. throw new Zend_Service_DeveloperGarden_Exception(
  73. 'tokenID ' . $tokenId . ' unknown.'
  74. );
  75. }
  76. if (self::hasCache() && self::$_storedToken[$tokenId] === null) {
  77. $cache = self::getCache();
  78. $token = $cache->load(md5($tokenId));
  79. if ($token !== false) {
  80. self::$_storedToken[$tokenId] = $token;
  81. }
  82. }
  83. return self::$_storedToken[$tokenId];
  84. }
  85. /**
  86. * set new value for the given tokenId
  87. *
  88. * @param string $tokenId
  89. * @throws Zend_Service_DeveloperGarden_Exception
  90. * @param Zend_Service_DeveloperGarden_Response_SecurityTokenServer_Interface $tokenValue
  91. * @return void
  92. */
  93. public static function setTokenToCache($tokenId,
  94. Zend_Service_DeveloperGarden_Response_SecurityTokenServer_Interface $tokenValue
  95. ) {
  96. if (!array_key_exists($tokenId, self::$_storedToken)) {
  97. throw new Zend_Service_DeveloperGarden_Exception(
  98. 'tokenID ' . $tokenId . ' unknown.'
  99. );
  100. }
  101. if (self::hasCache()) {
  102. $cache = self::getCache();
  103. $cache->save($tokenValue, md5($tokenId));
  104. }
  105. self::$_storedToken[$tokenId] = $tokenValue;
  106. }
  107. /**
  108. * reset the internal cache structure
  109. *
  110. * @return void
  111. */
  112. public static function resetTokenCache()
  113. {
  114. foreach (self::$_storedToken as $key => $value) {
  115. $value = null;
  116. self::$_storedToken[$key] = $value;
  117. }
  118. }
  119. /**
  120. * Returns the cache
  121. *
  122. * @return Zend_Cache_Core
  123. */
  124. public static function getCache()
  125. {
  126. return self::$_cache;
  127. }
  128. /**
  129. * Set a cache for token
  130. *
  131. * @param Zend_Cache_Core $cache A cache frontend
  132. */
  133. public static function setCache(Zend_Cache_Core $cache)
  134. {
  135. self::$_cache = $cache;
  136. }
  137. /**
  138. * Returns true when a cache is set
  139. *
  140. * @return boolean
  141. */
  142. public static function hasCache()
  143. {
  144. return (self::$_cache !== null);
  145. }
  146. /**
  147. * Removes any cache
  148. *
  149. * @return void
  150. */
  151. public static function removeCache()
  152. {
  153. self::$_cache = null;
  154. }
  155. /**
  156. * Clears all cache data
  157. *
  158. * @return void
  159. */
  160. public static function clearCache()
  161. {
  162. $cache = self::getCache();
  163. if (method_exists($cache, 'clean')) {
  164. $cache->clean();
  165. }
  166. self::$_wsdlCache = null;
  167. }
  168. /**
  169. * Returns the wsdl cache
  170. *
  171. * @return integer
  172. */
  173. public static function getWsdlCache()
  174. {
  175. return self::$_wsdlCache;
  176. }
  177. /**
  178. * Set a cache for wsdl file
  179. *
  180. * @param integer $cache
  181. * @return void
  182. */
  183. public static function setWsdlCache($cache = null)
  184. {
  185. self::$_wsdlCache = $cache;
  186. }
  187. }