/storage/Session.php

https://github.com/k2052/arthur · PHP · 196 lines · 164 code · 32 blank · 0 comment · 29 complexity · c8f84ed5ca9556d055fffd2022d4a63c MD5 · raw file

  1. <?php
  2. namespace arthur\storage;
  3. use arthur\core\Libraries;
  4. class Session extends \arthur\core\Adaptable
  5. {
  6. protected static $_configurations = array();
  7. protected static $_adapters = 'adapter.storage.session';
  8. protected static $_strategies = 'strategy.storage.session';
  9. public static function key($name = null)
  10. {
  11. return is_object($adapter = static::adapter($name)) ? $adapter->key() : null;
  12. }
  13. public static function isStarted($name = null)
  14. {
  15. return is_object($adapter = static::adapter($name)) ? $adapter->isStarted() : false;
  16. }
  17. public static function read($key = null, array $options = array())
  18. {
  19. $defaults = array('name' => null, 'strategies' => true);
  20. $options += $defaults;
  21. $method = ($name = $options['name']) ? static::adapter($name)->read($key, $options) : null;
  22. $settings = static::_config($name);
  23. if(!$method)
  24. {
  25. foreach(array_keys(static::$_configurations) as $name) {
  26. if($method = static::adapter($name)->read($key, $options))
  27. break;
  28. }
  29. if(!$method || !$name) return null;
  30. }
  31. $filters = $settings['filters'] ?: array();
  32. $result = static::_filter(__FUNCTION__, compact('key', 'options'), $method, $filters);
  33. if($options['strategies'])
  34. {
  35. $options += array('key' => $key, 'mode' => 'LIFO', 'class' => __CLASS__);
  36. return static::applyStrategies(__FUNCTION__, $name, $result, $options);
  37. }
  38. return $result;
  39. }
  40. public static function write($key, $value = null, array $options = array())
  41. {
  42. $defaults = array('name' => null, 'strategies' => true);
  43. $options += $defaults;
  44. if(is_resource($value) || !static::$_configurations)
  45. return false;
  46. $methods = array();
  47. if($name = $options['name'])
  48. $methods = array($name => static::adapter($name)->write($key, $value, $options));
  49. else
  50. {
  51. foreach(array_keys(static::$_configurations) as $name) {
  52. if($method = static::adapter($name)->write($key, $value, $options))
  53. $methods[$name] = $method;
  54. }
  55. }
  56. $result = false;
  57. $settings = static::_config($name);
  58. if($options['strategies']) {
  59. $options += array('key' => $key, 'class' => __CLASS__);
  60. $value = static::applyStrategies(__FUNCTION__, $name, $value, $options);
  61. }
  62. $params = compact('key', 'value', 'options');
  63. foreach($methods as $name => $method) {
  64. $filters = $settings['filters'];
  65. $result = static::_filter(__FUNCTION__, $params, $method, $filters) || $result;
  66. }
  67. return $result;
  68. }
  69. public static function delete($key, array $options = array())
  70. {
  71. $defaults = array('name' => null, 'strategies' => true);
  72. $options += $defaults;
  73. $methods = array();
  74. if($name = $options['name'])
  75. $methods = array($name => static::adapter($name)->delete($key, $options));
  76. else
  77. {
  78. foreach(static::$_configurations as $name => $config) {
  79. if($method = static::adapter($name)->delete($key, $options))
  80. $methods[$name] = $method;
  81. }
  82. }
  83. $result = false;
  84. $options += array('key' => $key, 'class' => __CLASS__);
  85. if($options['strategies']) {
  86. $options += array('key' => $key, 'class' => __CLASS__);
  87. $key = static::applyStrategies(__FUNCTION__, $name, $key, $options);
  88. }
  89. $params = compact('key', 'options');
  90. foreach($methods as $name => $method)
  91. {
  92. $settings = static::_config($name);
  93. $filters = $settings['filters'];
  94. $result = static::_filter(__FUNCTION__, $params, $method, $filters) || $result;
  95. }
  96. return $result;
  97. }
  98. public static function clear(array $options = array())
  99. {
  100. $defaults = array('name' => null, 'strategies' => true);
  101. $options += $defaults;
  102. $methods = array();
  103. if($name = $options['name'])
  104. $methods = array($name => static::adapter($name)->clear($options));
  105. else
  106. {
  107. foreach(static::$_configurations as $name => $config) {
  108. if($method = static::adapter($name)->clear($options))
  109. $methods[$name] = $method;
  110. }
  111. }
  112. $params = compact('options');
  113. $result = false;
  114. foreach($methods as $name => $method)
  115. {
  116. $settings = static::_config($name);
  117. $filters = $settings['filters'];
  118. $result = static::_filter(__FUNCTION__, $params, $method, $filters) || $result;
  119. }
  120. if($options['strategies']) {
  121. $options += array('mode' => 'LIFO', 'class' => __CLASS__);
  122. return static::applyStrategies(__FUNCTION__, $name, $result, $options);
  123. }
  124. return $result;
  125. }
  126. public static function check($key, array $options = array())
  127. {
  128. $defaults = array('name' => null, 'strategies' => true);
  129. $options += $defaults;
  130. $methods = array();
  131. if($name = $options['name'])
  132. $methods = array($name => static::adapter($name)->check($key, $options));
  133. else
  134. {
  135. foreach(static::$_configurations as $name => $config) {
  136. if($method = static::adapter($name)->check($key, $options))
  137. $methods[$name] = $method;
  138. }
  139. }
  140. $params = compact('key', 'options');
  141. $result = false;
  142. foreach($methods as $name => $method)
  143. {
  144. $settings = static::_config($name);
  145. $filters = $settings['filters'];
  146. $result = static::_filter(__FUNCTION__, $params, $method, $filters) || $result;
  147. }
  148. if($options['strategies']) {
  149. $options += array('key' => $key, 'mode' => 'LIFO', 'class' => __CLASS__);
  150. return static::applyStrategies(__FUNCTION__, $name, $result, $options);
  151. }
  152. return $result;
  153. }
  154. public static function adapter($name = null)
  155. {
  156. if(!$name)
  157. {
  158. if(!$names = array_keys(static::$_configurations))
  159. return;
  160. $name = end($names);
  161. }
  162. return parent::adapter($name);
  163. }
  164. }