diff 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
line wrap: on
line diff
--- a/src/Generator/BaseGenerator.php	Sun Jun 25 14:45:15 2023 -0400
+++ b/src/Generator/BaseGenerator.php	Tue Jun 27 15:32:47 2023 -0400
@@ -4,33 +4,18 @@
 
 use DB;
 
-use Illuminate\Routing\Console\ControllerMakeCommand;
-
+use Illuminate\Console\GeneratorCommand;
+use Illuminate\Support\Str;
 use Symfony\Component\Console\Attribute\AsCommand;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
-use Illuminate\Support\Str;
 
-use Wizzard\MagicForger\Replacer;
+use Wizzard\MagicForger\Generator\Replacer;
 
-#[AsCommand(name: 'mf')]
-class BaseGenerator extends ControllerMakeCommand
+abstract class BaseGenerator extends GeneratorCommand
 {
-    /**
-     * The name and signature of the console command.
-     *
-     * @var string
-     */
-    protected $name = 'mf';
-
-    /**
-     * The console command description.
-     *
-     * @var string
-     */
-    protected $description = 'Generates any (or all) of the available files.';
-
+    use Replacer;
 
     /**
      * The console command description.
@@ -51,62 +36,24 @@
      */
     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;
         }
 
-        if ($this->option('all')) {
-            $this->input->setOption('factory', true);
-            $this->input->setOption('seed', true);
-            $this->input->setOption('migration', true);
-            $this->input->setOption('controller', true);
-            $this->input->setOption('model', true);
-        }
-
-        if ($this->option('factory')) {
-            $this->createFactory();
-        }
-
-        if ($this->option('migration')) {
-            $this->createMigration();
-        }
-
-        if ($this->option('seed')) {
-            $this->createSeeder();
-        }
-
-        if ($this->option('controller')) {
-            $this->createController();
-        }
-
-        if ($this->option('model')) {
-            $this->createModel();
-        }
-
-        //TODO: when working on an actual
         $name = $this->qualifyClass($this->getTableInput());
 
         $path = $this->getPath($name);
-        dd($name, $path);
 
-        // Next, We will check to see if the class already exists. If it does, we don't want
-        // to create the class and overwrite the user's code. So, we will bail out so the
-        // code is untouched. Otherwise, we will continue generating this class' files.
-        if ((! $this->hasOption('force') ||
-             ! $this->option('force')) &&
-             $this->alreadyExists($this->getNameInput())) {
-            $this->components->error($this->type.' already exists.');
+        $file = $this->getFile($name);
 
-            return false;
-        }
+        dd($this->get_all_inserts($file));
 
         // Next, we will generate the path to the location where this class' file should get
         // written. Then, we will build the class and make the proper replacements on the
-        // stub files so that it gets the correctly formatted namespace and class name.
+        // file so that it gets the correctly formatted namespace and class name.
         $this->makeDirectory($path);
 
         $this->files->put($path, $this->sortImports($this->buildClass($name)));
@@ -120,28 +67,18 @@
         }
 
         $this->components->info(sprintf('%s [%s] created successfully.', $info, $path));
-        echo "Generating Replacements\n";
-        $replacer = new Replacer();
-
-        $table_name = 'timesheet_statuses';
-        $model_name = $replacer->model_name($table_name);
-        $controller_name = $replacer->controller_name($table_name);
-        $human_readable = $replacer->human_readable($table_name);
-        $human_readable_lc = $replacer->human_readable_lc($table_name);
-        $replacer->log("Table Name : $table_name");
-        $replacer->log("Model Name : $model_name");
-        $replacer->log("Controller Name : $controller_name");
-        $replacer->log("Human Readable: $human_readable");
-        $replacer->log("Human Readable LC: $human_readable_lc");
-
-        echo "Stub Location\n";
-        $replacer->log($this->getStub());
 
     }
 
+    /**
+     * Override the original so that we can prompt for a table with autocomplete.
+     *
+     */
     protected function promptForMissingArguments(InputInterface $input, OutputInterface $output)
     {
+        $prompted = false;
         if(is_null($input->getArgument('table'))) {
+            $prompted = true;
             $table = null;
             while ($table === null) {
                 $table = $this->components->askWithCompletion(
@@ -152,7 +89,13 @@
 
             $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);
+        }
     }
 
     /**
@@ -186,12 +129,6 @@
     protected function getOptions()
     {
         return [
-            /* ['type', null, InputOption::VALUE_REQUIRED, 'Manually specify the controller stub file to use'], */
-            /* ['force', null, InputOption::VALUE_NONE, 'Create the class even if the controller already exists'], */
-            /* ['invokable', 'i', InputOption::VALUE_NONE, 'Generate a single method, invokable controller class'], */
-
-            ['all', 'a', InputOption::VALUE_NONE, 'Generate a migration, seeder, factory, policy, resource controller, and form request classes for the model'],
-
         ];
     }
 
@@ -204,11 +141,6 @@
     {
     }
 
-    ////////////////////////////////////////////
-    ///				TO GO IN THE BASE CLASS				 ///
-    ////////////////////////////////////////////
-
-
     /**
      * 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
@@ -239,6 +171,9 @@
         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());
@@ -265,7 +200,7 @@
     }
 
     /**
-     * Get the database schema
+     * Get the database schema for DB interactions
      */
     protected function getSchema()
     {