db.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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->query()->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->query()->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->query()->where('id', '=', $id)->delete();
  37. }
  38. /**
  39. * Delete all expired sessions.
  40. *
  41. * @param int $expiration
  42. * @return void
  43. */
  44. public function sweep($expiration)
  45. {
  46. $this->query()->where('last_activity', '<', $expiration)->delete();
  47. }
  48. /**
  49. * Get a session database query.
  50. *
  51. * @return Query
  52. */
  53. private function query()
  54. {
  55. return \System\DB::table(\System\Config::get('session.table'));
  56. }
  57. }