Mercurial > packages > magicforger
annotate src/Generator/Model/ModelGenerator.php @ 26:555bfaa500ac codex
Big update for view creation based on the column type.
| author | Luka Sitas <sitas.luka.97@gmail.com> |
|---|---|
| date | Thu, 15 May 2025 21:50:38 -0400 |
| parents | 31109c61ce02 |
| children | f88d2d5dee30 |
| 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; |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
8 use Illuminate\Support\Str; |
| 4 | 9 |
| 10 #[AsCommand(name: 'mf:model')] | |
| 11 class ModelGenerator extends BaseGenerator | |
| 12 { | |
| 13 /** | |
| 14 * The name and signature of the console command. | |
| 15 * | |
| 16 * @var string | |
| 17 */ | |
| 18 protected $name = 'mf:model'; | |
| 19 | |
| 20 /** | |
| 21 * The console command description. | |
| 22 * | |
| 23 * @var string | |
| 24 */ | |
| 25 protected $description = 'Generates the Model File for a table.'; | |
| 26 | |
| 27 /** | |
| 28 * The type of class being generated. | |
| 29 * | |
| 30 * @var string | |
| 31 */ | |
| 32 protected $type = 'Model'; | |
| 33 | |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
34 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
|
35 |
| 4 | 36 /** |
| 37 * Execute the console command. | |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
38 * |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
39 * @return mixed |
| 4 | 40 */ |
| 41 public function handle() | |
| 42 { | |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
43 // Delegate to parent handler (includes replacements and insertions) |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
44 return parent::handle(); |
| 4 | 45 } |
| 46 | |
| 47 /** | |
| 48 * Get the stub file for the generator. | |
| 49 * | |
| 50 * @return string | |
| 51 */ | |
| 52 protected function getStub() | |
| 53 { | |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
54 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
|
55 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
|
56 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
57 |
| 4 | 58 return $this->resolveStubPath('/stubs/model.stub'); |
| 59 } | |
| 60 | |
| 61 /** | |
| 62 * Resolve the fully-qualified path to the stub. | |
| 63 * | |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
64 * @param string $stub |
| 4 | 65 * @return string |
| 66 */ | |
| 67 protected function resolveStubPath($stub) | |
| 68 { | |
| 69 return is_file($customPath = $this->laravel->basePath(trim($stub, '/'))) | |
| 70 ? $customPath | |
| 71 : __DIR__.$stub; | |
| 72 } | |
| 73 | |
| 74 protected function getClassName($name) | |
| 75 { | |
| 76 return $this->model_name($name); | |
| 77 } | |
| 78 | |
| 79 /** | |
| 80 * Get the stub file for the generator. | |
| 81 * | |
| 82 * @return string | |
| 83 */ | |
| 84 protected function getPath($name = null) | |
| 85 { | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
86 return str_replace(['App\\', '\\'], ['app/', '/'], $this->getModelNamespace().'/'.$this->model_name($this->getTableInput()).'.php'); |
| 4 | 87 } |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
88 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
89 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
|
90 $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
|
91 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
92 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
|
93 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
94 } |
|
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 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
|
97 $renders = [ |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
98 'belongsTo' => [], |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
99 'hasMany' => [], |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
100 'belongsToMany' => [], |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
101 ]; |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
102 // Render belongsTo relations |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
103 foreach (($relations['belongsTo'] ?? []) as $relation) { |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
104 $renders['belongsTo'][] = $this->renderBelongsTo($relation); |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
105 } |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
106 |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
107 // Render hasMany relations |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
108 foreach (($relations['hasMany'] ?? []) as $relation) { |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
109 $renders['hasMany'][] = $this->renderHasMany($relation); |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
110 } |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
111 |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
112 // Render belongsToMany (many-to-many) via hasManyThrough pivot relations |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
113 foreach (($relations['hasManyThrough'] ?? []) as $relation) { |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
114 $renders['belongsToMany'][] = $this->renderBelongsToMany($relation); |
|
23
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 return $renders; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
117 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
118 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
119 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
|
120 { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
121 $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
|
122 $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
|
123 $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
|
124 $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
|
125 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
126 // 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
|
127 $string = str_replace( |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
128 ['{{relationName}}', '{{relatedModel}}', '{{columnName}}'], |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
129 [$relationName, $relatedModel, $columnName], |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
130 $snippet |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
131 ); |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
132 |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
133 return $string; |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
134 } |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
135 |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
136 /** |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
137 * Render a hasMany relation. |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
138 * |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
139 * @param array $relationship |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
140 * @return string |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
141 */ |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
142 protected function renderHasMany($relationship) |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
143 { |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
144 $snippet = $this->getSnippet('has_many_relation'); |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
145 // Method name uses camel case for plural relation |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
146 $relationName = Str::camel($relationship['table']); |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
147 $relatedModel = $this->getClassName($relationship['table']); |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
148 $columnName = $relationship['column']; |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
149 |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
150 // Replace placeholders with actual values |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
151 $string = str_replace( |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
152 ['{{relationName}}', '{{relatedModel}}', '{{columnName}}'], |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
153 [$relationName, $relatedModel, $columnName], |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
154 $snippet |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
155 ); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
156 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
157 return $string; |
|
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 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
|
161 { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
162 $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
|
163 $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
|
164 $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
|
165 $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
|
166 $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
|
167 $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
|
168 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
169 // 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
|
170 $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
|
171 ['{{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
|
172 [$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
|
173 $snippet |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
174 ); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
175 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
176 return $string; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
177 } |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
178 /** |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
179 * Get available insertions including model relationships. |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
180 * |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
181 * @return array |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
182 */ |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
183 public function get_available_inserts(): array |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
184 { |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
185 // Merge parent insertions (attributes, fillable, etc.) |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
186 $inserts = parent::get_available_inserts(); |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
187 |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
188 // Gather and render relationships for this model |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
189 $relations = RelationshipNavigator::getRelations($this->getCurrentTable()); |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
190 $rendered = $this->renderRelations($relations); |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
191 |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
192 // Build code blocks for each relation type |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
193 $belongs = !empty($rendered['belongsTo']) ? implode("\n ", $rendered['belongsTo']) : ''; |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
194 $hasMany = !empty($rendered['hasMany']) ? implode("\n ", $rendered['hasMany']) : ''; |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
195 $belongsMany = !empty($rendered['belongsToMany']) ? implode("\n ", $rendered['belongsToMany']) : ''; |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
196 |
|
26
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
197 // Default relations are based on the belongsTo relationship |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
198 $default_relations = implode(", \n", array_map(function ($rel) {return '\'' . Str::singular($rel['table']) . '\''; }, $relations['belongsTo'])); |
|
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
199 |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
200 // Assign to stub placeholders |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
201 $inserts['# {{ belongs_to_relationships }}'] = $belongs; |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
202 $inserts['# {{ has_many_relationships }}'] = $hasMany; |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
203 $inserts['# {{ has_many_through_relationships }}'] = $belongsMany; |
|
26
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
204 $inserts['# {{ defaultRelationsInsertPoint }}'] = $default_relations; |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
205 |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
206 return $inserts; |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
207 } |
| 4 | 208 } |
