db.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php namespace System\Session;
  2. use System\Config;
  3. use System\DB\Manager;
  4. class DB implements Driver {
  5. /**
  6. * Load a session by ID.
  7. *
  8. * @param string $id
  9. * @return array
  10. */
  11. public function load($id)
  12. {
  13. $session = $this->table()->find($id);
  14. if ( ! is_null($session))
  15. {
  16. return array('id' => $session->id, 'last_activity' => $session->last_activity, 'data' => unserialize($session->data));
  17. }
  18. }
  19. /**
  20. * Save a session.
  21. *
  22. * @param array $session
  23. * @return void
  24. */
  25. public function save($session)
  26. {
  27. $this->delete($session['id']);
  28. $this->table()->insert(array('id' => $session['id'], 'last_activity' => $session['last_activity'], 'data' => serialize($session['data'])));
  29. }
  30. /**
  31. * Delete a session by ID.
  32. *
  33. * @param string $id
  34. * @return void
  35. */
  36. public function delete($id)
  37. {
  38. $this->table()->delete($id);
  39. }
  40. /**
  41. * Delete all expired sessions.
  42. *
  43. * @param int $expiration
  44. * @return void
  45. */
  46. public function sweep($expiration)
  47. {
  48. $this->table()->where('last_activity', '<', $expiration)->delete();
  49. }
  50. /**
  51. * Get a session database query.
  52. *
  53. * @return Query
  54. */
  55. private function table()
  56. {
  57. return Manager::connection()->table(Config::get('session.table'));
  58. }
  59. }