Mercurial > packages > magicforger
annotate 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 |
| rev | line source |
|---|---|
| 4 | 1 <?php |
| 2 | |
|
19
19b7a8de0019
updating namespace from typo
Luka Sitas <sitas.luka.97@gmail.com>
parents:
14
diff
changeset
|
3 namespace Wizard\MagicForger\Generator\Model; |
| 4 | 4 |
| 5 use Symfony\Component\Console\Attribute\AsCommand; | |
|
19
19b7a8de0019
updating namespace from typo
Luka Sitas <sitas.luka.97@gmail.com>
parents:
14
diff
changeset
|
6 use Wizard\MagicForger\Generator\BaseGenerator; |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
7 use Wizard\MagicForger\Helpers\RelationshipNavigator; |
| 4 | 8 |
| 9 #[AsCommand(name: 'mf:model')] | |
| 10 class ModelGenerator extends BaseGenerator | |
| 11 { | |
| 12 /** | |
| 13 * The name and signature of the console command. | |
| 14 * | |
| 15 * @var string | |
| 16 */ | |
| 17 protected $name = 'mf:model'; | |
| 18 | |
| 19 /** | |
| 20 * The console command description. | |
| 21 * | |
| 22 * @var string | |
| 23 */ | |
| 24 protected $description = 'Generates the Model File for a table.'; | |
| 25 | |
| 26 /** | |
| 27 * The type of class being generated. | |
| 28 * | |
| 29 * @var string | |
| 30 */ | |
| 31 protected $type = 'Model'; | |
| 32 | |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
33 protected static $cached_snippets = []; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
34 |
| 4 | 35 /** |
| 36 * Execute the console command. | |
| 37 */ | |
| 38 public function handle() | |
| 39 { | |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
40 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
41 $belongs_to_many_relations = RelationshipNavigator::getRelations($this->getTableInput()); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
42 dd($belongs_to_many_relations); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
43 echo $this->renderBelongsToMany($belongs_to_many_relations['hasManyThrough'][0]); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
44 dd('here'); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
45 /* parent::handle(); */ |
| 4 | 46 } |
| 47 | |
| 48 /** | |
| 49 * Get the stub file for the generator. | |
| 50 * | |
| 51 * @return string | |
| 52 */ | |
| 53 protected function getStub() | |
| 54 { | |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
55 if (! is_null(RelationshipNavigator::isPivot($this->getCurrentTable()))) { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
56 return $this->resolveStubPath('/stubs/model.pivot.stub'); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
57 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
58 |
| 4 | 59 return $this->resolveStubPath('/stubs/model.stub'); |
| 60 } | |
| 61 | |
| 62 /** | |
| 63 * Resolve the fully-qualified path to the stub. | |
| 64 * | |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
65 * @param string $stub |
| 4 | 66 * @return string |
| 67 */ | |
| 68 protected function resolveStubPath($stub) | |
| 69 { | |
| 70 return is_file($customPath = $this->laravel->basePath(trim($stub, '/'))) | |
| 71 ? $customPath | |
| 72 : __DIR__.$stub; | |
| 73 } | |
| 74 | |
| 75 protected function getClassName($name) | |
| 76 { | |
| 77 return $this->model_name($name); | |
| 78 } | |
| 79 | |
| 80 /** | |
| 81 * Get the stub file for the generator. | |
| 82 * | |
| 83 * @return string | |
| 84 */ | |
| 85 protected function getPath($name = null) | |
| 86 { | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
87 return str_replace(['App\\', '\\'], ['app/', '/'], $this->getModelNamespace().'/'.$this->model_name($this->getTableInput()).'.php'); |
| 4 | 88 } |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
89 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
90 protected function getSnippet($snippet_name) |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
91 { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
92 if (! isset($cached_snippets[$snippet_name])) { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
93 $cached_snippets[$snippet_name] = $this->files->get( |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
94 $this->resolveStubPath("/snippets/$snippet_name.stub")); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
95 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
96 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
97 return $cached_snippets[$snippet_name]; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
98 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
99 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
100 protected function gatherRelations() { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
101 $relations = RelationshipNavigator::getRelations($this->getCurrentTable()); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
102 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
103 return renderRelations($relations); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
104 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
105 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
106 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
107 protected function renderRelations($relations) { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
108 $renders = [ |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
109 'belongsTo' => [], |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
110 'hasMany' => [], |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
111 'belongsToMany' => [], |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
112 ]; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
113 foreach($relations['belongsTo'] as $belongs_to_relation) { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
114 $renders['belongsTo'] = $this->renderBelongsTo($belongs_to_relation); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
115 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
116 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
117 foreach($relations['hasMany'] as $has_many_relation) { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
118 $renders['hasMany'] = $this->renderHasMany($has_many_relation); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
119 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
120 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
121 foreach($relations['belongsToMany'] as $belongs_to_many_relation) { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
122 $renders['belongsToMany'] = $this->renderBelongsToMany($belongs_to_many_relation); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
123 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
124 return $renders; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
125 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
126 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
127 protected function renderBelongsTo($relationship) |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
128 { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
129 $snippet = $this->getSnippet('belongs_to_relation'); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
130 $relationName = Str::singular($relationship['table']); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
131 $relatedModel = $this->getClassName($relationship['table']); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
132 $columnName = $relationship['column']; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
133 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
134 // Replace placeholders with actual values |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
135 $string = str_replace( |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
136 ['{{relationName}}', '{{relatedModel}}', '{{pivotTable}}', '{{foreignPivotKey}}', '{{relatedPivotKey}}'], |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
137 [$relationName, $relatedModel, $pivotTable, $foreignPivotKey, $relatedPivotKey], |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
138 $snippet |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
139 ); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
140 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
141 return $string; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
142 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
143 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
144 protected function renderBelongsToMany($relationship) |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
145 { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
146 $snippet = $this->getSnippet('belongs_to_many_relation'); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
147 $relationName = $relationship['table']; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
148 $relatedModel = $this->getClassName($relationship['table']); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
149 $pivotTable = $relationship['through']['table']; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
150 $foreignPivotKey = $relationship['through']['external_column']; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
151 $relatedPivotKey = $relationship['through']['internal_column']; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
152 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
153 // Replace placeholders with actual values |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
154 $string = str_replace( |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
155 ['{{relationName}}', '{{relatedModel}}', '{{pivotTable}}', '{{foreignPivotKey}}', '{{relatedPivotKey}}'], |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
156 [$relationName, $relatedModel, $pivotTable, $foreignPivotKey, $relatedPivotKey], |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
157 $snippet |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
158 ); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
159 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
160 return $string; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
161 } |
| 4 | 162 } |
