Mercurial > packages > magicforger
diff src/Generator/BaseGenerator.php @ 34:f65ab84ee47f default
merge with codex
| author | luka |
|---|---|
| date | Wed, 10 Sep 2025 21:00:47 -0400 |
| parents | 555bfaa500ac |
| children |
line wrap: on
line diff
--- a/src/Generator/BaseGenerator.php Sat Dec 02 10:20:32 2023 -0500 +++ b/src/Generator/BaseGenerator.php Wed Sep 10 21:00:47 2025 -0400 @@ -1,242 +1,203 @@ <?php -namespace Wizzard\MagicForger\Generator; +namespace Wizard\MagicForger\Generator; -use DB; use Illuminate\Console\GeneratorCommand; +use Illuminate\Support\Facades\Schema; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Wizzard\MagicForger\Replacer\Replacer; -use Wizzard\MagicForger\Replacer\TableReplacer; +use Wizard\MagicForger\Replacer\Replacer; +use Wizard\MagicForger\Replacer\TableReplacer; abstract class BaseGenerator extends GeneratorCommand { use Replacer; use TableReplacer; - /** - * The schema of the database. - * - * @var string - */ - protected $schema; + protected $schema = null; + + protected $tables = null; - /** - * The tables available in the schema. - * - * @var array - */ - protected $tables; + protected $currentTable = null; - /** - * The current Table being used. - * - * @var table - */ - protected $currentTable; + protected static $cached_snippets = []; - /** - * Execute the console command. - */ public function handle() { - // First we need to ensure that the table exists, then we can - if (!$this->tableExists($this->getTableInput())) { + + if (! $this->tableExists($this->getTableInput())) { $this->components->error('The table: "'.$this->getTableInput().'" does not exist in the database.'); return false; } $this->setCurrentTable($this->getTableInput()); - $path = $this->getPath(); - $file = $this->getFile($path); - $file = $this->apply_replacements($file); - $file = $this->apply_inserts($file); - $this->makeDirectory($path); - $this->files->put($path, $this->sortImports($file)); - $this->format_file($path); - - $info = $this->type; - - $this->components->info(sprintf('%s [%s] created successfully.', $info, $path)); + $this->components->info(sprintf('%s [%s] created successfully.', $this->type, $path)); } - /** - * Override the original so that we can prompt for a table with autocomplete. - */ - protected function promptForMissingArguments(InputInterface $input, OutputInterface $output) + protected function promptForMissingArguments(InputInterface $input, OutputInterface $output): void { $prompted = false; if (is_null($input->getArgument('table'))) { $prompted = true; $table = null; - while (null === $table) { + while ($table === null) { $table = $this->components->askWithCompletion( 'What Table should we use?', $this->possibleTables() ); } - $input->setArgument('table', $table); } parent::promptForMissingArguments($input, $output); - // This will get missed if we prompt here but not in the parent if ($prompted) { $this->afterPromptingForMissingArguments($input, $output); } } - /** - * Get the console command arguments. - * - * @return array - */ - protected function getArguments() + protected function getArguments(): array { return [ ['table', InputOption::VALUE_REQUIRED, 'The table to generate files for.'], ]; } - /** - * Prompt for missing input arguments using the returned questions. - * - * @return array - */ - protected function promptForMissingArgumentsUsing() + protected function promptForMissingArgumentsUsing(): array { - return [ - ]; + return []; } - /** - * Get the console command options. - * - * @return array - */ - protected function getOptions() + protected function getOptions(): array { return [ ['fresh', 'f', InputOption::VALUE_NONE, 'Start from the stub or use existing if possible.'], ]; } - /** - * Interact further with the user if they were prompted for missing arguments. - * - * @return void - */ - protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output) + protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output): void { + // Additional logic after prompting goes here } - /** - * Determines if the file exists. - */ protected function fileExists(string $path): bool { return $this->files->exists($path); } - /** - * Gets the file that will be worked on. If there is already an existing file - * then we can open that. However if we are forcing the operation, then we - * will start with an empty stub. - */ - protected function getFile($name) + protected function getFile($name): string { - if (!($this->hasOption('fresh') - && $this->option('fresh')) - && $this->fileExists($name)) { - // Working with an existing file + if (! ($this->hasOption('fresh') && $this->option('fresh')) && $this->fileExists($name)) { return $this->files->get($name); } - // Working with a stub return $this->files->get($this->getStub()); } - /** - * Get the desired class table from the input. - * - * @return string - */ - protected function getTableInput() + protected function getTableInput(): string { return trim($this->argument('table')); } - /** - * Determines if the table exists in the current database. - */ protected function tableExists(string $table_name): bool { return in_array($table_name, $this->getTables()); } - /** - * Get a list of possible table names. - */ - protected function possibleTables() + protected function possibleTables(): array { return $this->getTables(); } - /** - * Get the tables in the schema. - */ - protected function getTables() + protected function getTables(): array { if (is_null($this->tables)) { - $this->tables = collect($this->getSchema()->listTableNames())->all(); + $this->tables = Schema::getTableListing(schema: config('database.connections.mariadb.database'), schemaQualified: false); } return $this->tables; } - /** - * Get the database schema for DB interactions. - */ - protected function getSchema() - { - if (is_null($this->schema)) { - $this->schema = \DB::connection()->getDoctrineSchemaManager(); - } - - return $this->schema; - } - protected function getTable(string $table_name) { return $this->getSchema()->introspectTable($table_name); } + /* + * returns array of columns in the form of: + * + [ + "name" => "column_type" + "type_name" => "bigint" + "type" => "bigint(20) unsigned" + "collation" => null + "nullable" => true + "default" => "NULL" + "auto_increment" => false + "comment" => null + "generation" => null + ] + */ + protected static function getTableColumns(string $table_name) + { + return Schema::getColumns($table_name); + } + + /* + * returns array of foreign keys in the form of: + * + [ + "name" => "foreign_key_name" + "columns" => [ + 0 => "local_column_name" + ] + "foreign_schema" => "schema_name" + "foreign_table" => "foreign_table_name" + "foreign_columns" => [ + 0 => "foreign_column_name" + ] + "on_update" => "restrict" + "on_delete" => "restrict" + ] + * + */ + protected static function getTableForeignKeys(string $table_name) + { + return Schema::getForeignKeys($table_name); + } + protected function getCurrentTable() { return $this->currentTable; } - protected function setCurrentTable(string $table_name) + protected function setCurrentTable(string $table_name): void { - $table = null; - if (!is_null($table_name) && '' !== trim($table_name)) { - $table = $this->getTable($table_name); - } - $this->currentTable = $table; + $this->currentTable = $table_name; + } + + protected function format_file(string $path): void + { + exec('./vendor/bin/pint '.escapeshellarg($path)); } - protected function format_file(string $path) + protected function getSnippet($snippet_name) { - exec('php-cs-fixer fix '.$path); + // Cache snippet contents to avoid re-reading files + if (! isset(self::$cached_snippets[$snippet_name])) { + self::$cached_snippets[$snippet_name] = $this->files->get( + $this->resolveStubPath("/snippets/$snippet_name.stub")); + } + + return self::$cached_snippets[$snippet_name]; } }
