FailedTable.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace think\queue\command;
  3. use think\console\Command;
  4. use think\helper\Str;
  5. use think\migration\Creator;
  6. class FailedTable extends Command
  7. {
  8. protected function configure()
  9. {
  10. $this->setName('queue:failed-table')
  11. ->setDescription('Create a migration for the failed queue jobs database table');
  12. }
  13. public function handle()
  14. {
  15. if (!$this->app->has('migration.creator')) {
  16. $this->output->error('Install think-migration first please');
  17. return;
  18. }
  19. $table = $this->app->config->get('queue.failed.table');
  20. $className = Str::studly("create_{$table}_table");
  21. /** @var Creator $creator */
  22. $creator = $this->app->get('migration.creator');
  23. $path = $creator->create($className);
  24. // Load the alternative template if it is defined.
  25. $contents = file_get_contents(__DIR__ . '/stubs/failed_jobs.stub');
  26. // inject the class names appropriate to this migration
  27. $contents = strtr($contents, [
  28. 'CreateFailedJobsTable' => $className,
  29. '{{table}}' => $table,
  30. ]);
  31. file_put_contents($path, $contents);
  32. $this->output->info('Migration created successfully!');
  33. }
  34. }