changeset 4:a20439b1c9d3

Added Model generator and other updates.
author luka
date Tue, 27 Jun 2023 20:16:55 -0400
parents 6468684362c2
children b0b2e79ad8e6 4bb4daa9e3f1
files .php-cs-fixer.cache examples/ExampleGenerator.php.stub src/Generator/BaseGenerator.php src/Generator/Controller/ControllerGenerator.php src/Generator/Model/ModelGenerator.php src/Generator/Model/stubs/model.pivot.stub src/Generator/Model/stubs/model.stub src/Generator/Replacer.php src/MagicForgerServiceProvider.php
diffstat 9 files changed, 138 insertions(+), 108 deletions(-) [+]
line wrap: on
line diff
--- a/.php-cs-fixer.cache	Tue Jun 27 15:32:47 2023 -0400
+++ b/.php-cs-fixer.cache	Tue Jun 27 20:16:55 2023 -0400
@@ -1,1 +1,1 @@
-{"php":"8.2.7","version":"3.19.1","indent":"    ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"blank_lines_before_namespace":true,"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"compact_nullable_typehint":true,"curly_braces_position":{"allow_single_line_empty_anonymous_classes":true},"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"return_type_declaration":true,"short_scalar_cast":true,"single_import_per_statement":{"group_to_single_imports":false},"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_line_after_imports":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true},"hashes":{"src\/Generator\/BaseGenerator.php":"bc9d41ab0960ae2c2b3c74c1d33086fb","src\/Replacer.php":"c6b5f421259f41016014c00270ca001c","src\/MagicForgerServiceProvider.php":"e56f92adc0181e9b7681de8ca8aca6eb","src\/Generator\/Controller\/ControllerGenerator.php":"abc9620527a5cc4194215ce7eeabcd50","src\/Generator\/Generator.php":"794271f48bed8f156878fc36a25ff60f","src\/Generator\/Replacer.php":"69aa83cbb0efbe8235730af51b68212f"}}
\ No newline at end of file
+{"php":"8.2.7","version":"3.19.1","indent":"    ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"blank_lines_before_namespace":true,"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"compact_nullable_typehint":true,"curly_braces_position":{"allow_single_line_empty_anonymous_classes":true},"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"return_type_declaration":true,"short_scalar_cast":true,"single_import_per_statement":{"group_to_single_imports":false},"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_line_after_imports":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true},"hashes":{"src\/Generator\/BaseGenerator.php":"409aadd181fbd4f216fe31f4f42ca3e2","src\/Replacer.php":"c6b5f421259f41016014c00270ca001c","src\/MagicForgerServiceProvider.php":"e56f92adc0181e9b7681de8ca8aca6eb","src\/Generator\/Controller\/ControllerGenerator.php":"075e65a43e5ecc55fbc4dcd7636dfcc1","src\/Generator\/Generator.php":"794271f48bed8f156878fc36a25ff60f","src\/Generator\/Replacer.php":"5a7f98d22504edb6aba92b061ddd9803","src\/Generator\/Model\/ModelGenerator.php":"e78c07fc13bd9c379991e8b01b13c672"}}
\ No newline at end of file
--- a/examples/ExampleGenerator.php.stub	Tue Jun 27 15:32:47 2023 -0400
+++ b/examples/ExampleGenerator.php.stub	Tue Jun 27 20:16:55 2023 -0400
@@ -1,17 +1,15 @@
 <?php
 
-namespace App\Console\Commands;
+namespace Wizzard\MagicForger\Generator\Controller;
 
 use Symfony\Component\Console\Attribute\AsCommand;
 use Wizzard\MagicForger\Generator\BaseGenerator;
 use Wizzard\MagicForger\Replacer;
-//use Illuminate\Console\Concerns\CreatesMatchingTest;
+use Illuminate\Support\Str;
 
 #[AsCommand(name: 'mf:{{ Command Name }}')]
 class {{ Class Name }}Generator extends BaseGenerator
 {
-    //use CreatesMatchingTest;
-
     /**
      * The name and signature of the console command.
      *
@@ -33,7 +31,6 @@
      */
     protected $type = '{{ Class Name }}';
 
-
     /**
      * Execute the console command.
      */
@@ -49,7 +46,7 @@
      */
     protected function getStub()
     {
-        return $this->resolveStubPath('/stubs/seeder.stub');
+        return $this->resolveStubPath('/stubs/{{ Command Name }}.stub');
     }
 
     /**
@@ -64,27 +61,20 @@
             ? $customPath
             : __DIR__.$stub;
     }
+
+    protected function getClassName($name)
+    {
+        return $this->{{ Command Name }}_name($name);
+    }
+
     /**
-     * Parse the class name and format according to the root namespace.
+     * Get the stub file for the generator.
      *
-     * @param  string  $name
      * @return string
      */
-    protected function qualifyClass($name)
+    protected function getPath($name = null)
     {
-        $name = ltrim($name, '\\/');
-
-        $name = str_replace('/', '\\', $name);
-
-        $rootNamespace = $this->rootNamespace();
-
-        if (Str::startsWith($name, $rootNamespace)) {
-            return $name;
-        }
-
-        return $this->qualifyClass(
-            $this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name
-        );
+        return str_replace(['App\\', '\\'], ['app/', '/'], $this->get{{ Class Name }}Namespace() . '/' . $this->{{ Command Name }}_name($this->getTableInput()) . '.php');
     }
 }
 
--- a/src/Generator/BaseGenerator.php	Tue Jun 27 15:32:47 2023 -0400
+++ b/src/Generator/BaseGenerator.php	Tue Jun 27 20:16:55 2023 -0400
@@ -36,6 +36,8 @@
      */
     public function handle()
     {
+
+
         // First we need to ensure that the table exists, then we can
         if (! $this->tableExists($this->getTableInput())) {
             $this->components->error('The table: "'.$this->getTableInput().'" does not exist in the database.');
@@ -43,31 +45,19 @@
             return false;
         }
 
-        $name = $this->qualifyClass($this->getTableInput());
-
-        $path = $this->getPath($name);
-
-        $file = $this->getFile($name);
+        $path = $this->getPath();
 
-        dd($this->get_all_inserts($file));
+        $file = $this->getFile($path);
 
-        // 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
-        // file so that it gets the correctly formatted namespace and class name.
+        $file = $this->apply_replacements($file);
+
         $this->makeDirectory($path);
 
-        $this->files->put($path, $this->sortImports($this->buildClass($name)));
+        $this->files->put($path, $this->sortImports($file));
 
         $info = $this->type;
 
-        if (in_array(CreatesMatchingTest::class, class_uses_recursive($this))) {
-            if ($this->handleTestCreation($path)) {
-                $info .= ' and test';
-            }
-        }
-
         $this->components->info(sprintf('%s [%s] created successfully.', $info, $path));
-
     }
 
     /**
@@ -129,6 +119,7 @@
     protected function getOptions()
     {
         return [
+            ['fresh', 'f', InputOption::VALUE_NONE, 'Start from the stub or use existing if possible.'],
         ];
     }
 
@@ -142,6 +133,14 @@
     }
 
     /**
+     * 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.
@@ -149,12 +148,11 @@
      */
     protected function getFile($name)
     {
-
-        if ((! $this->hasOption('force') ||
-             ! $this->option('force')) &&
-             $this->alreadyExists($name)) {
+        if ((! $this->hasOption('fresh') ||
+             ! $this->option('fresh')) &&
+             $this->fileExists($name)) {
             //Working with an existing file
-            $this->files->get($name);
+            return $this->files->get($name);
         }
 
         //Working with a stub
@@ -171,6 +169,7 @@
         return trim($this->argument('table'));
     }
 
+
     /**
      * Determines if the table exists in the current database
      */
--- a/src/Generator/Controller/ControllerGenerator.php	Tue Jun 27 15:32:47 2023 -0400
+++ b/src/Generator/Controller/ControllerGenerator.php	Tue Jun 27 20:16:55 2023 -0400
@@ -7,13 +7,9 @@
 use Wizzard\MagicForger\Replacer;
 use Illuminate\Support\Str;
 
-//use Illuminate\Console\Concerns\CreatesMatchingTest;
-
 #[AsCommand(name: 'mf:controller')]
 class ControllerGenerator extends BaseGenerator
 {
-    //use CreatesMatchingTest;
-
     /**
      * The name and signature of the console command.
      *
@@ -35,46 +31,12 @@
      */
     protected $type = 'Controller';
 
-
     /**
      * Execute the console command.
      */
     public function handle()
     {
-
-
-        // First we need to ensure that the table exists, then we can
-        if (! $this->tableExists($this->getTableInput())) {
-            $this->components->error('The table: "'.$this->getTableInput().'" does not exist in the database.');
-
-            return false;
-        }
-
-        $name = $this->qualifyClass($this->getTableInput());
-
-        $path = $this->getPath($name);
-
-        $file = $this->getFile($path);
-
-        $file = $this->apply_replacements($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
-        // file so that it gets the correctly formatted namespace and class name.
-        $path = $this->makeDirectory($path);
-
-        $this->files->put($path, $this->sortImports($file));
-
-        $info = $this->type;
-
-        if (in_array(CreatesMatchingTest::class, class_uses_recursive($this))) {
-            if ($this->handleTestCreation($path)) {
-                $info .= ' and test';
-            }
-        }
-
-        $this->components->info(sprintf('%s [%s] created successfully.', $info, $path));
+        parent::handle();
     }
 
     /**
@@ -99,28 +61,6 @@
             ? $customPath
             : __DIR__.$stub;
     }
-    /**
-     * Parse the class name and format according to the root namespace.
-     *
-     * @param  string  $name
-     * @return string
-     */
-    protected function qualifyClass($name)
-    {
-        $name = ltrim($name, '\\/');
-
-        $name = str_replace('/', '\\', $name);
-
-        $rootNamespace = $this->rootNamespace();
-
-        if (Str::startsWith($name, $rootNamespace)) {
-            return $name;
-        }
-
-        return $this->qualifyClass(
-            $this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name
-        );
-    }
 
     protected function getClassName($name)
     {
@@ -132,7 +72,7 @@
      *
      * @return string
      */
-    protected function getPath($name)
+    protected function getPath($name = null)
     {
         return str_replace(['App\\', '\\'], ['app/', '/'], $this->getControllerNamespace() . '/' . $this->controller_name($this->getTableInput()) . '.php');
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Generator/Model/ModelGenerator.php	Tue Jun 27 20:16:55 2023 -0400
@@ -0,0 +1,79 @@
+<?php
+
+namespace Wizzard\MagicForger\Generator\Controller;
+
+use Symfony\Component\Console\Attribute\AsCommand;
+use Wizzard\MagicForger\Generator\BaseGenerator;
+use Wizzard\MagicForger\Replacer;
+use Illuminate\Support\Str;
+
+#[AsCommand(name: 'mf:model')]
+class ModelGenerator extends BaseGenerator
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $name = 'mf:model';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Generates the Model File for a table.';
+
+    /**
+     * The type of class being generated.
+     *
+     * @var string
+     */
+    protected $type = 'Model';
+
+    /**
+     * Execute the console command.
+     */
+    public function handle()
+    {
+        parent::handle();
+    }
+
+    /**
+     * Get the stub file for the generator.
+     *
+     * @return string
+     */
+    protected function getStub()
+    {
+        return $this->resolveStubPath('/stubs/model.stub');
+    }
+
+    /**
+     * Resolve the fully-qualified path to the stub.
+     *
+     * @param  string  $stub
+     * @return string
+     */
+    protected function resolveStubPath($stub)
+    {
+        return is_file($customPath = $this->laravel->basePath(trim($stub, '/')))
+            ? $customPath
+            : __DIR__.$stub;
+    }
+
+    protected function getClassName($name)
+    {
+        return $this->model_name($name);
+    }
+
+    /**
+     * Get the stub file for the generator.
+     *
+     * @return string
+     */
+    protected function getPath($name = null)
+    {
+        return str_replace(['App\\', '\\'], ['app/', '/'], $this->getModelNamespace() . '/' . $this->model_name($this->getTableInput()) . '.php');
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Generator/Model/stubs/model.pivot.stub	Tue Jun 27 20:16:55 2023 -0400
@@ -0,0 +1,10 @@
+<?php
+
+namespace {{ namespace }};
+
+use Illuminate\Database\Eloquent\Relations\Pivot;
+
+class {{ class }} extends Pivot
+{
+    //
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Generator/Model/stubs/model.stub	Tue Jun 27 20:16:55 2023 -0400
@@ -0,0 +1,11 @@
+<?php
+
+namespace {{ namespace }};
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class {{ class }} extends Model
+{
+    use HasFactory;
+}
--- a/src/Generator/Replacer.php	Tue Jun 27 15:32:47 2023 -0400
+++ b/src/Generator/Replacer.php	Tue Jun 27 20:16:55 2023 -0400
@@ -68,7 +68,6 @@
                     "{{ storeRequest }}" => $this->store_request_name($table_name),
                     "{{ tableName }}" => $table_name,
                     "{{ updateRequest }}" => $this->update_request_name($table_name),
-
         ];
 
         return $replacements;
--- a/src/MagicForgerServiceProvider.php	Tue Jun 27 15:32:47 2023 -0400
+++ b/src/MagicForgerServiceProvider.php	Tue Jun 27 20:16:55 2023 -0400
@@ -5,6 +5,7 @@
 use Illuminate\Support\ServiceProvider;
 use Wizzard\MagicForger\Generator\Generator;
 use Wizzard\MagicForger\Generator\Controller\ControllerGenerator;
+use Wizzard\MagicForger\Generator\Model\ModelGenerator;
 
 class MagicForgerServiceProvider extends ServiceProvider
 {
@@ -17,6 +18,7 @@
             $this->commands([
                 Generator::class,
                 ControllerGenerator::class,
+                ModelGenerator::class,
             ]);
         }
     }