changeset 36:76584181267a ls_dev_2025_09

Got factories working in a basic way, not sure how complex tables will handle it though
author Luka Sitas <sitas.luka.97@gmail.com>
date Sat, 20 Sep 2025 17:14:29 -0400
parents 55d2e5c5dad9
children 116b36f5e73b
files src/Generator/Factory/FactoryGenerator.php src/Generator/Factory/snippets/column.stub src/Generator/Factory/snippets/value.stub src/Generator/Factory/stubs/factory.stub src/Generator/Generator.php src/Generator/Model/stubs/model.stub
diffstat 6 files changed, 29 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/src/Generator/Factory/FactoryGenerator.php	Thu Sep 11 21:25:51 2025 -0400
+++ b/src/Generator/Factory/FactoryGenerator.php	Sat Sep 20 17:14:29 2025 -0400
@@ -91,33 +91,34 @@
 
 		protected function renderColumns() {
         $insert = '';
+				$values = [];
         foreach ($this->get_columns() as $column) {
             if (in_array($column['name'], $this->columns_to_ignore)) {
                 continue;
             }
 
-
             $type = $column['type_name'];
 						$nullable = ($column['nullable'] ? '->optional($weight = 0.5)' : '' );
+						$value = 'fake()' . $nullable;
 						$name = $column['name'];
 
             // Get the expected header name
             $replacements = [
-                '{{value}}' => 'fake()' . $nullable . '->text()',
+                '{{value}}' => $value . '->text()',
                 '{{column_name}}' => $name,
             ];
 
             // date
             if (in_array($type, ['date'])) {
-                $replacements['{{value}}'] = 'fake()' . $nullable . '->date()';
+                $replacements['{{value}}'] = $value . '->date()';
             }
             // time
             if (in_array($type, ['timestamp'])) {
-                $replacements['{{value}}'] = 'fake()' . $nullable . '->timestamp()';
+                $replacements['{{value}}'] = $value . '->timestamp()';
             }
             // checkbox
             if (in_array($type, ['tinyint'])) {
-                $replacements['{{value}}'] = 'fake()' . $nullable . '->boolean()';
+                $replacements['{{value}}'] = $value . '->boolean()';
             }
             // select
             elseif (in_array($type, ['bigint']) && array_key_exists($name, $selects)) {
@@ -125,14 +126,17 @@
                 $replacements['{{value}}'] = '{{ $item->'.Str::singular($selects[$name]).'?->name ?? "" }}';
             }
             // bigint, float
-            elseif (in_array($type, ['bigint', 'float', 'int'])) {
-                $replacements['{{valueClass}}'] .= ' text-start';
+            elseif (in_array($type, ['bigint', 'int'])) {
+                $replacements['{{value}}'] = $value . '->randomNumber()';
+						}
+            elseif (in_array($type, ['float'])) {
+                $replacements['{{value}}'] = $value . '->randomFloat()';
             } else {
                 // text area
                 // varchar, , etc
             }
 
-            $snippet = $this->getSnippet('index/value');
+            $snippet = $this->getSnippet('value');
             // Replace placeholders with actual values
             $values[] = str_replace(
                 array_keys($replacements),
@@ -140,21 +144,9 @@
                 $snippet
             );
 
-
-
-						$tableName = $this->getCurrentTable();
-						$value = 'value'; // TODO: this should be determined based on column type
-						$columnName = $column['name'];
-
 						// Replace placeholders with actual values
-        		$string = str_replace(
-        		    ['{{value}}', '{{columnName}}'],
-        		    [$value, $columnName],
-        		    $snippet
-        		);
-            $insert .= sprintf("%s", $string);
         }
-				dd('done');
+				$insert = implode("\n", $values);
 
 				return $insert;
 		}
--- a/src/Generator/Factory/snippets/column.stub	Thu Sep 11 21:25:51 2025 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,6 +0,0 @@
-'{{columnName}}' => [
-	'column_name' => '{{columnName}}',
-	'table' => '{{tableName}}',
-	'display' => '{{columnDisplay}}',
-	'type' => '{{value}}',
-],
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Generator/Factory/snippets/value.stub	Sat Sep 20 17:14:29 2025 -0400
@@ -0,0 +1,2 @@
+'{{column_name}}' => {{value}},
+
--- a/src/Generator/Factory/stubs/factory.stub	Thu Sep 11 21:25:51 2025 -0400
+++ b/src/Generator/Factory/stubs/factory.stub	Sat Sep 20 17:14:29 2025 -0400
@@ -2,13 +2,13 @@
 
 namespace Database\Factories;
 
-use App\Models\{{ modelName }};
+use {{ namespacedModel }};
 use Illuminate\Database\Eloquent\Factories\Factory;
 use Illuminate\Support\Facades\Hash;
 use Illuminate\Support\Str;
 
 /**
- * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\{{ modelName }}>
+ * @extends \Illuminate\Database\Eloquent\Factories\Factory<{{ namespacedModel }}>
  */
 class {{ class }} extends Factory
 {
@@ -18,7 +18,7 @@
      *
      * @var class-string<\Illuminate\Database\Eloquent\Model>
      */
-    protected $model = {{ modelName }}::class;
+    protected $model = {{ model }}::class;
 
     /**
      * Define the model's default state.
--- a/src/Generator/Generator.php	Thu Sep 11 21:25:51 2025 -0400
+++ b/src/Generator/Generator.php	Sat Sep 20 17:14:29 2025 -0400
@@ -41,7 +41,7 @@
         /* dd($this->getCurrentTable()->getForeignKeys()); */
 
         if ($this->option('all')) {
-            /* $this->input->setOption('factory', true); */
+            $this->input->setOption('factory', true);
             /* $this->input->setOption('seed', true); */
             /* $this->input->setOption('migration', true); */
             $this->input->setOption('controller', true);
@@ -51,9 +51,9 @@
             $this->input->setOption('route', true);
         }
 
-        /* if ($this->option('factory')) { */
-        /*     $this->createFactory(); */
-        /* } */
+        if ($this->option('factory')) {
+            $this->createFactory();
+        }
 
         /* if ($this->option('migration')) { */
         /*     $this->createMigration(); */
@@ -96,6 +96,7 @@
             ['request', 'r', InputOption::VALUE_NONE, 'Generate base request classes for the table.'],
             ['view', '', InputOption::VALUE_NONE, 'Generate base views for the table.'],
             ['route', 'w', InputOption::VALUE_NONE, 'Generate base routes classes for the table.'],
+            ['factory', 'f', InputOption::VALUE_NONE, 'Generate base factory classes for the table.'],
         ]);
     }
 
@@ -130,4 +131,9 @@
     {
         $this->call('mf:route', ['table' => $this->getTableInput(), '--fresh' => $this->option('fresh')]);
     }
+
+    protected function createFactory()
+    {
+        $this->call('mf:factory', ['table' => $this->getTableInput(), '--fresh' => $this->option('fresh')]);
+    }
 }
--- a/src/Generator/Model/stubs/model.stub	Thu Sep 11 21:25:51 2025 -0400
+++ b/src/Generator/Model/stubs/model.stub	Sat Sep 20 17:14:29 2025 -0400
@@ -7,6 +7,7 @@
 
 class {{ class }} extends BaseModel
 {
+		/** @use HasFactory<\Database\Factories\{{ class }}Factory> */
     //use HasFactory;
     use SoftDeletes;