driver.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php namespace Laravel\Session\Drivers; use Laravel\Config, Laravel\Str;
  2. abstract class Driver {
  3. /**
  4. * Load a session from storage by a given ID.
  5. *
  6. * If no session is found for the ID, null will be returned.
  7. *
  8. * @param string $id
  9. * @return array
  10. */
  11. abstract public function load($id);
  12. /**
  13. * Save a given session to storage.
  14. *
  15. * @param array $session
  16. * @param array $config
  17. * @param bool $exists
  18. * @return void
  19. */
  20. abstract public function save($session, $config, $exists);
  21. /**
  22. * Delete a session from storage by a given ID.
  23. *
  24. * @param string $id
  25. * @return void
  26. */
  27. abstract public function delete($id);
  28. /**
  29. * Insert a fresh session and return the payload array.
  30. *
  31. * @return array
  32. */
  33. public function fresh()
  34. {
  35. // We will simply generate an empty session payload array, using an ID
  36. // that is not currently assigned to any existing session within the
  37. // application and return it to the driver.
  38. return array('id' => $this->id(), 'data' => array(
  39. ':new:' => array(),
  40. ':old:' => array(),
  41. ));
  42. }
  43. /**
  44. * Get a new session ID that isn't assigned to any current session.
  45. *
  46. * @return string
  47. */
  48. public function id()
  49. {
  50. $session = array();
  51. // We'll containue generating random IDs until we find an ID that is
  52. // not currently assigned to a session. This is almost definitely
  53. // going to happen on the first iteration.
  54. do {
  55. $session = $this->load($id = Str::random(40));
  56. } while ( ! is_null($session));
  57. return $id;
  58. }
  59. }