file.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php namespace Laravel\Session\Drivers;
  2. use Laravel\File as F;
  3. class File implements Driver, Sweeper {
  4. /**
  5. * The path to which the session files should be written.
  6. *
  7. * @var string
  8. */
  9. private $path;
  10. /**
  11. * Create a new File session driver instance.
  12. *
  13. * @param string $path
  14. * @return void
  15. */
  16. public function __construct($path)
  17. {
  18. $this->path = $path;
  19. }
  20. /**
  21. * Load a session from storage by a given ID.
  22. *
  23. * If no session is found for the ID, null will be returned.
  24. *
  25. * @param string $id
  26. * @return array
  27. */
  28. public function load($id)
  29. {
  30. if (F::exists($path = $this->path.$id)) return unserialize(F::get($path));
  31. }
  32. /**
  33. * Save a given session to storage.
  34. *
  35. * @param array $session
  36. * @param array $config
  37. * @return void
  38. */
  39. public function save($session, $config)
  40. {
  41. F::put($this->path.$session['id'], serialize($session), LOCK_EX);
  42. }
  43. /**
  44. * Delete a session from storage by a given ID.
  45. *
  46. * @param string $id
  47. * @return void
  48. */
  49. public function delete($id)
  50. {
  51. F::delete($this->path.$id);
  52. }
  53. /**
  54. * Delete all expired sessions from persistant storage.
  55. *
  56. * @param int $expiration
  57. * @return void
  58. */
  59. public function sweep($expiration)
  60. {
  61. foreach (glob($this->path.'*') as $file)
  62. {
  63. if (F::type($file) == 'file' and F::modified($file) < $expiration)
  64. {
  65. F::delete($file);
  66. }
  67. }
  68. }
  69. }