driver.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. * Create a fresh session array with a unique ID.
  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. // If the driver is an instance of the Cookie driver, we are able to
  52. // just return any string since the Cookie driver has no real idea
  53. // of a server side persisted session with an ID.
  54. if ($this instanceof Cookie)
  55. {
  56. return Str::random(40);
  57. }
  58. // We'll continue generating random IDs until we find an ID that is
  59. // not currently assigned to a session. This is almost definitely
  60. // going to happen on the first iteration.
  61. do {
  62. $session = $this->load($id = Str::random(40));
  63. } while ( ! is_null($session));
  64. return $id;
  65. }
  66. }