1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ikechukwukalu\Dynamicdatabaseconfig\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Ikechukwukalu\Dynamicdatabaseconfig\Trait\DatabaseConfig; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
|
8
|
|
|
class DynamicDatabaseConfigSeedCommand 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 = 'dynamic:seed |
18
|
|
|
{ref : The ref for the database configuration} |
19
|
|
|
{--seed : Running seeders} |
20
|
|
|
{--seeder= : Running a single seeder class}'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command description. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $description = 'Seeding into databases from env'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Execute the console command. |
31
|
|
|
*/ |
32
|
|
|
public function handle(): void |
33
|
|
|
{ |
34
|
|
|
$ref = $this->argument('ref'); |
35
|
|
|
|
36
|
|
|
[$database, $configuration, $name] = $this->getDynamicDatabaseConfiguration($ref); |
|
|
|
|
37
|
|
|
|
38
|
|
|
if (!$database) { |
39
|
|
|
$this->components->error("No database configuration was found"); |
|
|
|
|
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->components->info("Running seeder(s) for {$name} with ref={$ref}"); |
44
|
|
|
|
45
|
|
|
$newConfig = $this->setNewDynamicConfig($database, $configuration); |
|
|
|
|
46
|
|
|
$this->addNewConfig($database, $name, $newConfig); |
47
|
|
|
$this->createDatabase($database, $configuration['database']); |
48
|
|
|
$this->execSeederCommands($name); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function execSeederCommands(string $name) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
if ($this->option('seed')) { |
54
|
|
|
$this->call('db:seed'); |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($seeder = $this->option('seeder')) { |
59
|
|
|
$this->call('db:seed', ['--class' => $seeder]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|