1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Milkmeowo\Framework\Repository\Generators\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Milkmeowo\Framework\Repository\Generators\ModelGenerator; |
10
|
|
|
use Milkmeowo\Framework\Repository\Generators\SeederGenerator; |
11
|
|
|
use Milkmeowo\Framework\Repository\Generators\CriteriaGenerator; |
12
|
|
|
use Milkmeowo\Framework\Repository\Generators\MigrationGenerator; |
13
|
|
|
use Milkmeowo\Framework\Repository\Generators\RepositoryEloquentGenerator; |
14
|
|
|
use Milkmeowo\Framework\Repository\Generators\RepositoryInterfaceGenerator; |
15
|
|
|
use Milkmeowo\Framework\Repository\Generators\Exceptions\FileAlreadyExistsException; |
16
|
|
|
|
17
|
|
|
class RepositoryCommand extends Command |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The name of command. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $name = 'starter:repository'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The description of command. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $description = 'Create a new repository.'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The type of class being generated. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $type = 'Repository'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Collection |
42
|
|
|
*/ |
43
|
|
|
protected $generators = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Execute the command. |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
public function fire() |
51
|
|
|
{ |
52
|
|
|
$this->generators = new Collection(); |
53
|
|
|
|
54
|
|
|
$this->generators->push(new MigrationGenerator([ |
55
|
|
|
'name' => 'create_'.snake_case(str_plural($this->argument('name'))).'_table', |
|
|
|
|
56
|
|
|
'fields' => $this->option('fillable'), |
57
|
|
|
'force' => $this->option('force'), |
58
|
|
|
])); |
59
|
|
|
|
60
|
|
|
$modelGenerator = new ModelGenerator([ |
61
|
|
|
'name' => $this->argument('name'), |
62
|
|
|
'fillable' => $this->option('fillable'), |
63
|
|
|
'force' => $this->option('force'), |
64
|
|
|
]); |
65
|
|
|
|
66
|
|
|
$this->generators->push($modelGenerator); |
67
|
|
|
|
68
|
|
|
$this->generators->push(new SeederGenerator([ |
69
|
|
|
'name' => $this->argument('name'), |
70
|
|
|
'force' => $this->option('force'), |
71
|
|
|
])); |
72
|
|
|
|
73
|
|
|
$this->generators->push(new RepositoryInterfaceGenerator([ |
74
|
|
|
'name' => $this->argument('name'), |
75
|
|
|
'force' => $this->option('force'), |
76
|
|
|
])); |
77
|
|
|
|
78
|
|
|
//$this->generators->push(new CriteriaGenerator([ |
|
|
|
|
79
|
|
|
// 'name' => $this->argument('name'), |
|
|
|
|
80
|
|
|
// 'force' => $this->option('force'), |
|
|
|
|
81
|
|
|
//])); |
82
|
|
|
|
83
|
|
|
foreach ($this->generators as $generator) { |
84
|
|
|
$generator->run(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$model = $modelGenerator->getRootNamespace().'\\'.$modelGenerator->getName(); |
88
|
|
|
$model = str_replace([ |
89
|
|
|
'\\', |
90
|
|
|
'/', |
91
|
|
|
], '\\', $model); |
92
|
|
|
|
93
|
|
|
try { |
94
|
|
|
(new RepositoryEloquentGenerator([ |
95
|
|
|
'name' => $this->argument('name'), |
96
|
|
|
'rules' => $this->option('rules'), |
97
|
|
|
'validator' => $this->option('validator'), |
98
|
|
|
'presenter' => $this->option('presenter'), |
99
|
|
|
'force' => $this->option('force'), |
100
|
|
|
'model' => $model, |
101
|
|
|
]))->run(); |
102
|
|
|
$this->info('Repository created successfully.'); |
103
|
|
|
} catch (FileAlreadyExistsException $e) { |
104
|
|
|
$this->error($this->type.' already exists!'); |
105
|
|
|
|
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* The array of command arguments. |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
public function getArguments() |
116
|
|
|
{ |
117
|
|
|
return [ |
118
|
|
|
[ |
119
|
|
|
'name', |
120
|
|
|
InputArgument::REQUIRED, |
121
|
|
|
'The name of class being generated.', |
122
|
|
|
null, |
123
|
|
|
], |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* The array of command options. |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
View Code Duplication |
public function getOptions() |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
return [ |
135
|
|
|
[ |
136
|
|
|
'fillable', |
137
|
|
|
null, |
138
|
|
|
InputOption::VALUE_OPTIONAL, |
139
|
|
|
'The fillable attributes.', |
140
|
|
|
null, |
141
|
|
|
], |
142
|
|
|
[ |
143
|
|
|
'rules', |
144
|
|
|
null, |
145
|
|
|
InputOption::VALUE_OPTIONAL, |
146
|
|
|
'The rules of validation attributes.', |
147
|
|
|
null, |
148
|
|
|
], |
149
|
|
|
[ |
150
|
|
|
'validator', |
151
|
|
|
null, |
152
|
|
|
InputOption::VALUE_OPTIONAL, |
153
|
|
|
'Adds validator reference to the repository.', |
154
|
|
|
null, |
155
|
|
|
], |
156
|
|
|
[ |
157
|
|
|
'presenter', |
158
|
|
|
null, |
159
|
|
|
InputOption::VALUE_OPTIONAL, |
160
|
|
|
'Adds presenter reference to the repository.', |
161
|
|
|
null, |
162
|
|
|
], |
163
|
|
|
[ |
164
|
|
|
'force', |
165
|
|
|
'f', |
166
|
|
|
InputOption::VALUE_NONE, |
167
|
|
|
'Force the creation if file already exists.', |
168
|
|
|
null, |
169
|
|
|
], |
170
|
|
|
]; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.