Mercurial > packages > magicforger
comparison src/Generator/BaseGenerator.php @ 1:ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
| author | luka |
|---|---|
| date | Sat, 24 Jun 2023 01:08:01 -0400 |
| parents | |
| children | cf9993c5c7df |
comparison
equal
deleted
inserted
replaced
| 0:329123c41eaf | 1:ca36acd2bef2 |
|---|---|
| 1 <?php | |
| 2 | |
| 3 namespace Wizzard\MagicForger\Generator; | |
| 4 | |
| 5 use DB; | |
| 6 | |
| 7 use Illuminate\Routing\Console\ControllerMakeCommand; | |
| 8 | |
| 9 use Symfony\Component\Console\Attribute\AsCommand; | |
| 10 use Symfony\Component\Console\Input\InputInterface; | |
| 11 use Symfony\Component\Console\Input\InputOption; | |
| 12 use Symfony\Component\Console\Output\OutputInterface; | |
| 13 | |
| 14 use Wizzard\MagicForger\Replacer; | |
| 15 | |
| 16 #[AsCommand(name: 'mf')] | |
| 17 class BaseGenerator extends ControllerMakeCommand | |
| 18 { | |
| 19 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 | |
| 29 /** | |
| 30 * The console command description. | |
| 31 * | |
| 32 * @var string | |
| 33 */ | |
| 34 protected $description = 'Generates any (or all) of the available files.'; | |
| 35 | |
| 36 /** | |
| 37 * Execute the console command. | |
| 38 */ | |
| 39 public function handle() | |
| 40 { | |
| 41 | |
| 42 // First we need to ensure that the table exists, then we can | |
| 43 if (!$this->tableExists($this->getTableInput())) { | |
| 44 $this->components->error('The table: "'.$this->getTableInput().'" does not exist in the database.'); | |
| 45 | |
| 46 return false; | |
| 47 } | |
| 48 | |
| 49 if ($this->option('all')) { | |
| 50 $this->input->setOption('factory', true); | |
| 51 $this->input->setOption('seed', true); | |
| 52 $this->input->setOption('migration', true); | |
| 53 $this->input->setOption('controller', true); | |
| 54 $this->input->setOption('model', true); | |
| 55 } | |
| 56 | |
| 57 if ($this->option('factory')) { | |
| 58 $this->createFactory(); | |
| 59 } | |
| 60 | |
| 61 if ($this->option('migration')) { | |
| 62 $this->createMigration(); | |
| 63 } | |
| 64 | |
| 65 if ($this->option('seed')) { | |
| 66 $this->createSeeder(); | |
| 67 } | |
| 68 | |
| 69 if ($this->option('controller')) { | |
| 70 $this->createController(); | |
| 71 } | |
| 72 | |
| 73 if ($this->option('model')) { | |
| 74 $this->createModel(); | |
| 75 } | |
| 76 | |
| 77 //TODO: when working on an actual | |
| 78 $name = $this->qualifyClass($this->getTableInput()); | |
| 79 | |
| 80 $path = $this->getPath($name); | |
| 81 dd($name, $path); | |
| 82 | |
| 83 // 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 | |
| 85 // code is untouched. Otherwise, we will continue generating this class' files. | |
| 86 if ((! $this->hasOption('force') || | |
| 87 ! $this->option('force')) && | |
| 88 $this->alreadyExists($this->getNameInput())) { | |
| 89 $this->components->error($this->type.' already exists.'); | |
| 90 | |
| 91 return false; | |
| 92 } | |
| 93 | |
| 94 // Next, we will generate the path to the location where this class' file should get | |
| 95 // written. Then, we will build the class and make the proper replacements on the | |
| 96 // stub files so that it gets the correctly formatted namespace and class name. | |
| 97 $this->makeDirectory($path); | |
| 98 | |
| 99 $this->files->put($path, $this->sortImports($this->buildClass($name))); | |
| 100 | |
| 101 $info = $this->type; | |
| 102 | |
| 103 if (in_array(CreatesMatchingTest::class, class_uses_recursive($this))) { | |
| 104 if ($this->handleTestCreation($path)) { | |
| 105 $info .= ' and test'; | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 $this->components->info(sprintf('%s [%s] created successfully.', $info, $path)); | |
| 110 echo "Generating Replacements\n"; | |
| 111 $replacer = new Replacer(); | |
| 112 | |
| 113 $table_name = 'timesheet_statuses'; | |
| 114 $model_name = $replacer->model_name($table_name); | |
| 115 $controller_name = $replacer->controller_name($table_name); | |
| 116 $human_readable = $replacer->human_readable($table_name); | |
| 117 $human_readable_lc = $replacer->human_readable_lc($table_name); | |
| 118 $replacer->log("Table Name : $table_name"); | |
| 119 $replacer->log("Model Name : $model_name"); | |
| 120 $replacer->log("Controller Name : $controller_name"); | |
| 121 $replacer->log("Human Readable: $human_readable"); | |
| 122 $replacer->log("Human Readable LC: $human_readable_lc"); | |
| 123 | |
| 124 echo "Stub Location\n"; | |
| 125 $replacer->log($this->getStub()); | |
| 126 | |
| 127 } | |
| 128 | |
| 129 protected function promptForMissingArguments(InputInterface $input, OutputInterface $output) | |
| 130 { | |
| 131 $table = null; | |
| 132 while ($table === null) { | |
| 133 $table = $this->components->askWithCompletion( | |
| 134 'What Table should we use?', | |
| 135 $this->possibleTables() | |
| 136 ); | |
| 137 } | |
| 138 | |
| 139 $input->setArgument('table', $table); | |
| 140 parent::promptForMissingArguments($input, $output); | |
| 141 } | |
| 142 | |
| 143 /** | |
| 144 * Get the console command arguments. | |
| 145 * | |
| 146 * @return array | |
| 147 */ | |
| 148 protected function getArguments() | |
| 149 { | |
| 150 return [ | |
| 151 ['table', InputOption::VALUE_REQUIRED, 'The table to generate files for.'], | |
| 152 ]; | |
| 153 } | |
| 154 | |
| 155 /** | |
| 156 * Prompt for missing input arguments using the returned questions. | |
| 157 * | |
| 158 * @return array | |
| 159 */ | |
| 160 protected function promptForMissingArgumentsUsing() | |
| 161 { | |
| 162 return [ | |
| 163 ]; | |
| 164 } | |
| 165 | |
| 166 /** | |
| 167 * Get the console command options. | |
| 168 * | |
| 169 * @return array | |
| 170 */ | |
| 171 protected function getOptions() | |
| 172 { | |
| 173 return [ | |
| 174 /* ['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'], */ | |
| 176 /* ['invokable', 'i', InputOption::VALUE_NONE, 'Generate a single method, invokable controller class'], */ | |
| 177 | |
| 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'], | |
| 180 | |
| 181 ]; | |
| 182 } | |
| 183 | |
| 184 /** | |
| 185 * Interact further with the user if they were prompted for missing arguments. | |
| 186 * | |
| 187 * @return void | |
| 188 */ | |
| 189 protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output) | |
| 190 { | |
| 191 } | |
| 192 | |
| 193 //////////////////////////////////////////// | |
| 194 /// TO GO IN THE BASE CLASS /// | |
| 195 //////////////////////////////////////////// | |
| 196 | |
| 197 /** | |
| 198 * Get the desired class table from the input. | |
| 199 * | |
| 200 * @return string | |
| 201 */ | |
| 202 protected function getTableInput() | |
| 203 { | |
| 204 return trim($this->argument('table')); | |
| 205 } | |
| 206 | |
| 207 protected function tableExists(string $table_name): bool | |
| 208 { | |
| 209 return in_array($table_name, $this->getTables()); | |
| 210 } | |
| 211 | |
| 212 /** | |
| 213 * Get a list of possible table names. | |
| 214 */ | |
| 215 protected function possibleTables() | |
| 216 { | |
| 217 | |
| 218 return $this->getTables(); | |
| 219 } | |
| 220 | |
| 221 /** | |
| 222 * Get the tables in the schema | |
| 223 */ | |
| 224 protected function getTables() | |
| 225 { | |
| 226 $tables = $this->getSchema()->listTableNames(); | |
| 227 | |
| 228 return collect($tables) | |
| 229 ->all(); | |
| 230 } | |
| 231 | |
| 232 /** | |
| 233 * Get the database schema | |
| 234 */ | |
| 235 protected function getSchema() | |
| 236 { | |
| 237 if (is_null($this->schema)) { | |
| 238 $this->schema = DB::connection()->getDoctrineSchemaManager(); | |
| 239 } | |
| 240 | |
| 241 return $this->schema; | |
| 242 } | |
| 243 } |
