db.php 1.2 KB

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