|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rawilk\LaravelModules\Commands\Other; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Contracts\Debug\ExceptionHandler; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Rawilk\LaravelModules\Contracts\Repository; |
|
9
|
|
|
use Rawilk\LaravelModules\Module; |
|
10
|
|
|
use RuntimeException; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
use Throwable; |
|
13
|
|
|
|
|
14
|
|
|
class SeedCommand extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
protected $signature = 'module:seed |
|
18
|
|
|
{module? : The name of the module to seed} |
|
19
|
|
|
{--class= : Specify a specific seeder class to run} |
|
20
|
|
|
{--database= : The database connection to seed} |
|
21
|
|
|
{--force : Force the operation to run when in production}'; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
protected $description = 'Run database seeds for a specific module or all modules.'; |
|
25
|
|
|
|
|
26
|
|
|
public function handle(): ?int |
|
27
|
|
|
{ |
|
28
|
|
|
try { |
|
29
|
|
|
if ($name = $this->argument('module')) { |
|
30
|
|
|
$name = Str::studly($name); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
$this->moduleSeed($this->getModuleByName($name)); |
|
33
|
|
|
} else { |
|
34
|
|
|
$modules = $this->getModuleRepository()->getOrdered(); |
|
35
|
|
|
|
|
36
|
|
|
array_walk($modules, [$this, 'moduleSeed']); |
|
37
|
|
|
|
|
38
|
|
|
$this->info('All modules seeded.'); |
|
39
|
|
|
} |
|
40
|
|
|
} catch (Throwable $e) { |
|
41
|
|
|
$this->reportException($e); |
|
42
|
|
|
|
|
43
|
|
|
$this->renderException($this->getOutput(), $e); |
|
44
|
|
|
|
|
45
|
|
|
return 1; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return 0; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private function dbSeed(string $className): void |
|
52
|
|
|
{ |
|
53
|
|
|
$class = $className; |
|
54
|
|
|
|
|
55
|
|
|
if ($seederClass = $this->option('class')) { |
|
56
|
|
|
$class = Str::finish(substr($className, 0, strrpos($className, '\\')), '\\') . $seederClass; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$params = ['--class' => $class]; |
|
60
|
|
|
|
|
61
|
|
|
if ($database = $this->option('database')) { |
|
62
|
|
|
$params['--database'] = $database; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if ($force = $this->option('force')) { |
|
66
|
|
|
$params['--force'] = $force; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$this->call('db:seed', $params); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function getModuleByName(string $name): Module |
|
73
|
|
|
{ |
|
74
|
|
|
$modules = $this->getModuleRepository(); |
|
75
|
|
|
|
|
76
|
|
|
if (! $modules->has($name)) { |
|
|
|
|
|
|
77
|
|
|
throw new RuntimeException("Module [{$name}] does not exist!"); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $modules->find($name); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private function getModuleRepository(): Repository |
|
84
|
|
|
{ |
|
85
|
|
|
$modules = $this->laravel['modules']; |
|
86
|
|
|
|
|
87
|
|
|
if (! $modules instanceof Repository) { |
|
88
|
|
|
throw new RuntimeException('Module repository not found!'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $modules; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function moduleSeed(Module $module): void |
|
95
|
|
|
{ |
|
96
|
|
|
$seeders = []; |
|
97
|
|
|
$name = $module->getName(); |
|
98
|
|
|
$config = $module->get('migration'); |
|
99
|
|
|
|
|
100
|
|
|
if (is_array($config) && array_key_exists('seeds', $config)) { |
|
101
|
|
|
foreach ((array) $config['seeds'] as $class) { |
|
102
|
|
|
if (class_exists($class)) { |
|
103
|
|
|
$seeders[] = $class; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (count($seeders) > 0) { |
|
109
|
|
|
array_walk($seeders, [$this, 'dbSeed']); |
|
110
|
|
|
|
|
111
|
|
|
$this->info("Module [{$name}] seeded."); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
private function renderException(OutputInterface $output, Throwable $e): void |
|
116
|
|
|
{ |
|
117
|
|
|
$this->laravel[ExceptionHandler::class]->renderForConsole($output, $e); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
private function reportException(Throwable $e): void |
|
121
|
|
|
{ |
|
122
|
|
|
$this->laravel[ExceptionHandler::class]->report($e); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|