PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/lithium/libraries/lithium/storage/Session.php

https://github.com/brtriver/sukonv
PHP | 300 lines | 158 code | 30 blank | 112 comment | 29 complexity | d3f1d26fa1bed94d2f46278d3124ac3a MD5 | raw file
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2010, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\storage;
  9. use lithium\core\Libraries;
  10. use lithium\util\Collection;
  11. /**
  12. * The `Session` static class provides a consistent interface to configure and utilize the
  13. * different persistent storage adatpers included with Lithium, as well as your own adapters.
  14. *
  15. * The Session layer of Lithium inherits from the common `Adaptable` class, which provides
  16. * the generic configuration setting & retrieval logic, as well as the logic required to
  17. * locate & instantiate the proper adapter class.
  18. *
  19. * In most cases, you will configure various named session configurations in your bootstrap
  20. * process, which will then be available to you in all other parts of your application.
  21. *
  22. * Each adapter provides a consistent interface for the basic cache operations of `write`, `read`
  23. * and `delete`, which can be used interchangably between all adapters.
  24. *
  25. * For more information on `Session` methods and specific adapters, please see their relevant
  26. * documentation.
  27. *
  28. * @see lithium\core\Adaptable
  29. * @see lithium\storage\session\adapter
  30. */
  31. class Session extends \lithium\core\Adaptable {
  32. /**
  33. * Stores configurations for cache adapters
  34. *
  35. * @var object Collection of cache configurations
  36. */
  37. protected static $_configurations = array();
  38. /**
  39. * Libraries::locate() compatible path to adapters for this class.
  40. *
  41. * @var string Dot-delimited path.
  42. */
  43. protected static $_adapters = 'adapter.storage.session';
  44. /**
  45. * Libraries::locate() compatible path to strategies for this class.
  46. *
  47. * @var string Dot-delimited path.
  48. */
  49. protected static $_strategies = 'strategy.storage.session';
  50. /**
  51. * Returns key used to identify the session.
  52. *
  53. * @param mixed $name Named session configuration.
  54. * @return null|string Value of key, or `null` if no named configuration exists.
  55. */
  56. public static function key($name = null) {
  57. return is_object($adapter = static::adapter($name)) ? $adapter->key() : null;
  58. }
  59. /**
  60. * Indicates whether the the current request includes information on a previously started
  61. * session.
  62. *
  63. * @param string $name Named session configuration.
  64. * @return boolean Returns true if a the request includes a key from a previously created
  65. * session.
  66. */
  67. public static function isStarted($name = null) {
  68. return is_object($adapter = static::adapter($name)) ? $adapter->isStarted() : false;
  69. }
  70. /**
  71. * Checks the validity of a previously-started session by running several checks, including
  72. * comparing the session start time to the expiration time set in the configuration, and any
  73. * security settings.
  74. *
  75. * @todo Implement
  76. * @param string $name Named session configuration.
  77. * @return boolean Returns true if the current session is active and valid.
  78. */
  79. public static function isValid($name = null) {
  80. }
  81. /**
  82. * Reads a value from a persistent session store.
  83. *
  84. * @param string $key Key to be read
  85. * @param array $options Optional parameters that this method accepts.
  86. * @return mixed Read result on successful session read, null otherwise.
  87. * @filter This method may be filtered.
  88. */
  89. public static function read($key = null, array $options = array()) {
  90. $defaults = array('name' => null, 'strategies' => true);
  91. $options += $defaults;
  92. $method = ($name = $options['name']) ? static::adapter($name)->read($key, $options) : null;
  93. $settings = static::_config($name);
  94. if (!$method) {
  95. foreach (array_keys(static::$_configurations) as $name) {
  96. if ($method = static::adapter($name)->read($key, $options)) {
  97. break;
  98. }
  99. }
  100. if (!$method || !$name) {
  101. return null;
  102. }
  103. }
  104. $filters = $settings['filters'] ?: array();
  105. $result = static::_filter(__FUNCTION__, compact('key', 'options'), $method, $filters);
  106. if ($options['strategies']) {
  107. $options += array('key' => $key, 'mode' => 'LIFO', 'class' => __CLASS__);
  108. return static::applyStrategies(__FUNCTION__, $name, $result, $options);
  109. }
  110. return $result;
  111. }
  112. /**
  113. * Writes a persistent value to one or more session stores.
  114. *
  115. * @param string $key Key to be read
  116. * @param mixed $value Data to be stored
  117. * @param array $options Optional parameters that this method accepts.
  118. * @return boolean True on successful write, false otherwise.
  119. * @filter This method may be filtered.
  120. */
  121. public static function write($key, $value = null, array $options = array()) {
  122. $defaults = array('name' => null, 'strategies' => true);
  123. $options += $defaults;
  124. if (is_resource($value) || !static::$_configurations) {
  125. return false;
  126. }
  127. $methods = array();
  128. if ($name = $options['name']) {
  129. $methods = array($name => static::adapter($name)->write($key, $value, $options));
  130. } else {
  131. foreach (array_keys(static::$_configurations) as $name) {
  132. if ($method = static::adapter($name)->write($key, $value, $options)) {
  133. $methods[$name] = $method;
  134. }
  135. }
  136. }
  137. $result = false;
  138. $settings = static::_config($name);
  139. if ($options['strategies']) {
  140. $options += array('key' => $key, 'class' => __CLASS__);
  141. $value = static::applyStrategies(__FUNCTION__, $name, $value, $options);
  142. }
  143. $params = compact('key', 'value', 'options');
  144. foreach ($methods as $name => $method) {
  145. $filters = $settings['filters'];
  146. $result = $result || static::_filter(__FUNCTION__, $params, $method, $filters);
  147. }
  148. return $result;
  149. }
  150. /**
  151. * Deletes a named key from a single adapter (if a `'name'` option is specified) or all
  152. * session adapters.
  153. *
  154. * @param string $key The name of the session key to delete.
  155. * @param array $options Optional parameters that this method accepts.
  156. * @return void
  157. * @filter This method may be filtered.
  158. */
  159. public static function delete($key, array $options = array()) {
  160. $defaults = array('name' => null, 'strategies' => true);
  161. $options += $defaults;
  162. $methods = array();
  163. if ($name = $options['name']) {
  164. $methods = array($name => static::adapter($name)->delete($key, $options));
  165. } else {
  166. foreach (static::$_configurations as $name => $config) {
  167. if ($method = static::adapter($name)->delete($key, $options)) {
  168. $methods[$name] = $method;
  169. }
  170. }
  171. }
  172. $result = false;
  173. $options += array('key' => $key, 'class' => __CLASS__);
  174. if ($options['strategies']) {
  175. $options += array('key' => $key, 'class' => __CLASS__);
  176. $key = static::applyStrategies(__FUNCTION__, $name, $key, $options);
  177. }
  178. $params = compact('key', 'options');
  179. foreach ($methods as $name => $method) {
  180. $settings = static::_config($name);
  181. $filters = $settings['filters'];
  182. $result = $result || static::_filter(__FUNCTION__, $params, $method, $filters);
  183. }
  184. return $result;
  185. }
  186. /**
  187. * Clears all keys from a single adapter (if a `'name'` options is specified) or all
  188. * session adapters.
  189. *
  190. * @param array $options Optional parameters that this method accepts.
  191. */
  192. public static function clear(array $options = array()) {
  193. $defaults = array('name' => null, 'strategies' => true);
  194. $options += $defaults;
  195. $methods = array();
  196. if ($name = $options['name']) {
  197. $methods = array($name => static::adapter($name)->clear($options));
  198. } else {
  199. foreach (static::$_configurations as $name => $config) {
  200. if ($method = static::adapter($name)->clear($options)) {
  201. $methods[$name] = $method;
  202. }
  203. }
  204. }
  205. $params = compact('options');
  206. $result = false;
  207. foreach ($methods as $name => $method) {
  208. $settings = static::_config($name);
  209. $filters = $settings['filters'];
  210. $result = $result || static::_filter(__FUNCTION__, $params, $method, $filters);
  211. }
  212. if ($options['strategies']) {
  213. $options += array('mode' => 'LIFO', 'class' => __CLASS__);
  214. return static::applyStrategies(__FUNCTION__, $name, $result, $options);
  215. }
  216. return $result;
  217. }
  218. /**
  219. * Checks if a session key is set in any adapter, or if a particular adapter configuration is
  220. * specified (via `'name'` in `$options`), only that configuration is checked.
  221. *
  222. * @param string $key The session key to check.
  223. * @param array $options Optional parameters that this method accepts.
  224. * @return boolean
  225. * @filter This method may be filtered.
  226. */
  227. public static function check($key, array $options = array()) {
  228. $defaults = array('name' => null, 'strategies' => true);
  229. $options += $defaults;
  230. $methods = array();
  231. if ($name = $options['name']) {
  232. $methods = array($name => static::adapter($name)->check($key, $options));
  233. } else {
  234. foreach (static::$_configurations as $name => $config) {
  235. if ($method = static::adapter($name)->check($key, $options)) {
  236. $methods[$name] = $method;
  237. }
  238. }
  239. }
  240. $params = compact('key', 'options');
  241. $result = false;
  242. foreach ($methods as $name => $method) {
  243. $settings = static::_config($name);
  244. $filters = $settings['filters'];
  245. $result = $result || static::_filter(__FUNCTION__, $params, $method, $filters);
  246. }
  247. if ($options['strategies']) {
  248. $options += array('key' => $key, 'mode' => 'LIFO', 'class' => __CLASS__);
  249. return static::applyStrategies(__FUNCTION__, $name, $result, $options);
  250. }
  251. return $result;
  252. }
  253. /**
  254. * Returns the adapter object instance of the named configuration.
  255. *
  256. * @param null|string $name Named configuration. If not set, the last configured
  257. * adapter object instance will be returned.
  258. * @return object Adapter instance.
  259. */
  260. public static function adapter($name = null) {
  261. if (empty($name)) {
  262. if (!$names = array_keys(static::$_configurations)) {
  263. return;
  264. }
  265. $name = end($names);
  266. }
  267. return parent::adapter($name);
  268. }
  269. }
  270. ?>