payload.php 3.9 KB

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