diff src/Generator/BaseGenerator.php @ 2:cf9993c5c7df

Updated .vimrc for some helper commands. updated the Base Generator Brought the controller generator into the package created an example generator, but it needs some work.
author luka
date Sun, 25 Jun 2023 14:45:15 -0400
parents ca36acd2bef2
children 6468684362c2
line wrap: on
line diff
--- a/src/Generator/BaseGenerator.php	Sat Jun 24 01:08:01 2023 -0400
+++ b/src/Generator/BaseGenerator.php	Sun Jun 25 14:45:15 2023 -0400
@@ -10,15 +10,13 @@
 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;
 
 #[AsCommand(name: 'mf')]
 class BaseGenerator extends ControllerMakeCommand
 {
-    protected $schema;
-		protected $tables;
-
     /**
      * The name and signature of the console command.
      *
@@ -33,6 +31,21 @@
      */
     protected $description = 'Generates any (or all) of the available files.';
 
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $schema;
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $tables;
+
     /**
      * Execute the console command.
      */
@@ -74,11 +87,11 @@
             $this->createModel();
         }
 
-//TODO: when working on an actual 
+        //TODO: when working on an actual
         $name = $this->qualifyClass($this->getTableInput());
 
         $path = $this->getPath($name);
-				dd($name, $path);
+        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
@@ -128,15 +141,17 @@
 
     protected function promptForMissingArguments(InputInterface $input, OutputInterface $output)
     {
-        $table = null;
-        while ($table === null) {
-            $table = $this->components->askWithCompletion(
-                'What Table should we use?',
-                $this->possibleTables()
-            );
+        if(is_null($input->getArgument('table'))) {
+            $table = null;
+            while ($table === null) {
+                $table = $this->components->askWithCompletion(
+                    'What Table should we use?',
+                    $this->possibleTables()
+                );
+            }
+
+            $input->setArgument('table', $table);
         }
-
-        $input->setArgument('table', $table);
         parent::promptForMissingArguments($input, $output);
     }
 
@@ -175,7 +190,6 @@
             /* ['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'], */
 
-            ['table', null, InputOption::VALUE_REQUIRED, 'The table to generate files for.'],
             ['all', 'a', InputOption::VALUE_NONE, 'Generate a migration, seeder, factory, policy, resource controller, and form request classes for the model'],
 
         ];
@@ -194,6 +208,27 @@
     ///				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
+     * will start with an empty stub.
+     *
+     */
+    protected function getFile($name)
+    {
+
+        if ((! $this->hasOption('force') ||
+             ! $this->option('force')) &&
+             $this->alreadyExists($name)) {
+            //Working with an existing file
+            $this->files->get($name);
+        }
+
+        //Working with a stub
+        return $this->files->get($this->getStub());
+    }
+
     /**
      * Get the desired class table from the input.
      *
@@ -214,19 +249,19 @@
      */
     protected function possibleTables()
     {
+        return $this->getTables();
+    }
 
-				return $this->getTables();
-    }
-		
     /**
      * Get the tables in the schema
      */
     protected function getTables()
     {
-        $tables = $this->getSchema()->listTableNames();
+        if (is_null($this->tables)) {
+            $this->tables = collect($this->getSchema()->listTableNames())->all();
+        }
 
-        return collect($tables)
-            ->all();
+        return $this->tables;
     }
 
     /**