Mercurial > packages > magicforger
comparison src/Generator/BaseGenerator.php @ 7:769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
| author | luka |
|---|---|
| date | Wed, 18 Oct 2023 21:04:11 -0400 |
| parents | b0b2e79ad8e6 |
| children | d4730c14806d |
comparison
equal
deleted
inserted
replaced
| 6:b46922d4a301 | 7:769a17898cc0 |
|---|---|
| 1 <?php | 1 <?php |
| 2 | 2 |
| 3 namespace Wizzard\MagicForger\Generator; | 3 namespace Wizzard\MagicForger\Generator; |
| 4 | 4 |
| 5 use DB; | 5 use DB; |
| 6 | |
| 7 use Illuminate\Console\GeneratorCommand; | 6 use Illuminate\Console\GeneratorCommand; |
| 8 use Illuminate\Support\Str; | |
| 9 use Symfony\Component\Console\Attribute\AsCommand; | |
| 10 use Symfony\Component\Console\Input\InputInterface; | 7 use Symfony\Component\Console\Input\InputInterface; |
| 11 use Symfony\Component\Console\Input\InputOption; | 8 use Symfony\Component\Console\Input\InputOption; |
| 12 use Symfony\Component\Console\Output\OutputInterface; | 9 use Symfony\Component\Console\Output\OutputInterface; |
| 13 | |
| 14 use Wizzard\MagicForger\Replacer\Replacer; | 10 use Wizzard\MagicForger\Replacer\Replacer; |
| 11 use Wizzard\MagicForger\Replacer\TableReplacer; | |
| 15 | 12 |
| 16 abstract class BaseGenerator extends GeneratorCommand | 13 abstract class BaseGenerator extends GeneratorCommand |
| 17 { | 14 { |
| 18 use Replacer; | 15 use Replacer; |
| 16 use TableReplacer; | |
| 19 | 17 |
| 20 /** | 18 /** |
| 21 * The schema of the database. | 19 * The schema of the database. |
| 22 * | 20 * |
| 23 * @var string | 21 * @var string |
| 25 protected $schema; | 23 protected $schema; |
| 26 | 24 |
| 27 /** | 25 /** |
| 28 * The tables available in the schema. | 26 * The tables available in the schema. |
| 29 * | 27 * |
| 30 * @var array | 28 * @var array |
| 31 */ | 29 */ |
| 32 protected $tables; | 30 protected $tables; |
| 33 | 31 |
| 34 /** | 32 /** |
| 35 * The current Table being used. | 33 * The current Table being used. |
| 41 /** | 39 /** |
| 42 * Execute the console command. | 40 * Execute the console command. |
| 43 */ | 41 */ |
| 44 public function handle() | 42 public function handle() |
| 45 { | 43 { |
| 46 | |
| 47 | |
| 48 // First we need to ensure that the table exists, then we can | 44 // First we need to ensure that the table exists, then we can |
| 49 if (! $this->tableExists($this->getTableInput())) { | 45 if (!$this->tableExists($this->getTableInput())) { |
| 50 $this->components->error('The table: "'.$this->getTableInput().'" does not exist in the database.'); | 46 $this->components->error('The table: "'.$this->getTableInput().'" does not exist in the database.'); |
| 51 | 47 |
| 52 return false; | 48 return false; |
| 53 } | 49 } |
| 54 | 50 |
| 56 | 52 |
| 57 $file = $this->getFile($path); | 53 $file = $this->getFile($path); |
| 58 | 54 |
| 59 $file = $this->apply_replacements($file); | 55 $file = $this->apply_replacements($file); |
| 60 | 56 |
| 57 $file = $this->apply_inserts($file); | |
| 58 | |
| 61 $this->makeDirectory($path); | 59 $this->makeDirectory($path); |
| 62 | 60 |
| 63 $this->files->put($path, $this->sortImports($file)); | 61 $this->files->put($path, $this->sortImports($file)); |
| 64 | 62 |
| 63 $this->format_file($path); | |
| 64 | |
| 65 $info = $this->type; | 65 $info = $this->type; |
| 66 | 66 |
| 67 $this->components->info(sprintf('%s [%s] created successfully.', $info, $path)); | 67 $this->components->info(sprintf('%s [%s] created successfully.', $info, $path)); |
| 68 } | 68 } |
| 69 | 69 |
| 70 /** | 70 /** |
| 71 * Override the original so that we can prompt for a table with autocomplete. | 71 * Override the original so that we can prompt for a table with autocomplete. |
| 72 * | |
| 73 */ | 72 */ |
| 74 protected function promptForMissingArguments(InputInterface $input, OutputInterface $output) | 73 protected function promptForMissingArguments(InputInterface $input, OutputInterface $output) |
| 75 { | 74 { |
| 76 $prompted = false; | 75 $prompted = false; |
| 77 if(is_null($input->getArgument('table'))) { | 76 if (is_null($input->getArgument('table'))) { |
| 78 $prompted = true; | 77 $prompted = true; |
| 79 $table = null; | 78 $table = null; |
| 80 while ($table === null) { | 79 while (null === $table) { |
| 81 $table = $this->components->askWithCompletion( | 80 $table = $this->components->askWithCompletion( |
| 82 'What Table should we use?', | 81 'What Table should we use?', |
| 83 $this->possibleTables() | 82 $this->possibleTables() |
| 84 ); | 83 ); |
| 85 } | 84 } |
| 88 } | 87 } |
| 89 | 88 |
| 90 parent::promptForMissingArguments($input, $output); | 89 parent::promptForMissingArguments($input, $output); |
| 91 | 90 |
| 92 // This will get missed if we prompt here but not in the parent | 91 // This will get missed if we prompt here but not in the parent |
| 93 if($prompted) { | 92 if ($prompted) { |
| 94 $this->afterPromptingForMissingArguments($input, $output); | 93 $this->afterPromptingForMissingArguments($input, $output); |
| 95 } | 94 } |
| 96 } | 95 } |
| 97 | 96 |
| 98 /** | 97 /** |
| 138 protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output) | 137 protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output) |
| 139 { | 138 { |
| 140 } | 139 } |
| 141 | 140 |
| 142 /** | 141 /** |
| 143 * Determines if the file exists | 142 * Determines if the file exists. |
| 144 */ | 143 */ |
| 145 protected function fileExists(string $path): bool | 144 protected function fileExists(string $path): bool |
| 146 { | 145 { |
| 147 return $this->files->exists($path); | 146 return $this->files->exists($path); |
| 148 } | 147 } |
| 149 | 148 |
| 150 /** | 149 /** |
| 151 * Gets the file that will be worked on. If there is already an existing file | 150 * Gets the file that will be worked on. If there is already an existing file |
| 152 * then we can open that. However if we are forcing the operation, then we | 151 * then we can open that. However if we are forcing the operation, then we |
| 153 * will start with an empty stub. | 152 * will start with an empty stub. |
| 154 * | |
| 155 */ | 153 */ |
| 156 protected function getFile($name) | 154 protected function getFile($name) |
| 157 { | 155 { |
| 158 if ((! $this->hasOption('fresh') || | 156 if (!($this->hasOption('fresh') |
| 159 ! $this->option('fresh')) && | 157 && $this->option('fresh')) |
| 160 $this->fileExists($name)) { | 158 && $this->fileExists($name)) { |
| 161 //Working with an existing file | 159 // Working with an existing file |
| 162 return $this->files->get($name); | 160 return $this->files->get($name); |
| 163 } | 161 } |
| 164 | 162 |
| 165 //Working with a stub | 163 // Working with a stub |
| 166 return $this->files->get($this->getStub()); | 164 return $this->files->get($this->getStub()); |
| 167 } | 165 } |
| 168 | 166 |
| 169 | |
| 170 /** | 167 /** |
| 171 * Get the desired class table from the input. | 168 * Get the desired class table from the input. |
| 172 * | 169 * |
| 173 * @return string | 170 * @return string |
| 174 */ | 171 */ |
| 175 protected function getTableInput() | 172 protected function getTableInput() |
| 176 { | 173 { |
| 177 return trim($this->argument('table')); | 174 return trim($this->argument('table')); |
| 178 } | 175 } |
| 179 | 176 |
| 180 | 177 /** |
| 181 /** | 178 * Determines if the table exists in the current database. |
| 182 * Determines if the table exists in the current database | |
| 183 */ | 179 */ |
| 184 protected function tableExists(string $table_name): bool | 180 protected function tableExists(string $table_name): bool |
| 185 { | 181 { |
| 186 return in_array($table_name, $this->getTables()); | 182 return in_array($table_name, $this->getTables()); |
| 187 } | 183 } |
| 193 { | 189 { |
| 194 return $this->getTables(); | 190 return $this->getTables(); |
| 195 } | 191 } |
| 196 | 192 |
| 197 /** | 193 /** |
| 198 * Get the tables in the schema | 194 * Get the tables in the schema. |
| 199 */ | 195 */ |
| 200 protected function getTables() | 196 protected function getTables() |
| 201 { | 197 { |
| 202 if (is_null($this->tables)) { | 198 if (is_null($this->tables)) { |
| 203 $this->tables = collect($this->getSchema()->listTableNames())->all(); | 199 $this->tables = collect($this->getSchema()->listTableNames())->all(); |
| 205 | 201 |
| 206 return $this->tables; | 202 return $this->tables; |
| 207 } | 203 } |
| 208 | 204 |
| 209 /** | 205 /** |
| 210 * Get the database schema for DB interactions | 206 * Get the database schema for DB interactions. |
| 211 */ | 207 */ |
| 212 protected function getSchema() | 208 protected function getSchema() |
| 213 { | 209 { |
| 214 if (is_null($this->schema)) { | 210 if (is_null($this->schema)) { |
| 215 $this->schema = DB::connection()->getDoctrineSchemaManager(); | 211 $this->schema = \DB::connection()->getDoctrineSchemaManager(); |
| 216 } | 212 } |
| 217 | 213 |
| 218 return $this->schema; | 214 return $this->schema; |
| 219 } | 215 } |
| 220 | 216 |
| 229 } | 225 } |
| 230 | 226 |
| 231 protected function setCurrentTable(string $table_name) | 227 protected function setCurrentTable(string $table_name) |
| 232 { | 228 { |
| 233 $table = null; | 229 $table = null; |
| 234 if(!is_null($table_name) && trim($table_name) !== '') { | 230 if (!is_null($table_name) && '' !== trim($table_name)) { |
| 235 $table = $this->getTable($table_name); | 231 $table = $this->getTable($table_name); |
| 236 } | 232 } |
| 237 $this->currentTable = $table; | 233 $this->currentTable = $table; |
| 238 } | 234 } |
| 235 | |
| 236 protected function format_file(string $path) | |
| 237 { | |
| 238 exec('php-cs-fixer fix '.$path); | |
| 239 } | |
| 239 } | 240 } |
