-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathSeedCommand.php
More file actions
129 lines (107 loc) · 3.11 KB
/
Copy pathSeedCommand.php
File metadata and controls
129 lines (107 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace Pingpong\Modules\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Pingpong\Modules\Traits\ModuleCommandTrait;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
class SeedCommand extends Command
{
use ModuleCommandTrait;
/**
* The console command name.
*
* @var string
*/
protected $name = 'module:seed';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Run database seeder from the specified module or from all modules.';
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$this->module = $this->laravel['modules'];
$name = $this->argument('module');
if ($name) {
if (!$this->module->has(Str::studly($name))) {
return $this->error("Module [$name] does not exists.");
}
$class = $this->getSeederName($name);
if (class_exists($class)) {
$this->dbseed($name);
return $this->info("Module [$name] seeded.");
} else {
return $this->error("Class [$class] does not exists.");
}
}
foreach ($this->module->getOrdered() as $module) {
$name = $module->getName();
if (class_exists($this->getSeederName($name))) {
$this->dbseed($name);
$this->info("Module [$name] seeded.");
}
}
return $this->info('All modules seeded.');
}
/**
* Seed the specified module.
*
* @parama string $name
*
* @return array
*/
protected function dbseed($name)
{
$params = [
'--class' => $this->option('class') ?: $this->getSeederName($name),
];
if ($option = $this->option('database')) {
$params['--database'] = $option;
}
$this->call('db:seed', $params);
}
/**
* Get master database seeder name for the specified module.
*
* @param string $name
*
* @return string
*/
public function getSeederName($name)
{
$name = Str::studly($name);
$namespace = $this->laravel['modules']->config('namespace');
return $namespace.'\\'.$name.'\Database\Seeders\\'.$name.'DatabaseSeeder';
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('module', InputArgument::OPTIONAL, 'The name of module will be used.'),
);
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array('class', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder', null),
array('all', null, InputOption::VALUE_NONE, 'Whether or not we should seed all modules.'),
array('database', null, InputOption::VALUE_OPTIONAL, 'The database connection to seed.'),
);
}
}