db.php 1.3 KB

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