|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use Nwidart\Modules\Module; |
|
9
|
|
|
use Nwidart\Modules\Repository; |
|
10
|
|
|
use Nwidart\Modules\Traits\ModuleCommandTrait; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
13
|
|
|
|
|
14
|
|
|
class SeedCommand extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
use ModuleCommandTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The console command name. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $name = 'module:seed'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The console command description. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $description = 'Run database seeder from the specified module or from all modules.'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Execute the console command. |
|
34
|
|
|
* |
|
35
|
|
|
* @return mixed |
|
36
|
|
|
*/ |
|
37
|
|
|
public function fire() |
|
38
|
|
|
{ |
|
39
|
|
|
try { |
|
40
|
|
|
if ($name = $this->argument('module')) { |
|
41
|
|
|
$name = Str::studly($name); |
|
|
|
|
|
|
42
|
|
|
$this->moduleSeed($this->getModuleByName($name)); |
|
43
|
|
|
} else { |
|
44
|
|
|
$modules = $this->getModuleRepository()->getOrdered(); |
|
45
|
|
|
array_walk($modules, [$this, 'moduleSeed']); |
|
46
|
|
|
$this->info('All modules seeded.'); |
|
47
|
|
|
} |
|
48
|
|
|
} catch (\Exception $e) { |
|
49
|
|
|
$this->error($e->getMessage()); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @throws RuntimeException |
|
55
|
|
|
* |
|
56
|
|
|
* @return Repository |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getModuleRepository() |
|
59
|
|
|
{ |
|
60
|
|
|
$modules = $this->laravel['modules']; |
|
61
|
|
|
if (!$modules instanceof Repository) { |
|
62
|
|
|
throw new RuntimeException("Module repository not found!"); |
|
63
|
|
|
} |
|
64
|
|
|
return $modules; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param $name |
|
69
|
|
|
* |
|
70
|
|
|
* @throws RuntimeException |
|
71
|
|
|
* |
|
72
|
|
|
* @return Module |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getModuleByName($name) |
|
75
|
|
|
{ |
|
76
|
|
|
$modules = $this->getModuleRepository(); |
|
77
|
|
|
if ($modules->has($name) === false) { |
|
78
|
|
|
throw new RuntimeException("Module [$name] does not exists."); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $modules->get($name); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param Module $module |
|
86
|
|
|
* |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
|
|
public function moduleSeed(Module $module) |
|
90
|
|
|
{ |
|
91
|
|
|
$seeders = []; |
|
92
|
|
|
$name = $module->getName(); |
|
93
|
|
|
$config = $module->get('migration'); |
|
94
|
|
|
if (is_array($config) && array_key_exists('seeds', $config)) { |
|
95
|
|
|
foreach ((array)$config['seeds'] as $class) { |
|
96
|
|
|
if (@class_exists($class)) { |
|
97
|
|
|
$seeders[] = $class; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} else { |
|
101
|
|
|
$class = $this->getSeederName($name); //legacy support |
|
102
|
|
|
if (@class_exists($class)) { |
|
103
|
|
|
$seeders[] = $class; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if (count($seeders) > 0) { |
|
108
|
|
|
array_walk($seeders, [$this, 'dbSeed']); |
|
109
|
|
|
$this->info("Module [$name] seeded."); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Seed the specified module. |
|
115
|
|
|
* |
|
116
|
|
|
* @parama string $className |
|
117
|
|
|
* |
|
118
|
|
|
* @return array |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function dbSeed($className) |
|
121
|
|
|
{ |
|
122
|
|
|
$params = [ |
|
123
|
|
|
'--class' => $className, |
|
124
|
|
|
]; |
|
125
|
|
|
|
|
126
|
|
|
if ($option = $this->option('database')) { |
|
127
|
|
|
$params['--database'] = $option; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if ($option = $this->option('force')) { |
|
131
|
|
|
$params['--force'] = $option; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$this->call('db:seed', $params); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Get master database seeder name for the specified module. |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $name |
|
141
|
|
|
* |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getSeederName($name) |
|
145
|
|
|
{ |
|
146
|
|
|
$name = Str::studly($name); |
|
147
|
|
|
|
|
148
|
|
|
$namespace = $this->laravel['modules']->config('namespace'); |
|
149
|
|
|
|
|
150
|
|
|
return $namespace . '\\' . $name . '\Database\Seeders\\' . $name . 'DatabaseSeeder'; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Get the console command arguments. |
|
155
|
|
|
* |
|
156
|
|
|
* @return array |
|
157
|
|
|
*/ |
|
158
|
38 |
|
protected function getArguments() |
|
159
|
|
|
{ |
|
160
|
|
|
return array( |
|
161
|
38 |
|
array('module', InputArgument::OPTIONAL, 'The name of module will be used.'), |
|
162
|
|
|
); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Get the console command options. |
|
167
|
|
|
* |
|
168
|
|
|
* @return array |
|
169
|
|
|
*/ |
|
170
|
38 |
|
protected function getOptions() |
|
171
|
|
|
{ |
|
172
|
|
|
return array( |
|
173
|
38 |
|
array('database', null, InputOption::VALUE_OPTIONAL, 'The database connection to seed.'), |
|
174
|
|
|
array('force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'), |
|
175
|
|
|
); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.