/storage/Session.php

https://github.com/mackstar/lithium · PHP · 306 lines · 155 code · 29 blank · 122 comment · 29 complexity · a895586477369ef686af46ad60aa421b MD5 · raw file

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