db.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php namespace System\Session;
  2. use System\Config;
  3. class DB implements Driver, Sweeper {
  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(
  16. 'id' => $session->id,
  17. 'last_activity' => $session->last_activity,
  18. 'data' => unserialize($session->data)
  19. );
  20. }
  21. }
  22. /**
  23. * Save a session.
  24. *
  25. * @param array $session
  26. * @return void
  27. */
  28. public function save($session)
  29. {
  30. $this->delete($session['id']);
  31. $this->table()->insert(array(
  32. 'id' => $session['id'],
  33. 'last_activity' => $session['last_activity'],
  34. 'data' => serialize($session['data'])
  35. ));
  36. }
  37. /**
  38. * Delete a session by ID.
  39. *
  40. * @param string $id
  41. * @return void
  42. */
  43. public function delete($id)
  44. {
  45. $this->table()->delete($id);
  46. }
  47. /**
  48. * Delete all expired sessions.
  49. *
  50. * @param int $expiration
  51. * @return void
  52. */
  53. public function sweep($expiration)
  54. {
  55. $this->table()->where('last_activity', '<', $expiration)->delete();
  56. }
  57. /**
  58. * Get a session database query.
  59. *
  60. * @return Query
  61. */
  62. private function table()
  63. {
  64. return \System\DB::connection()->table(Config::get('session.table'));
  65. }
  66. }