manager.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 install($arguments = array())
  18. {
  19. $migrator = IoC::resolve('task: migrate');
  20. $key = IoC::resolve('task: key');
  21. // Since sessions can't work without an application key, we will go
  22. // ahead and set the key if one has not already been set for the
  23. // application so the developer doesn't need to set it.
  24. $key->generate();
  25. // To create the session table, we will actually create a database
  26. // migration and then run it. This allows the application to stay
  27. // portable through migrations while still having a session table
  28. // generated on the database.
  29. $migration = $migrator->make(array('create_session_table'));
  30. $stub = $GLOBALS['SYS_PATH'].'cli/tasks/session/migration'.EXT;
  31. File::put($migration, File::get($stub));
  32. // By default no session driver is specified in the configuration.
  33. // Since the developer is requesting that the session table be
  34. // created on the database, we'll set the driver to database
  35. // to save an extra step for the developer.
  36. $config = File::get($GLOBALS['APP_PATH'].'config/session'.EXT);
  37. $config = str_replace(
  38. "'driver' => '',",
  39. "'driver' => 'database',",
  40. $config
  41. );
  42. File::put($GLOBALS['APP_PATH'].'config/session'.EXT, $config);
  43. echo PHP_EOL;
  44. $migrator->run();
  45. }
  46. /**
  47. * Sweep the expired sessions from storage.
  48. *
  49. * @param array $arguments
  50. * @return void
  51. */
  52. public function sweep($arguments = array())
  53. {
  54. $driver = Session::factory(Config::get('session.driver'));
  55. // If the driver implements the "Sweeper" interface, we know that
  56. // it can sweep expired sessions from storage. Not all drivers
  57. // need be sweepers, as stores like Memcached and APC will
  58. // perform their own garbage collection.
  59. if ($driver instanceof Sweeper)
  60. {
  61. $lifetime = Config::get('session.lifetime');
  62. $driver->sweep(time() - ($lifetime * 60));
  63. }
  64. echo "The session table has been swept!";
  65. }
  66. }