Mercurial > packages > magicforger
comparison src/Generator/BaseGenerator.php @ 3:6468684362c2
It works! Created a controller, no update insert but it works
| author | luka |
|---|---|
| date | Tue, 27 Jun 2023 15:32:47 -0400 |
| parents | cf9993c5c7df |
| children | a20439b1c9d3 |
comparison
equal
deleted
inserted
replaced
| 2:cf9993c5c7df | 3:6468684362c2 |
|---|---|
| 2 | 2 |
| 3 namespace Wizzard\MagicForger\Generator; | 3 namespace Wizzard\MagicForger\Generator; |
| 4 | 4 |
| 5 use DB; | 5 use DB; |
| 6 | 6 |
| 7 use Illuminate\Routing\Console\ControllerMakeCommand; | 7 use Illuminate\Console\GeneratorCommand; |
| 8 | 8 use Illuminate\Support\Str; |
| 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\Generator\Replacer; |
| 15 use Wizzard\MagicForger\Replacer; | 15 |
| 16 | 16 abstract class BaseGenerator extends GeneratorCommand |
| 17 #[AsCommand(name: 'mf')] | |
| 18 class BaseGenerator extends ControllerMakeCommand | |
| 19 { | 17 { |
| 20 /** | 18 use Replacer; |
| 21 * The name and signature of the console command. | 19 |
| 20 /** | |
| 21 * The console command description. | |
| 22 * | 22 * |
| 23 * @var string | 23 * @var string |
| 24 */ | 24 */ |
| 25 protected $name = 'mf'; | 25 protected $schema; |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * The console command description. | 28 * The console command description. |
| 29 * | 29 * |
| 30 * @var string | 30 * @var string |
| 31 */ | 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 */ | |
| 40 protected $schema; | |
| 41 | |
| 42 /** | |
| 43 * The console command description. | |
| 44 * | |
| 45 * @var string | |
| 46 */ | |
| 47 protected $tables; | 32 protected $tables; |
| 48 | 33 |
| 49 /** | 34 /** |
| 50 * Execute the console command. | 35 * Execute the console command. |
| 51 */ | 36 */ |
| 52 public function handle() | 37 public function handle() |
| 53 { | 38 { |
| 54 | |
| 55 // First we need to ensure that the table exists, then we can | 39 // First we need to ensure that the table exists, then we can |
| 56 if (!$this->tableExists($this->getTableInput())) { | 40 if (! $this->tableExists($this->getTableInput())) { |
| 57 $this->components->error('The table: "'.$this->getTableInput().'" does not exist in the database.'); | 41 $this->components->error('The table: "'.$this->getTableInput().'" does not exist in the database.'); |
| 58 | 42 |
| 59 return false; | 43 return false; |
| 60 } | 44 } |
| 61 | 45 |
| 62 if ($this->option('all')) { | |
| 63 $this->input->setOption('factory', true); | |
| 64 $this->input->setOption('seed', true); | |
| 65 $this->input->setOption('migration', true); | |
| 66 $this->input->setOption('controller', true); | |
| 67 $this->input->setOption('model', true); | |
| 68 } | |
| 69 | |
| 70 if ($this->option('factory')) { | |
| 71 $this->createFactory(); | |
| 72 } | |
| 73 | |
| 74 if ($this->option('migration')) { | |
| 75 $this->createMigration(); | |
| 76 } | |
| 77 | |
| 78 if ($this->option('seed')) { | |
| 79 $this->createSeeder(); | |
| 80 } | |
| 81 | |
| 82 if ($this->option('controller')) { | |
| 83 $this->createController(); | |
| 84 } | |
| 85 | |
| 86 if ($this->option('model')) { | |
| 87 $this->createModel(); | |
| 88 } | |
| 89 | |
| 90 //TODO: when working on an actual | |
| 91 $name = $this->qualifyClass($this->getTableInput()); | 46 $name = $this->qualifyClass($this->getTableInput()); |
| 92 | 47 |
| 93 $path = $this->getPath($name); | 48 $path = $this->getPath($name); |
| 94 dd($name, $path); | 49 |
| 95 | 50 $file = $this->getFile($name); |
| 96 // Next, We will check to see if the class already exists. If it does, we don't want | 51 |
| 97 // to create the class and overwrite the user's code. So, we will bail out so the | 52 dd($this->get_all_inserts($file)); |
| 98 // code is untouched. Otherwise, we will continue generating this class' files. | |
| 99 if ((! $this->hasOption('force') || | |
| 100 ! $this->option('force')) && | |
| 101 $this->alreadyExists($this->getNameInput())) { | |
| 102 $this->components->error($this->type.' already exists.'); | |
| 103 | |
| 104 return false; | |
| 105 } | |
| 106 | 53 |
| 107 // Next, we will generate the path to the location where this class' file should get | 54 // Next, we will generate the path to the location where this class' file should get |
| 108 // written. Then, we will build the class and make the proper replacements on the | 55 // written. Then, we will build the class and make the proper replacements on the |
| 109 // stub files so that it gets the correctly formatted namespace and class name. | 56 // file so that it gets the correctly formatted namespace and class name. |
| 110 $this->makeDirectory($path); | 57 $this->makeDirectory($path); |
| 111 | 58 |
| 112 $this->files->put($path, $this->sortImports($this->buildClass($name))); | 59 $this->files->put($path, $this->sortImports($this->buildClass($name))); |
| 113 | 60 |
| 114 $info = $this->type; | 61 $info = $this->type; |
| 118 $info .= ' and test'; | 65 $info .= ' and test'; |
| 119 } | 66 } |
| 120 } | 67 } |
| 121 | 68 |
| 122 $this->components->info(sprintf('%s [%s] created successfully.', $info, $path)); | 69 $this->components->info(sprintf('%s [%s] created successfully.', $info, $path)); |
| 123 echo "Generating Replacements\n"; | 70 |
| 124 $replacer = new Replacer(); | 71 } |
| 125 | 72 |
| 126 $table_name = 'timesheet_statuses'; | 73 /** |
| 127 $model_name = $replacer->model_name($table_name); | 74 * Override the original so that we can prompt for a table with autocomplete. |
| 128 $controller_name = $replacer->controller_name($table_name); | 75 * |
| 129 $human_readable = $replacer->human_readable($table_name); | 76 */ |
| 130 $human_readable_lc = $replacer->human_readable_lc($table_name); | |
| 131 $replacer->log("Table Name : $table_name"); | |
| 132 $replacer->log("Model Name : $model_name"); | |
| 133 $replacer->log("Controller Name : $controller_name"); | |
| 134 $replacer->log("Human Readable: $human_readable"); | |
| 135 $replacer->log("Human Readable LC: $human_readable_lc"); | |
| 136 | |
| 137 echo "Stub Location\n"; | |
| 138 $replacer->log($this->getStub()); | |
| 139 | |
| 140 } | |
| 141 | |
| 142 protected function promptForMissingArguments(InputInterface $input, OutputInterface $output) | 77 protected function promptForMissingArguments(InputInterface $input, OutputInterface $output) |
| 143 { | 78 { |
| 79 $prompted = false; | |
| 144 if(is_null($input->getArgument('table'))) { | 80 if(is_null($input->getArgument('table'))) { |
| 81 $prompted = true; | |
| 145 $table = null; | 82 $table = null; |
| 146 while ($table === null) { | 83 while ($table === null) { |
| 147 $table = $this->components->askWithCompletion( | 84 $table = $this->components->askWithCompletion( |
| 148 'What Table should we use?', | 85 'What Table should we use?', |
| 149 $this->possibleTables() | 86 $this->possibleTables() |
| 150 ); | 87 ); |
| 151 } | 88 } |
| 152 | 89 |
| 153 $input->setArgument('table', $table); | 90 $input->setArgument('table', $table); |
| 154 } | 91 } |
| 92 | |
| 155 parent::promptForMissingArguments($input, $output); | 93 parent::promptForMissingArguments($input, $output); |
| 94 | |
| 95 // This will get missed if we prompt here but not in the parent | |
| 96 if($prompted) { | |
| 97 $this->afterPromptingForMissingArguments($input, $output); | |
| 98 } | |
| 156 } | 99 } |
| 157 | 100 |
| 158 /** | 101 /** |
| 159 * Get the console command arguments. | 102 * Get the console command arguments. |
| 160 * | 103 * |
| 184 * @return array | 127 * @return array |
| 185 */ | 128 */ |
| 186 protected function getOptions() | 129 protected function getOptions() |
| 187 { | 130 { |
| 188 return [ | 131 return [ |
| 189 /* ['type', null, InputOption::VALUE_REQUIRED, 'Manually specify the controller stub file to use'], */ | |
| 190 /* ['force', null, InputOption::VALUE_NONE, 'Create the class even if the controller already exists'], */ | |
| 191 /* ['invokable', 'i', InputOption::VALUE_NONE, 'Generate a single method, invokable controller class'], */ | |
| 192 | |
| 193 ['all', 'a', InputOption::VALUE_NONE, 'Generate a migration, seeder, factory, policy, resource controller, and form request classes for the model'], | |
| 194 | |
| 195 ]; | 132 ]; |
| 196 } | 133 } |
| 197 | 134 |
| 198 /** | 135 /** |
| 199 * Interact further with the user if they were prompted for missing arguments. | 136 * Interact further with the user if they were prompted for missing arguments. |
| 201 * @return void | 138 * @return void |
| 202 */ | 139 */ |
| 203 protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output) | 140 protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output) |
| 204 { | 141 { |
| 205 } | 142 } |
| 206 | |
| 207 //////////////////////////////////////////// | |
| 208 /// TO GO IN THE BASE CLASS /// | |
| 209 //////////////////////////////////////////// | |
| 210 | |
| 211 | 143 |
| 212 /** | 144 /** |
| 213 * Gets the file that will be worked on. If there is already an existing file | 145 * 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 | 146 * then we can open that. However if we are forcing the operation, then we |
| 215 * will start with an empty stub. | 147 * will start with an empty stub. |
| 237 protected function getTableInput() | 169 protected function getTableInput() |
| 238 { | 170 { |
| 239 return trim($this->argument('table')); | 171 return trim($this->argument('table')); |
| 240 } | 172 } |
| 241 | 173 |
| 174 /** | |
| 175 * Determines if the table exists in the current database | |
| 176 */ | |
| 242 protected function tableExists(string $table_name): bool | 177 protected function tableExists(string $table_name): bool |
| 243 { | 178 { |
| 244 return in_array($table_name, $this->getTables()); | 179 return in_array($table_name, $this->getTables()); |
| 245 } | 180 } |
| 246 | 181 |
| 263 | 198 |
| 264 return $this->tables; | 199 return $this->tables; |
| 265 } | 200 } |
| 266 | 201 |
| 267 /** | 202 /** |
| 268 * Get the database schema | 203 * Get the database schema for DB interactions |
| 269 */ | 204 */ |
| 270 protected function getSchema() | 205 protected function getSchema() |
| 271 { | 206 { |
| 272 if (is_null($this->schema)) { | 207 if (is_null($this->schema)) { |
| 273 $this->schema = DB::connection()->getDoctrineSchemaManager(); | 208 $this->schema = DB::connection()->getDoctrineSchemaManager(); |
