file.php 1.6 KB

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