session.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php namespace Laravel\CLI\Tasks\Session;
  2. use Laravel\File;
  3. use Laravel\Config;
  4. use Laravel\Database\Schema;
  5. use Laravel\Session\Drivers\Sweeper;
  6. class Manager extends Task {
  7. /**
  8. * Generate the session table on the database.
  9. *
  10. * @param array $arguments
  11. * @return void
  12. */
  13. public function table($arguments = array())
  14. {
  15. Schema::table(Config::get('session.table'), function($table)
  16. {
  17. $table->create();
  18. // The session table consists simply of an ID, a UNIX timestamp to
  19. // indicate the expiration time, and a blob field which will hold
  20. // the serialized form of the session payload.
  21. $table->string('id')->length(40)->primary('session_primary');
  22. $table->integer('last_activity');
  23. $table->text('data');
  24. });
  25. // By default no session driver is specified in the configuration.
  26. // Since the developer is requesting that the session table be
  27. // created on the database, we'll set the driver to database
  28. // to save an extra step for the developer.
  29. $config = File::get(APP_PATH.'config/session'.EXT);
  30. $config = str_replace("'driver' => '',", "'driver' => 'database',", $config);
  31. File::put(APP_PATH.'config/session'.EXT, $config);
  32. echo "The table has been created! Database set as session driver.";
  33. }
  34. /**
  35. * Sweep the expired sessions from storage.
  36. *
  37. * @param array $arguments
  38. * @return void
  39. */
  40. public function sweep($arguments = array())
  41. {
  42. $driver = \Laravel\Session::factory(Config::get('session.driver'));
  43. // If the driver implements the "Sweeper" interface, we know that
  44. // it can sweep expired sessions from storage. Not all drivers
  45. // need be sweepers, as stores like Memcached and APC will
  46. // perform their own garbage collection.
  47. if ($driver instanceof Sweeper)
  48. {
  49. $lifetime = Config::get('session.lifetime');
  50. $driver->sweep(time() - ($lifetime * 60));
  51. }
  52. echo "The session table has been swept!";
  53. }
  54. }