db.php 1.2 KB

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