1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Nwidart\Modules\Support\Config\GenerateConfigReader; |
7
|
|
|
use Nwidart\Modules\Support\Stub; |
8
|
|
|
use Nwidart\Modules\Traits\CanClearModulesCache; |
9
|
|
|
use Nwidart\Modules\Traits\ModuleCommandTrait; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
|
13
|
|
|
class SeedMakeCommand extends GeneratorCommand |
14
|
|
|
{ |
15
|
|
|
use ModuleCommandTrait, CanClearModulesCache; |
16
|
|
|
|
17
|
|
|
protected $argumentName = 'name'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command name. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $name = 'module:make-seed'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The console command description. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $description = 'Generate new seeder for the specified module.'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get the console command arguments. |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
protected function getArguments() |
39
|
|
|
{ |
40
|
|
|
return [ |
41
|
|
|
['name', InputArgument::REQUIRED, 'The name of seeder will be created.'], |
42
|
|
|
['module', InputArgument::OPTIONAL, 'The name of module will be used.'], |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the console command options. |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
protected function getOptions() |
52
|
|
|
{ |
53
|
|
|
return [ |
54
|
|
|
[ |
55
|
|
|
'master', |
56
|
|
|
null, |
57
|
|
|
InputOption::VALUE_NONE, |
58
|
|
|
'Indicates the seeder will created is a master database seeder.', |
59
|
|
|
], |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
protected function getTemplateContents() |
67
|
|
|
{ |
68
|
|
|
$module = $this->laravel['modules']->findOrFail($this->getModuleName()); |
69
|
|
|
|
70
|
|
|
return (new Stub('/seeder.stub', [ |
71
|
|
|
'NAME' => $this->getSeederName(), |
72
|
|
|
'MODULE' => $this->getModuleName(), |
73
|
|
|
'NAMESPACE' => $this->getClassNamespace($module), |
74
|
|
|
|
75
|
|
|
]))->render(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
|
|
protected function getDestinationFilePath() |
82
|
|
|
{ |
83
|
|
|
$this->clearCache(); |
84
|
|
|
|
85
|
|
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
86
|
|
|
|
87
|
|
|
$seederPath = GenerateConfigReader::read('seeder'); |
88
|
|
|
|
89
|
|
|
return $path . $seederPath->getPath() . '/' . $this->getSeederName() . '.php'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get seeder name. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
private function getSeederName() |
98
|
|
|
{ |
99
|
|
|
$end = $this->option('master') ? 'DatabaseSeeder' : 'TableSeeder'; |
100
|
|
|
|
101
|
|
|
return Str::studly($this->argument('name')) . $end; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get default namespace. |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function getDefaultNamespace() : string |
110
|
|
|
{ |
111
|
|
|
return $this->laravel['modules']->config('paths.generator.seeder.path', 'Database/Seeders'); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|