SeedCommand.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Illuminate\Database\Console\Seeds;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Console\ConfirmableTrait;
  5. use Illuminate\Database\ConnectionResolverInterface as Resolver;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Symfony\Component\Console\Attribute\AsCommand;
  8. use Symfony\Component\Console\Input\InputArgument;
  9. use Symfony\Component\Console\Input\InputOption;
  10. #[AsCommand(name: 'db:seed')]
  11. class SeedCommand extends Command
  12. {
  13. use ConfirmableTrait;
  14. /**
  15. * The console command name.
  16. *
  17. * @var string
  18. */
  19. protected $name = 'db:seed';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = 'Seed the database with records';
  26. /**
  27. * The connection resolver instance.
  28. *
  29. * @var \Illuminate\Database\ConnectionResolverInterface
  30. */
  31. protected $resolver;
  32. /**
  33. * Create a new database seed command instance.
  34. *
  35. * @param \Illuminate\Database\ConnectionResolverInterface $resolver
  36. * @return void
  37. */
  38. public function __construct(Resolver $resolver)
  39. {
  40. parent::__construct();
  41. $this->resolver = $resolver;
  42. }
  43. /**
  44. * Execute the console command.
  45. *
  46. * @return int
  47. */
  48. public function handle()
  49. {
  50. if (! $this->confirmToProceed()) {
  51. return 1;
  52. }
  53. $this->components->info('Seeding database.');
  54. $previousConnection = $this->resolver->getDefaultConnection();
  55. $this->resolver->setDefaultConnection($this->getDatabase());
  56. Model::unguarded(function () {
  57. $this->getSeeder()->__invoke();
  58. });
  59. if ($previousConnection) {
  60. $this->resolver->setDefaultConnection($previousConnection);
  61. }
  62. return 0;
  63. }
  64. /**
  65. * Get a seeder instance from the container.
  66. *
  67. * @return \Illuminate\Database\Seeder
  68. */
  69. protected function getSeeder()
  70. {
  71. $class = $this->input->getArgument('class') ?? $this->input->getOption('class');
  72. if (! str_contains($class, '\\')) {
  73. $class = 'Database\\Seeders\\'.$class;
  74. }
  75. if ($class === 'Database\\Seeders\\DatabaseSeeder' &&
  76. ! class_exists($class)) {
  77. $class = 'DatabaseSeeder';
  78. }
  79. return $this->laravel->make($class)
  80. ->setContainer($this->laravel)
  81. ->setCommand($this);
  82. }
  83. /**
  84. * Get the name of the database connection to use.
  85. *
  86. * @return string
  87. */
  88. protected function getDatabase()
  89. {
  90. $database = $this->input->getOption('database');
  91. return $database ?: $this->laravel['config']['database.default'];
  92. }
  93. /**
  94. * Get the console command arguments.
  95. *
  96. * @return array
  97. */
  98. protected function getArguments()
  99. {
  100. return [
  101. ['class', InputArgument::OPTIONAL, 'The class name of the root seeder', null],
  102. ];
  103. }
  104. /**
  105. * Get the console command options.
  106. *
  107. * @return array
  108. */
  109. protected function getOptions()
  110. {
  111. return [
  112. ['class', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder', 'Database\\Seeders\\DatabaseSeeder'],
  113. ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to seed'],
  114. ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production'],
  115. ];
  116. }
  117. }