payload.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php namespace Laravel\Session;
  2. use Closure;
  3. use Laravel\Str;
  4. class Payload {
  5. /**
  6. * The raw session payload array.
  7. *
  8. * @var array
  9. */
  10. public $session = array();
  11. /**
  12. * Indicates if the session ID has been regenerated.
  13. *
  14. * @var bool
  15. */
  16. public $regenerated = false;
  17. /**
  18. * Create a new session container instance.
  19. *
  20. * @param array $session
  21. * @return void
  22. */
  23. public function __construct($session)
  24. {
  25. $this->session = $session;
  26. }
  27. /**
  28. * Determine if the session or flash data contains an item.
  29. *
  30. * @param string $key
  31. * @return bool
  32. */
  33. public function has($key)
  34. {
  35. return ( ! is_null($this->get($key)));
  36. }
  37. /**
  38. * Get an item from the session.
  39. *
  40. * A default value may also be specified, and will be returned in the item doesn't exist.
  41. *
  42. * <code>
  43. * // Get an item from the session
  44. * $name = Session::get('name');
  45. *
  46. * // Return a default value if the item doesn't exist
  47. * $name = Session::get('name', 'Taylor');
  48. * </code>
  49. *
  50. * @param string $key
  51. * @param mixed $default
  52. * @return mixed
  53. */
  54. public function get($key, $default = null)
  55. {
  56. foreach (array($key, ':old:'.$key, ':new:'.$key) as $possibility)
  57. {
  58. if (array_key_exists($possibility, $this->session['data']))
  59. {
  60. return $this->session['data'][$possibility];
  61. }
  62. }
  63. return ($default instanceof Closure) ? call_user_func($default) : $default;
  64. }
  65. /**
  66. * Write an item to the session.
  67. *
  68. * <code>
  69. * // Write an item to the session
  70. * Session::put('name', 'Taylor');
  71. * </code>
  72. *
  73. * @param string $key
  74. * @param mixed $value
  75. * @return Driver
  76. */
  77. public function put($key, $value)
  78. {
  79. $this->session['data'][$key] = $value;
  80. return $this;
  81. }
  82. /**
  83. * Write an item to the session flash data.
  84. *
  85. * Flash data only exists for the next request. After that, it will be removed from
  86. * the session. Flash data is useful for temporary status or welcome messages.
  87. *
  88. * <code>
  89. * // Flash an item to the session
  90. * Session::flash('name', 'Taylor');
  91. * </code>
  92. *
  93. * @param string $key
  94. * @param mixed $value
  95. * @return Driver
  96. */
  97. public function flash($key, $value)
  98. {
  99. $this->put(':new:'.$key, $value);
  100. return $this;
  101. }
  102. /**
  103. * Keep all of the session flash data from expiring at the end of the request.
  104. *
  105. * @return void
  106. */
  107. public function reflash()
  108. {
  109. $this->readdress(':old:', ':new:', array_keys($this->session['data']));
  110. }
  111. /**
  112. * Keep a session flash item from expiring at the end of the request.
  113. *
  114. * If a string is passed to the method, only that item will be kept. An array may also
  115. * be passed to the method, in which case all items in the array will be kept.
  116. *
  117. * <code>
  118. * // Keep a session flash item from expiring
  119. * Session::keep('name');
  120. * </code>
  121. *
  122. * @param string|array $key
  123. * @return void
  124. */
  125. public function keep($key)
  126. {
  127. if (is_array($key)) return array_map(array($this, 'keep'), $key);
  128. $this->flash($key, $this->get($key));
  129. $this->forget(':old:'.$key);
  130. }
  131. /**
  132. * Remove an item from the session.
  133. *
  134. * @param string $key
  135. * @return Driver
  136. */
  137. public function forget($key)
  138. {
  139. unset($this->session['data'][$key]);
  140. }
  141. /**
  142. * Remove all items from the session.
  143. *
  144. * @return void
  145. */
  146. public function flush()
  147. {
  148. $this->session['data'] = array();
  149. }
  150. /**
  151. * Regenerate the session ID.
  152. *
  153. * @return void
  154. */
  155. public function regenerate()
  156. {
  157. $this->session['id'] = Str::random(40);
  158. $this->regenerated = true;
  159. }
  160. /**
  161. * Age the session payload, preparing it for storage after a request.
  162. *
  163. * The session flash data will be aged and the last activity timestamp will be updated.
  164. * The aged session array will be returned by the method.
  165. *
  166. * @return array
  167. */
  168. public function age()
  169. {
  170. $this->session['last_activity'] = time();
  171. // To age the data, we will forget all of the old keys and then rewrite the newly
  172. // flashed items to have old keys, which will be available for the next request.
  173. foreach ($this->session['data'] as $key => $value)
  174. {
  175. if (strpos($key, ':old:') === 0) $this->forget($key);
  176. }
  177. $this->readdress(':new:', ':old:', array_keys($this->session['data']));
  178. return $this->session;
  179. }
  180. /**
  181. * Readdress the session data by performing a string replacement on the keys.
  182. *
  183. * @param string $search
  184. * @param string $replace
  185. * @param array $keys
  186. * @return void
  187. */
  188. private function readdress($search, $replace, $keys)
  189. {
  190. $this->session['data'] = array_combine(str_replace($search, $replace, $keys), array_values($this->session['data']));
  191. }
  192. }