file.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php namespace Laravel\Session\Drivers;
  2. class File implements Driver, 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 persistant storage.
  61. *
  62. * @param int $expiration
  63. * @return void
  64. */
  65. public function sweep($expiration)
  66. {
  67. foreach (glob($this->path.'*') as $file)
  68. {
  69. if (filetype($file) == 'file' and filemtime($file) < $expiration)
  70. {
  71. @unlink($file);
  72. }
  73. }
  74. }
  75. }