manager.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php namespace Laravel\CLI\Tasks\Session;
  2. use Laravel\IoC;
  3. use Laravel\File;
  4. use Laravel\Config;
  5. use Laravel\Session;
  6. use Laravel\CLI\Tasks\Task;
  7. use Laravel\Database\Schema;
  8. use Laravel\Session\Drivers\Sweeper;
  9. use Laravel\CLI\Tasks\Migrate\Migrator;
  10. class Manager extends Task {
  11. /**
  12. * Generate the session table on the database.
  13. *
  14. * @param array $arguments
  15. * @return void
  16. */
  17. public function table($arguments = array())
  18. {
  19. $migrator = IoC::resolve('task: migrate');
  20. // To create the session table, we will actually create a database
  21. // migration and then run it. This allows the application to stay
  22. // portable through migrations while still having a session table
  23. // generated on the database.
  24. $migration = $migrator->make(array('create_session_table'));
  25. $stub = SYS_PATH.'cli/tasks/session/migration'.EXT;
  26. File::put($migration, File::get($stub));
  27. // By default no session driver is specified in the configuration.
  28. // Since the developer is requesting that the session table be
  29. // created on the database, we'll set the driver to database
  30. // to save an extra step for the developer.
  31. $config = File::get(APP_PATH.'config/session'.EXT);
  32. $config = str_replace(
  33. "'driver' => '',",
  34. "'driver' => 'database',",
  35. $config
  36. );
  37. File::put(APP_PATH.'config/session'.EXT, $config);
  38. echo PHP_EOL;
  39. $migrator->run();
  40. }
  41. /**
  42. * Sweep the expired sessions from storage.
  43. *
  44. * @param array $arguments
  45. * @return void
  46. */
  47. public function sweep($arguments = array())
  48. {
  49. $driver = Session::factory(Config::get('session.driver'));
  50. // If the driver implements the "Sweeper" interface, we know that
  51. // it can sweep expired sessions from storage. Not all drivers
  52. // need be sweepers, as stores like Memcached and APC will
  53. // perform their own garbage collection.
  54. if ($driver instanceof Sweeper)
  55. {
  56. $lifetime = Config::get('session.lifetime');
  57. $driver->sweep(time() - ($lifetime * 60));
  58. }
  59. echo "The session table has been swept!";
  60. }
  61. }