manager.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\Session\Drivers\Sweeper;
  8. class Manager extends Task {
  9. /**
  10. * Generate the session table on the database.
  11. *
  12. * @param array $arguments
  13. * @return void
  14. */
  15. public function table($arguments = array())
  16. {
  17. $migrator = IoC::resolve('task: migrate');
  18. $key = IoC::resolve('task: key');
  19. // Since sessions can't work without an application key, we will go
  20. // ahead and set the key if one has not already been set for the
  21. // application so the developer doesn't need to set it.
  22. $key->generate();
  23. // To create the session table, we will actually create a database
  24. // migration and then run it. This allows the application to stay
  25. // portable through the framework's migrations system.
  26. $migration = $migrator->make(array('create_session_table'));
  27. $stub = path('sys').'cli/tasks/session/migration'.EXT;
  28. File::put($migration, File::get($stub));
  29. // By default no session driver is set within the configuration.
  30. // Since the developer is requesting that the session table be
  31. // created on the database, we'll set it.
  32. $this->driver('database');
  33. echo PHP_EOL;
  34. $migrator->run();
  35. }
  36. /**
  37. * Sweep the expired sessions from storage.
  38. *
  39. * @param array $arguments
  40. * @return void
  41. */
  42. public function sweep($arguments = array())
  43. {
  44. $driver = Session::factory(Config::get('session.driver'));
  45. // If the driver implements the "Sweeper" interface, we know that it
  46. // can sweep expired sessions from storage. Not all drivers need be
  47. // sweepers since they do their own.
  48. if ($driver instanceof Sweeper)
  49. {
  50. $lifetime = Config::get('session.lifetime');
  51. $driver->sweep(time() - ($lifetime * 60));
  52. }
  53. echo "The session table has been swept!";
  54. }
  55. /**
  56. * Set the session driver to a given value.
  57. *
  58. * @param string $driver
  59. * @return void
  60. */
  61. protected function driver($driver)
  62. {
  63. // By default no session driver is set within the configuration.
  64. // This method will replace the empty driver option with the
  65. // driver specified in the arguments.
  66. $config = File::get(path('app').'config/session'.EXT);
  67. $config = str_replace(
  68. "'driver' => '',",
  69. "'driver' => 'database',",
  70. $config
  71. );
  72. File::put(path('app').'config/session'.EXT, $config);
  73. }
  74. }