|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Nwidart\Modules\Traits\ModuleCommandTrait; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
10
|
|
|
|
|
11
|
|
|
class SeedCommand extends Command |
|
12
|
|
|
{ |
|
13
|
|
|
use ModuleCommandTrait; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The console command name. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $name = 'module:seed'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The console command description. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $description = 'Run database seeder from the specified module or from all modules.'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Execute the console command. |
|
31
|
|
|
* |
|
32
|
|
|
* @return mixed |
|
33
|
|
|
*/ |
|
34
|
|
|
public function fire() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->module = $this->laravel['modules']; |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
$name = $this->argument('module'); |
|
39
|
|
|
|
|
40
|
|
|
if ($name) { |
|
41
|
|
|
if (!$this->module->has(Str::studly($name))) { |
|
|
|
|
|
|
42
|
|
|
return $this->error("Module [$name] does not exists."); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$class = $this->getSeederName($name); |
|
|
|
|
|
|
46
|
|
|
if (class_exists($class)) { |
|
47
|
|
|
$this->dbseed($name); |
|
48
|
|
|
|
|
49
|
|
|
return $this->info("Module [$name] seeded."); |
|
50
|
|
|
} else { |
|
51
|
|
|
return $this->error("Class [$class] does not exists."); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
foreach ($this->module->getOrdered() as $module) { |
|
56
|
|
|
$name = $module->getName(); |
|
57
|
|
|
$config = $module->get('migration'); |
|
58
|
|
|
if(is_array($config) && array_key_exists('seeds', $config)) { |
|
59
|
|
|
foreach((array)$config['seeds'] as $class) { |
|
60
|
|
|
if (class_exists($class)) { |
|
61
|
|
|
$this->dbseed($class); |
|
62
|
|
|
} else { |
|
63
|
|
|
return $this->error("Class [$class] does not exist"); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} else { |
|
67
|
|
|
if (class_exists($this->getSeederName($name))) { |
|
68
|
|
|
$this->dbseed($name); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
$this->info("Module [$name] seeded."); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this->info('All modules seeded.'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Seed the specified module. |
|
79
|
|
|
* |
|
80
|
|
|
* @parama string $name |
|
81
|
|
|
* |
|
82
|
|
|
* @return array |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function dbseed($name) |
|
85
|
|
|
{ |
|
86
|
|
|
$params = [ |
|
87
|
|
|
'--class' => $this->option('class') ?: $this->getSeederName($name), |
|
88
|
|
|
]; |
|
89
|
|
|
|
|
90
|
|
|
if ($option = $this->option('database')) { |
|
91
|
|
|
$params['--database'] = $option; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if ($option = $this->option('force')) { |
|
95
|
|
|
$params['--force'] = $option; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$this->call('db:seed', $params); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get master database seeder name for the specified module. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $name |
|
105
|
|
|
* |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getSeederName($name) |
|
109
|
|
|
{ |
|
110
|
|
|
$name = Str::studly($name); |
|
111
|
|
|
|
|
112
|
|
|
$namespace = $this->laravel['modules']->config('namespace'); |
|
113
|
|
|
|
|
114
|
|
|
return $namespace . '\\' . $name . '\Database\Seeders\\' . $name . 'DatabaseSeeder'; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get the console command arguments. |
|
119
|
|
|
* |
|
120
|
|
|
* @return array |
|
121
|
|
|
*/ |
|
122
|
38 |
|
protected function getArguments() |
|
123
|
|
|
{ |
|
124
|
|
|
return array( |
|
125
|
38 |
|
array('module', InputArgument::OPTIONAL, 'The name of module will be used.'), |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Get the console command options. |
|
131
|
|
|
* |
|
132
|
|
|
* @return array |
|
133
|
|
|
*/ |
|
134
|
38 |
|
protected function getOptions() |
|
135
|
|
|
{ |
|
136
|
|
|
return array( |
|
137
|
38 |
|
array('class', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder', null), |
|
138
|
|
|
array('all', null, InputOption::VALUE_NONE, 'Whether or not we should seed all modules.'), |
|
139
|
|
|
array('database', null, InputOption::VALUE_OPTIONAL, 'The database connection to seed.'), |
|
140
|
|
|
array('force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'), |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: