manager.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. $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 the framework's migrations system.
  28. $migration = $migrator->make(array('create_session_table'));
  29. $stub = path('sys').'cli/tasks/session/migration'.EXT;
  30. File::put($migration, File::get($stub));
  31. // By default no session driver is specified in the configuration.
  32. // Since the developer is requesting that the session table be
  33. // created on the database, we'll set the driver.
  34. $config = File::get(path('app').'config/session'.EXT);
  35. $config = str_replace(
  36. "'driver' => '',",
  37. "'driver' => 'database',",
  38. $config
  39. );
  40. File::put(path('app').'config/session'.EXT, $config);
  41. echo PHP_EOL;
  42. $migrator->run();
  43. }
  44. /**
  45. * Sweep the expired sessions from storage.
  46. *
  47. * @param array $arguments
  48. * @return void
  49. */
  50. public function sweep($arguments = array())
  51. {
  52. $driver = Session::factory(Config::get('session.driver'));
  53. // If the driver implements the "Sweeper" interface, we know that
  54. // it can sweep expired sessions from storage. Not all drivers
  55. // need be sweepers since they do their own.
  56. if ($driver instanceof Sweeper)
  57. {
  58. $lifetime = Config::get('session.lifetime');
  59. $driver->sweep(time() - ($lifetime * 60));
  60. }
  61. echo "The session table has been swept!";
  62. }
  63. }