file.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. * @param bool $exists
  38. * @return void
  39. */
  40. public function save($session, $config, $exists)
  41. {
  42. F::put($this->path.$session['id'], serialize($session), LOCK_EX);
  43. }
  44. /**
  45. * Delete a session from storage by a given ID.
  46. *
  47. * @param string $id
  48. * @return void
  49. */
  50. public function delete($id)
  51. {
  52. F::delete($this->path.$id);
  53. }
  54. /**
  55. * Delete all expired sessions from persistant storage.
  56. *
  57. * @param int $expiration
  58. * @return void
  59. */
  60. public function sweep($expiration)
  61. {
  62. foreach (glob($this->path.'*') as $file)
  63. {
  64. if (F::type($file) == 'file' and F::modified($file) < $expiration)
  65. {
  66. F::delete($file);
  67. }
  68. }
  69. }
  70. }