file.php 1.5 KB

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