comparison src/Generator/BaseGenerator.php @ 2:cf9993c5c7df

Updated .vimrc for some helper commands. updated the Base Generator Brought the controller generator into the package created an example generator, but it needs some work.
author luka
date Sun, 25 Jun 2023 14:45:15 -0400
parents ca36acd2bef2
children 6468684362c2
comparison
equal deleted inserted replaced
1:ca36acd2bef2 2:cf9993c5c7df
8 8
9 use Symfony\Component\Console\Attribute\AsCommand; 9 use Symfony\Component\Console\Attribute\AsCommand;
10 use Symfony\Component\Console\Input\InputInterface; 10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption; 11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface; 12 use Symfony\Component\Console\Output\OutputInterface;
13 use Illuminate\Support\Str;
13 14
14 use Wizzard\MagicForger\Replacer; 15 use Wizzard\MagicForger\Replacer;
15 16
16 #[AsCommand(name: 'mf')] 17 #[AsCommand(name: 'mf')]
17 class BaseGenerator extends ControllerMakeCommand 18 class BaseGenerator extends ControllerMakeCommand
18 { 19 {
20 /**
21 * The name and signature of the console command.
22 *
23 * @var string
24 */
25 protected $name = 'mf';
26
27 /**
28 * The console command description.
29 *
30 * @var string
31 */
32 protected $description = 'Generates any (or all) of the available files.';
33
34
35 /**
36 * The console command description.
37 *
38 * @var string
39 */
19 protected $schema; 40 protected $schema;
20 protected $tables;
21
22 /**
23 * The name and signature of the console command.
24 *
25 * @var string
26 */
27 protected $name = 'mf';
28 41
29 /** 42 /**
30 * The console command description. 43 * The console command description.
31 * 44 *
32 * @var string 45 * @var string
33 */ 46 */
34 protected $description = 'Generates any (or all) of the available files.'; 47 protected $tables;
35 48
36 /** 49 /**
37 * Execute the console command. 50 * Execute the console command.
38 */ 51 */
39 public function handle() 52 public function handle()
72 85
73 if ($this->option('model')) { 86 if ($this->option('model')) {
74 $this->createModel(); 87 $this->createModel();
75 } 88 }
76 89
77 //TODO: when working on an actual 90 //TODO: when working on an actual
78 $name = $this->qualifyClass($this->getTableInput()); 91 $name = $this->qualifyClass($this->getTableInput());
79 92
80 $path = $this->getPath($name); 93 $path = $this->getPath($name);
81 dd($name, $path); 94 dd($name, $path);
82 95
83 // Next, We will check to see if the class already exists. If it does, we don't want 96 // Next, We will check to see if the class already exists. If it does, we don't want
84 // to create the class and overwrite the user's code. So, we will bail out so the 97 // to create the class and overwrite the user's code. So, we will bail out so the
85 // code is untouched. Otherwise, we will continue generating this class' files. 98 // code is untouched. Otherwise, we will continue generating this class' files.
86 if ((! $this->hasOption('force') || 99 if ((! $this->hasOption('force') ||
126 139
127 } 140 }
128 141
129 protected function promptForMissingArguments(InputInterface $input, OutputInterface $output) 142 protected function promptForMissingArguments(InputInterface $input, OutputInterface $output)
130 { 143 {
131 $table = null; 144 if(is_null($input->getArgument('table'))) {
132 while ($table === null) { 145 $table = null;
133 $table = $this->components->askWithCompletion( 146 while ($table === null) {
134 'What Table should we use?', 147 $table = $this->components->askWithCompletion(
135 $this->possibleTables() 148 'What Table should we use?',
136 ); 149 $this->possibleTables()
137 } 150 );
138 151 }
139 $input->setArgument('table', $table); 152
153 $input->setArgument('table', $table);
154 }
140 parent::promptForMissingArguments($input, $output); 155 parent::promptForMissingArguments($input, $output);
141 } 156 }
142 157
143 /** 158 /**
144 * Get the console command arguments. 159 * Get the console command arguments.
173 return [ 188 return [
174 /* ['type', null, InputOption::VALUE_REQUIRED, 'Manually specify the controller stub file to use'], */ 189 /* ['type', null, InputOption::VALUE_REQUIRED, 'Manually specify the controller stub file to use'], */
175 /* ['force', null, InputOption::VALUE_NONE, 'Create the class even if the controller already exists'], */ 190 /* ['force', null, InputOption::VALUE_NONE, 'Create the class even if the controller already exists'], */
176 /* ['invokable', 'i', InputOption::VALUE_NONE, 'Generate a single method, invokable controller class'], */ 191 /* ['invokable', 'i', InputOption::VALUE_NONE, 'Generate a single method, invokable controller class'], */
177 192
178 ['table', null, InputOption::VALUE_REQUIRED, 'The table to generate files for.'],
179 ['all', 'a', InputOption::VALUE_NONE, 'Generate a migration, seeder, factory, policy, resource controller, and form request classes for the model'], 193 ['all', 'a', InputOption::VALUE_NONE, 'Generate a migration, seeder, factory, policy, resource controller, and form request classes for the model'],
180 194
181 ]; 195 ];
182 } 196 }
183 197
192 206
193 //////////////////////////////////////////// 207 ////////////////////////////////////////////
194 /// TO GO IN THE BASE CLASS /// 208 /// TO GO IN THE BASE CLASS ///
195 //////////////////////////////////////////// 209 ////////////////////////////////////////////
196 210
211
212 /**
213 * Gets the file that will be worked on. If there is already an existing file
214 * then we can open that. However if we are forcing the operation, then we
215 * will start with an empty stub.
216 *
217 */
218 protected function getFile($name)
219 {
220
221 if ((! $this->hasOption('force') ||
222 ! $this->option('force')) &&
223 $this->alreadyExists($name)) {
224 //Working with an existing file
225 $this->files->get($name);
226 }
227
228 //Working with a stub
229 return $this->files->get($this->getStub());
230 }
231
197 /** 232 /**
198 * Get the desired class table from the input. 233 * Get the desired class table from the input.
199 * 234 *
200 * @return string 235 * @return string
201 */ 236 */
212 /** 247 /**
213 * Get a list of possible table names. 248 * Get a list of possible table names.
214 */ 249 */
215 protected function possibleTables() 250 protected function possibleTables()
216 { 251 {
217 252 return $this->getTables();
218 return $this->getTables(); 253 }
219 } 254
220
221 /** 255 /**
222 * Get the tables in the schema 256 * Get the tables in the schema
223 */ 257 */
224 protected function getTables() 258 protected function getTables()
225 { 259 {
226 $tables = $this->getSchema()->listTableNames(); 260 if (is_null($this->tables)) {
227 261 $this->tables = collect($this->getSchema()->listTableNames())->all();
228 return collect($tables) 262 }
229 ->all(); 263
264 return $this->tables;
230 } 265 }
231 266
232 /** 267 /**
233 * Get the database schema 268 * Get the database schema
234 */ 269 */