diff src/Generator/Model/ModelGenerator.php @ 23:827efbf4d73c main-dev

Huge changes to the relationships for models and more complex
author Luka Sitas <sitas.luka.97@gmail.com>
date Fri, 11 Apr 2025 20:50:20 -0400
parents 19b7a8de0019
children 31109c61ce02
line wrap: on
line diff
--- a/src/Generator/Model/ModelGenerator.php	Wed Feb 26 20:12:17 2025 -0500
+++ b/src/Generator/Model/ModelGenerator.php	Fri Apr 11 20:50:20 2025 -0400
@@ -4,6 +4,7 @@
 
 use Symfony\Component\Console\Attribute\AsCommand;
 use Wizard\MagicForger\Generator\BaseGenerator;
+use Wizard\MagicForger\Helpers\RelationshipNavigator;
 
 #[AsCommand(name: 'mf:model')]
 class ModelGenerator extends BaseGenerator
@@ -29,12 +30,19 @@
      */
     protected $type = 'Model';
 
+    protected static $cached_snippets = [];
+
     /**
      * Execute the console command.
      */
     public function handle()
     {
-        parent::handle();
+
+        $belongs_to_many_relations = RelationshipNavigator::getRelations($this->getTableInput());
+				dd($belongs_to_many_relations);
+        echo $this->renderBelongsToMany($belongs_to_many_relations['hasManyThrough'][0]);
+        dd('here');
+        /* parent::handle(); */
     }
 
     /**
@@ -44,14 +52,17 @@
      */
     protected function getStub()
     {
+        if (! is_null(RelationshipNavigator::isPivot($this->getCurrentTable()))) {
+            return $this->resolveStubPath('/stubs/model.pivot.stub');
+        }
+
         return $this->resolveStubPath('/stubs/model.stub');
     }
 
     /**
      * Resolve the fully-qualified path to the stub.
      *
-     * @param string $stub
-     *
+     * @param  string  $stub
      * @return string
      */
     protected function resolveStubPath($stub)
@@ -75,4 +86,77 @@
     {
         return str_replace(['App\\', '\\'], ['app/', '/'], $this->getModelNamespace().'/'.$this->model_name($this->getTableInput()).'.php');
     }
+
+    protected function getSnippet($snippet_name)
+    {
+        if (! isset($cached_snippets[$snippet_name])) {
+					$cached_snippets[$snippet_name] = $this->files->get(
+						$this->resolveStubPath("/snippets/$snippet_name.stub"));
+        }
+
+        return $cached_snippets[$snippet_name];
+    }
+
+		protected function gatherRelations() {
+			$relations = RelationshipNavigator::getRelations($this->getCurrentTable());
+
+			return renderRelations($relations);
+			
+		}
+
+		protected function renderRelations($relations) {
+			$renders = [
+				'belongsTo' => [],
+				'hasMany' => [],
+				'belongsToMany' => [],
+			];
+			foreach($relations['belongsTo'] as $belongs_to_relation) {
+				$renders['belongsTo'] = $this->renderBelongsTo($belongs_to_relation);
+			}
+
+			foreach($relations['hasMany'] as $has_many_relation) {
+				$renders['hasMany'] = $this->renderHasMany($has_many_relation);
+			}
+
+			foreach($relations['belongsToMany'] as $belongs_to_many_relation) {
+				$renders['belongsToMany'] = $this->renderBelongsToMany($belongs_to_many_relation);
+			}
+			return $renders;
+		}
+
+    protected function renderBelongsTo($relationship)
+    {
+        $snippet = $this->getSnippet('belongs_to_relation');
+        $relationName = Str::singular($relationship['table']);
+        $relatedModel = $this->getClassName($relationship['table']);
+        $columnName = $relationship['column'];
+
+        // Replace placeholders with actual values
+        $string = str_replace(
+            ['{{relationName}}', '{{relatedModel}}', '{{pivotTable}}', '{{foreignPivotKey}}', '{{relatedPivotKey}}'],
+            [$relationName, $relatedModel, $pivotTable, $foreignPivotKey, $relatedPivotKey],
+            $snippet
+        );
+
+        return $string;
+    }
+
+    protected function renderBelongsToMany($relationship)
+    {
+        $snippet = $this->getSnippet('belongs_to_many_relation');
+        $relationName = $relationship['table'];
+        $relatedModel = $this->getClassName($relationship['table']);
+        $pivotTable = $relationship['through']['table'];
+        $foreignPivotKey = $relationship['through']['external_column'];
+        $relatedPivotKey = $relationship['through']['internal_column'];
+
+        // Replace placeholders with actual values
+        $string = str_replace(
+            ['{{relationName}}', '{{relatedModel}}', '{{pivotTable}}', '{{foreignPivotKey}}', '{{relatedPivotKey}}'],
+            [$relationName, $relatedModel, $pivotTable, $foreignPivotKey, $relatedPivotKey],
+            $snippet
+        );
+
+        return $string;
+    }
 }