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