Store.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php namespace Illuminate\Session;
  2. use Symfony\Component\HttpFoundation\Session\Session as SymfonySession;
  3. class Store extends SymfonySession {
  4. /**
  5. * {@inheritdoc}
  6. */
  7. public function start()
  8. {
  9. parent::start();
  10. if ( ! $this->has('_token')) $this->put('_token', str_random(40));
  11. }
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function save()
  16. {
  17. $this->ageFlashData();
  18. return parent::save();
  19. }
  20. /**
  21. * Age the flash data for the session.
  22. *
  23. * @return void
  24. */
  25. protected function ageFlashData()
  26. {
  27. foreach ($this->get('flash.old', array()) as $old) { $this->forget($old); }
  28. $this->put('flash.old', $this->get('flash.new', array()));
  29. $this->put('flash.new', array());
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function has($name)
  35. {
  36. return ! is_null($this->get($name));
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function get($name, $default = null)
  42. {
  43. return array_get($this->all(), $name, $default);
  44. }
  45. /**
  46. * Determine if the session contains old input.
  47. *
  48. * @param string $key
  49. * @return bool
  50. */
  51. public function hasOldInput($key = null)
  52. {
  53. return ! is_null($this->getOldInput($key));
  54. }
  55. /**
  56. * Get the requested item from the flashed input array.
  57. *
  58. * @param string $key
  59. * @param mixed $default
  60. * @return mixed
  61. */
  62. public function getOldInput($key = null, $default = null)
  63. {
  64. $input = $this->get('_old_input', array());
  65. // Input that is flashed to the session can be easily retrieved by the
  66. // developer, making repopulating old forms and the like much more
  67. // convenient, since the request's previous input is available.
  68. if (is_null($key)) return $input;
  69. return array_get($input, $key, $default);
  70. }
  71. /**
  72. * Get the CSRF token value.
  73. *
  74. * @return string
  75. */
  76. public function getToken()
  77. {
  78. return $this->token();
  79. }
  80. /**
  81. * Get the CSRF token value.
  82. *
  83. * @return string
  84. */
  85. public function token()
  86. {
  87. return $this->get('_token');
  88. }
  89. /**
  90. * Put a key / value pair in the session.
  91. *
  92. * @param string $key
  93. * @param mixed $value
  94. * @return void
  95. */
  96. public function put($key, $value)
  97. {
  98. $all = $this->all();
  99. array_set($all, $key, $value);
  100. $this->replace($all);
  101. }
  102. /**
  103. * Push a value onto a session array.
  104. *
  105. * @param string $key
  106. * @param mixed $value
  107. * @return void
  108. */
  109. public function push($key, $value)
  110. {
  111. $array = $this->get($key, array());
  112. $array[] = $value;
  113. $this->put($key, $array);
  114. }
  115. /**
  116. * Flash a key / value pair to the session.
  117. *
  118. * @param string $key
  119. * @param mixed $value
  120. * @return void
  121. */
  122. public function flash($key, $value)
  123. {
  124. $this->put($key, $value);
  125. $this->push('flash.new', $key);
  126. $this->removeFromOldFlashData(array($key));
  127. }
  128. /**
  129. * Flash an input array to the session.
  130. *
  131. * @param array $value
  132. * @return void
  133. */
  134. public function flashInput(array $value)
  135. {
  136. return $this->flash('_old_input', $value);
  137. }
  138. /**
  139. * Reflash all of the session flash data.
  140. *
  141. * @return void
  142. */
  143. public function reflash()
  144. {
  145. $this->mergeNewFlashes($this->get('flash.old'));
  146. $this->put('flash.old', array());
  147. }
  148. /**
  149. * Reflash a subset of the current flash data.
  150. *
  151. * @param array|dynamic $keys
  152. * @return void
  153. */
  154. public function keep($keys = null)
  155. {
  156. $keys = is_array($keys) ? $keys : func_get_args();
  157. $this->mergeNewFlashes($keys);
  158. $this->removeFromOldFlashData($keys);
  159. }
  160. /**
  161. * Merge new flash keys into the new flash array.
  162. *
  163. * @param array $keys
  164. * @return void
  165. */
  166. protected function mergeNewFlashes(array $keys)
  167. {
  168. $values = array_unique(array_merge($this->get('flash.new'), $keys));
  169. $this->put('flash.new', $values);
  170. }
  171. /**
  172. * Remove the given keys from the old flash data.
  173. *
  174. * @param array $keys
  175. * @return void
  176. */
  177. protected function removeFromOldFlashData(array $keys)
  178. {
  179. $this->put('flash.old', array_diff($this->get('flash.old', array()), $keys));
  180. }
  181. /**
  182. * Remove an item from the session.
  183. *
  184. * @param string $key
  185. * @return void
  186. */
  187. public function forget($key)
  188. {
  189. $all = $this->all();
  190. array_forget($all, $key);
  191. $this->replace($all);
  192. }
  193. /**
  194. * Remove all of the items from the session.
  195. *
  196. * @return void
  197. */
  198. public function flush()
  199. {
  200. return $this->clear();
  201. }
  202. /**
  203. * Generate a new session identifier.
  204. *
  205. * @return string
  206. */
  207. public function regenerate()
  208. {
  209. return $this->migrate();
  210. }
  211. }