TableCommand.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Illuminate\Queue\Console;
  3. use Illuminate\Support\Str;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Composer;
  6. use Illuminate\Filesystem\Filesystem;
  7. class TableCommand extends Command
  8. {
  9. /**
  10. * The console command name.
  11. *
  12. * @var string
  13. */
  14. protected $name = 'queue:table';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Create a migration for the queue jobs database table';
  21. /**
  22. * The filesystem instance.
  23. *
  24. * @var \Illuminate\Filesystem\Filesystem
  25. */
  26. protected $files;
  27. /**
  28. * @var \Illuminate\Support\Composer
  29. */
  30. protected $composer;
  31. /**
  32. * Create a new queue job table command instance.
  33. *
  34. * @param \Illuminate\Filesystem\Filesystem $files
  35. * @param \Illuminate\Support\Composer $composer
  36. * @return void
  37. */
  38. public function __construct(Filesystem $files, Composer $composer)
  39. {
  40. parent::__construct();
  41. $this->files = $files;
  42. $this->composer = $composer;
  43. }
  44. /**
  45. * Execute the console command.
  46. *
  47. * @return void
  48. */
  49. public function handle()
  50. {
  51. $table = $this->laravel['config']['queue.connections.database.table'];
  52. $this->replaceMigration(
  53. $this->createBaseMigration($table), $table, Str::studly($table)
  54. );
  55. $this->info('Migration created successfully!');
  56. $this->composer->dumpAutoloads();
  57. }
  58. /**
  59. * Create a base migration file for the table.
  60. *
  61. * @param string $table
  62. * @return string
  63. */
  64. protected function createBaseMigration($table = 'jobs')
  65. {
  66. return $this->laravel['migration.creator']->create(
  67. 'create_'.$table.'_table', $this->laravel->databasePath().'/migrations'
  68. );
  69. }
  70. /**
  71. * Replace the generated migration with the job table stub.
  72. *
  73. * @param string $path
  74. * @param string $table
  75. * @param string $tableClassName
  76. * @return void
  77. */
  78. protected function replaceMigration($path, $table, $tableClassName)
  79. {
  80. $stub = str_replace(
  81. ['{{table}}', '{{tableClassName}}'],
  82. [$table, $tableClassName],
  83. $this->files->get(__DIR__.'/stubs/jobs.stub')
  84. );
  85. $this->files->put($path, $stub);
  86. }
  87. }