1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ikechukwukalu\Dynamicdatabaseconfig\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Ikechukwukalu\Dynamicdatabaseconfig\Trait\DatabaseConfig; |
7
|
|
|
|
8
|
|
|
class EnvDatabaseConfigSeedCommand extends Command |
9
|
|
|
{ |
10
|
|
|
use DatabaseConfig; |
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The name and signature of the console command. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'env:seed |
18
|
|
|
{database : The type of database} |
19
|
|
|
{name : The name of the new database connection} |
20
|
|
|
{postfix : The postfix for the database configuration} |
21
|
|
|
{--seed : Running seeders} |
22
|
|
|
{--seeder= : Running a single seeder class}'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The console command description. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $description = 'Seeding into databases from env'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Execute the console command. |
33
|
|
|
*/ |
34
|
|
|
public function handle(): void |
35
|
|
|
{ |
36
|
|
|
$database = $this->argument('database'); |
37
|
|
|
$name = $this->argument('name'); |
38
|
|
|
$postFix = $this->argument('postfix'); |
39
|
|
|
|
40
|
|
|
$this->components->info("Running seeder(s) for {$name}"); |
|
|
|
|
41
|
|
|
|
42
|
|
|
$newConfig = $this->setNewEnvConfig($database, $postFix); |
|
|
|
|
43
|
|
|
$this->addNewConfig($database, $name, $newConfig); |
|
|
|
|
44
|
|
|
$this->createDatabase($database, $newConfig['database']); |
|
|
|
|
45
|
|
|
$this->execSeederCommands($name); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function execSeederCommands(string $name) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
|
51
|
|
|
if ($this->option('seed')) { |
52
|
|
|
$this->call('db:seed'); |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($seeder = $this->option('seeder')) { |
57
|
|
|
$this->call('db:seed', ['--class' => $seeder]); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|