Mercurial > packages > magicforger
comparison src/Generator/Model/ModelGenerator.php @ 24:31109c61ce02 codex
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
| author | Luka Sitas <sitas.luka.97@gmail.com> |
|---|---|
| date | Tue, 22 Apr 2025 21:37:44 -0400 |
| parents | 827efbf4d73c |
| children | 555bfaa500ac |
comparison
equal
deleted
inserted
replaced
| 23:827efbf4d73c | 24:31109c61ce02 |
|---|---|
| 3 namespace Wizard\MagicForger\Generator\Model; | 3 namespace Wizard\MagicForger\Generator\Model; |
| 4 | 4 |
| 5 use Symfony\Component\Console\Attribute\AsCommand; | 5 use Symfony\Component\Console\Attribute\AsCommand; |
| 6 use Wizard\MagicForger\Generator\BaseGenerator; | 6 use Wizard\MagicForger\Generator\BaseGenerator; |
| 7 use Wizard\MagicForger\Helpers\RelationshipNavigator; | 7 use Wizard\MagicForger\Helpers\RelationshipNavigator; |
| 8 use Illuminate\Support\Str; | |
| 8 | 9 |
| 9 #[AsCommand(name: 'mf:model')] | 10 #[AsCommand(name: 'mf:model')] |
| 10 class ModelGenerator extends BaseGenerator | 11 class ModelGenerator extends BaseGenerator |
| 11 { | 12 { |
| 12 /** | 13 /** |
| 32 | 33 |
| 33 protected static $cached_snippets = []; | 34 protected static $cached_snippets = []; |
| 34 | 35 |
| 35 /** | 36 /** |
| 36 * Execute the console command. | 37 * Execute the console command. |
| 38 * | |
| 39 * @return mixed | |
| 37 */ | 40 */ |
| 38 public function handle() | 41 public function handle() |
| 39 { | 42 { |
| 40 | 43 // Delegate to parent handler (includes replacements and insertions) |
| 41 $belongs_to_many_relations = RelationshipNavigator::getRelations($this->getTableInput()); | 44 return parent::handle(); |
| 42 dd($belongs_to_many_relations); | |
| 43 echo $this->renderBelongsToMany($belongs_to_many_relations['hasManyThrough'][0]); | |
| 44 dd('here'); | |
| 45 /* parent::handle(); */ | |
| 46 } | 45 } |
| 47 | 46 |
| 48 /** | 47 /** |
| 49 * Get the stub file for the generator. | 48 * Get the stub file for the generator. |
| 50 * | 49 * |
| 87 return str_replace(['App\\', '\\'], ['app/', '/'], $this->getModelNamespace().'/'.$this->model_name($this->getTableInput()).'.php'); | 86 return str_replace(['App\\', '\\'], ['app/', '/'], $this->getModelNamespace().'/'.$this->model_name($this->getTableInput()).'.php'); |
| 88 } | 87 } |
| 89 | 88 |
| 90 protected function getSnippet($snippet_name) | 89 protected function getSnippet($snippet_name) |
| 91 { | 90 { |
| 92 if (! isset($cached_snippets[$snippet_name])) { | 91 // Cache snippet contents to avoid re-reading files |
| 93 $cached_snippets[$snippet_name] = $this->files->get( | 92 if (! isset(self::$cached_snippets[$snippet_name])) { |
| 94 $this->resolveStubPath("/snippets/$snippet_name.stub")); | 93 self::$cached_snippets[$snippet_name] = $this->files->get( |
| 95 } | 94 $this->resolveStubPath("/snippets/$snippet_name.stub")); |
| 96 | 95 } |
| 97 return $cached_snippets[$snippet_name]; | 96 |
| 97 return self::$cached_snippets[$snippet_name]; | |
| 98 } | 98 } |
| 99 | 99 |
| 100 protected function gatherRelations() { | 100 protected function gatherRelations() { |
| 101 $relations = RelationshipNavigator::getRelations($this->getCurrentTable()); | 101 $relations = RelationshipNavigator::getRelations($this->getCurrentTable()); |
| 102 | 102 |
| 108 $renders = [ | 108 $renders = [ |
| 109 'belongsTo' => [], | 109 'belongsTo' => [], |
| 110 'hasMany' => [], | 110 'hasMany' => [], |
| 111 'belongsToMany' => [], | 111 'belongsToMany' => [], |
| 112 ]; | 112 ]; |
| 113 foreach($relations['belongsTo'] as $belongs_to_relation) { | 113 // Render belongsTo relations |
| 114 $renders['belongsTo'] = $this->renderBelongsTo($belongs_to_relation); | 114 foreach (($relations['belongsTo'] ?? []) as $relation) { |
| 115 } | 115 $renders['belongsTo'][] = $this->renderBelongsTo($relation); |
| 116 | 116 } |
| 117 foreach($relations['hasMany'] as $has_many_relation) { | 117 |
| 118 $renders['hasMany'] = $this->renderHasMany($has_many_relation); | 118 // Render hasMany relations |
| 119 } | 119 foreach (($relations['hasMany'] ?? []) as $relation) { |
| 120 | 120 $renders['hasMany'][] = $this->renderHasMany($relation); |
| 121 foreach($relations['belongsToMany'] as $belongs_to_many_relation) { | 121 } |
| 122 $renders['belongsToMany'] = $this->renderBelongsToMany($belongs_to_many_relation); | 122 |
| 123 // Render belongsToMany (many-to-many) via hasManyThrough pivot relations | |
| 124 foreach (($relations['hasManyThrough'] ?? []) as $relation) { | |
| 125 $renders['belongsToMany'][] = $this->renderBelongsToMany($relation); | |
| 123 } | 126 } |
| 124 return $renders; | 127 return $renders; |
| 125 } | 128 } |
| 126 | 129 |
| 127 protected function renderBelongsTo($relationship) | 130 protected function renderBelongsTo($relationship) |
| 128 { | 131 { |
| 129 $snippet = $this->getSnippet('belongs_to_relation'); | 132 $snippet = $this->getSnippet('belongs_to_relation'); |
| 130 $relationName = Str::singular($relationship['table']); | 133 $relationName = Str::singular($relationship['table']); |
| 131 $relatedModel = $this->getClassName($relationship['table']); | 134 $relatedModel = $this->getClassName($relationship['table']); |
| 132 $columnName = $relationship['column']; | 135 $columnName = $relationship['column']; |
| 136 | |
| 137 // Replace placeholders with actual values | |
| 138 $string = str_replace( | |
| 139 ['{{relationName}}', '{{relatedModel}}', '{{columnName}}'], | |
| 140 [$relationName, $relatedModel, $columnName], | |
| 141 $snippet | |
| 142 ); | |
| 143 | |
| 144 return $string; | |
| 145 } | |
| 146 | |
| 147 /** | |
| 148 * Render a hasMany relation. | |
| 149 * | |
| 150 * @param array $relationship | |
| 151 * @return string | |
| 152 */ | |
| 153 protected function renderHasMany($relationship) | |
| 154 { | |
| 155 $snippet = $this->getSnippet('has_many_relation'); | |
| 156 // Method name uses camel case for plural relation | |
| 157 $relationName = Str::camel($relationship['table']); | |
| 158 $relatedModel = $this->getClassName($relationship['table']); | |
| 159 $columnName = $relationship['column']; | |
| 160 | |
| 161 // Replace placeholders with actual values | |
| 162 $string = str_replace( | |
| 163 ['{{relationName}}', '{{relatedModel}}', '{{columnName}}'], | |
| 164 [$relationName, $relatedModel, $columnName], | |
| 165 $snippet | |
| 166 ); | |
| 167 | |
| 168 return $string; | |
| 169 } | |
| 170 | |
| 171 protected function renderBelongsToMany($relationship) | |
| 172 { | |
| 173 $snippet = $this->getSnippet('belongs_to_many_relation'); | |
| 174 $relationName = $relationship['table']; | |
| 175 $relatedModel = $this->getClassName($relationship['table']); | |
| 176 $pivotTable = $relationship['through']['table']; | |
| 177 $foreignPivotKey = $relationship['through']['external_column']; | |
| 178 $relatedPivotKey = $relationship['through']['internal_column']; | |
| 133 | 179 |
| 134 // Replace placeholders with actual values | 180 // Replace placeholders with actual values |
| 135 $string = str_replace( | 181 $string = str_replace( |
| 136 ['{{relationName}}', '{{relatedModel}}', '{{pivotTable}}', '{{foreignPivotKey}}', '{{relatedPivotKey}}'], | 182 ['{{relationName}}', '{{relatedModel}}', '{{pivotTable}}', '{{foreignPivotKey}}', '{{relatedPivotKey}}'], |
| 137 [$relationName, $relatedModel, $pivotTable, $foreignPivotKey, $relatedPivotKey], | 183 [$relationName, $relatedModel, $pivotTable, $foreignPivotKey, $relatedPivotKey], |
| 138 $snippet | 184 $snippet |
| 139 ); | 185 ); |
| 140 | 186 |
| 141 return $string; | 187 return $string; |
| 142 } | 188 } |
| 143 | 189 /** |
| 144 protected function renderBelongsToMany($relationship) | 190 * Get available insertions including model relationships. |
| 145 { | 191 * |
| 146 $snippet = $this->getSnippet('belongs_to_many_relation'); | 192 * @return array |
| 147 $relationName = $relationship['table']; | 193 */ |
| 148 $relatedModel = $this->getClassName($relationship['table']); | 194 public function get_available_inserts(): array |
| 149 $pivotTable = $relationship['through']['table']; | 195 { |
| 150 $foreignPivotKey = $relationship['through']['external_column']; | 196 // Merge parent insertions (attributes, fillable, etc.) |
| 151 $relatedPivotKey = $relationship['through']['internal_column']; | 197 $inserts = parent::get_available_inserts(); |
| 152 | 198 |
| 153 // Replace placeholders with actual values | 199 // Gather and render relationships for this model |
| 154 $string = str_replace( | 200 $relations = RelationshipNavigator::getRelations($this->getCurrentTable()); |
| 155 ['{{relationName}}', '{{relatedModel}}', '{{pivotTable}}', '{{foreignPivotKey}}', '{{relatedPivotKey}}'], | 201 $rendered = $this->renderRelations($relations); |
| 156 [$relationName, $relatedModel, $pivotTable, $foreignPivotKey, $relatedPivotKey], | 202 |
| 157 $snippet | 203 // Build code blocks for each relation type |
| 158 ); | 204 $belongs = !empty($rendered['belongsTo']) ? implode("\n ", $rendered['belongsTo']) : ''; |
| 159 | 205 $hasMany = !empty($rendered['hasMany']) ? implode("\n ", $rendered['hasMany']) : ''; |
| 160 return $string; | 206 $belongsMany = !empty($rendered['belongsToMany']) ? implode("\n ", $rendered['belongsToMany']) : ''; |
| 207 | |
| 208 // Assign to stub placeholders | |
| 209 $inserts['# {{ belongs_to_relationships }}'] = $belongs; | |
| 210 $inserts['# {{ has_many_relationships }}'] = $hasMany; | |
| 211 $inserts['# {{ has_many_through_relationships }}'] = $belongsMany; | |
| 212 | |
| 213 return $inserts; | |
| 161 } | 214 } |
| 162 } | 215 } |
