Mercurial > packages > magicforger
annotate src/Generator/Model/ModelGenerator.php @ 29:010ace248d14 codex
Added support for filters, not fully there with relations or anything like that but it's a start
| author | Luka Sitas <sitas.luka.97@gmail.com> |
|---|---|
| date | Mon, 09 Jun 2025 20:51:04 -0400 |
| parents | f88d2d5dee30 |
| children |
| 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 |
|
29
010ace248d14
Added support for filters, not fully there with relations or anything like that but it's a start
Luka Sitas <sitas.luka.97@gmail.com>
parents:
28
diff
changeset
|
92 return $relations; |
|
23
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 |
|
28
f88d2d5dee30
Updated inputs and routes
Luka Sitas <sitas.luka.97@gmail.com>
parents:
26
diff
changeset
|
96 protected function renderFilters() { |
|
f88d2d5dee30
Updated inputs and routes
Luka Sitas <sitas.luka.97@gmail.com>
parents:
26
diff
changeset
|
97 $insert = ''; |
|
f88d2d5dee30
Updated inputs and routes
Luka Sitas <sitas.luka.97@gmail.com>
parents:
26
diff
changeset
|
98 foreach ($this->get_columns() as $column) { |
|
f88d2d5dee30
Updated inputs and routes
Luka Sitas <sitas.luka.97@gmail.com>
parents:
26
diff
changeset
|
99 if (in_array($column['name'], $this->columns_to_ignore)) { |
|
f88d2d5dee30
Updated inputs and routes
Luka Sitas <sitas.luka.97@gmail.com>
parents:
26
diff
changeset
|
100 continue; |
|
f88d2d5dee30
Updated inputs and routes
Luka Sitas <sitas.luka.97@gmail.com>
parents:
26
diff
changeset
|
101 } |
|
29
010ace248d14
Added support for filters, not fully there with relations or anything like that but it's a start
Luka Sitas <sitas.luka.97@gmail.com>
parents:
28
diff
changeset
|
102 $snippet = $this->getSnippet('filter'); |
|
010ace248d14
Added support for filters, not fully there with relations or anything like that but it's a start
Luka Sitas <sitas.luka.97@gmail.com>
parents:
28
diff
changeset
|
103 $tableName = $this->getCurrentTable(); |
|
010ace248d14
Added support for filters, not fully there with relations or anything like that but it's a start
Luka Sitas <sitas.luka.97@gmail.com>
parents:
28
diff
changeset
|
104 $value = 'value'; // TODO: this should be determined based on column type |
|
010ace248d14
Added support for filters, not fully there with relations or anything like that but it's a start
Luka Sitas <sitas.luka.97@gmail.com>
parents:
28
diff
changeset
|
105 $columnName = $column['name']; |
|
010ace248d14
Added support for filters, not fully there with relations or anything like that but it's a start
Luka Sitas <sitas.luka.97@gmail.com>
parents:
28
diff
changeset
|
106 $columnDisplay = Str::headline($columnName); |
|
010ace248d14
Added support for filters, not fully there with relations or anything like that but it's a start
Luka Sitas <sitas.luka.97@gmail.com>
parents:
28
diff
changeset
|
107 |
|
010ace248d14
Added support for filters, not fully there with relations or anything like that but it's a start
Luka Sitas <sitas.luka.97@gmail.com>
parents:
28
diff
changeset
|
108 // Replace placeholders with actual values |
|
010ace248d14
Added support for filters, not fully there with relations or anything like that but it's a start
Luka Sitas <sitas.luka.97@gmail.com>
parents:
28
diff
changeset
|
109 $string = str_replace( |
|
010ace248d14
Added support for filters, not fully there with relations or anything like that but it's a start
Luka Sitas <sitas.luka.97@gmail.com>
parents:
28
diff
changeset
|
110 ['{{value}}', '{{columnDisplay}}', '{{tableName}}', '{{columnName}}'], |
|
010ace248d14
Added support for filters, not fully there with relations or anything like that but it's a start
Luka Sitas <sitas.luka.97@gmail.com>
parents:
28
diff
changeset
|
111 [$value, $columnDisplay, $tableName, $columnName], |
|
010ace248d14
Added support for filters, not fully there with relations or anything like that but it's a start
Luka Sitas <sitas.luka.97@gmail.com>
parents:
28
diff
changeset
|
112 $snippet |
|
010ace248d14
Added support for filters, not fully there with relations or anything like that but it's a start
Luka Sitas <sitas.luka.97@gmail.com>
parents:
28
diff
changeset
|
113 ); |
|
010ace248d14
Added support for filters, not fully there with relations or anything like that but it's a start
Luka Sitas <sitas.luka.97@gmail.com>
parents:
28
diff
changeset
|
114 $insert .= sprintf("%s", $string); |
|
28
f88d2d5dee30
Updated inputs and routes
Luka Sitas <sitas.luka.97@gmail.com>
parents:
26
diff
changeset
|
115 } |
|
f88d2d5dee30
Updated inputs and routes
Luka Sitas <sitas.luka.97@gmail.com>
parents:
26
diff
changeset
|
116 |
|
29
010ace248d14
Added support for filters, not fully there with relations or anything like that but it's a start
Luka Sitas <sitas.luka.97@gmail.com>
parents:
28
diff
changeset
|
117 return $insert; |
|
28
f88d2d5dee30
Updated inputs and routes
Luka Sitas <sitas.luka.97@gmail.com>
parents:
26
diff
changeset
|
118 } |
|
f88d2d5dee30
Updated inputs and routes
Luka Sitas <sitas.luka.97@gmail.com>
parents:
26
diff
changeset
|
119 |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
120 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
|
121 $renders = [ |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
122 'belongsTo' => [], |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
123 'hasMany' => [], |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
124 'belongsToMany' => [], |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
125 ]; |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
126 // 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
|
127 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
|
128 $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
|
129 } |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
130 |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
131 // 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
|
132 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
|
133 $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
|
134 } |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
135 |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
136 // 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
|
137 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
|
138 $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
|
139 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
140 return $renders; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
141 } |
|
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 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
|
144 { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
145 $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
|
146 $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
|
147 $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
|
148 $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
|
149 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
150 // 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
|
151 $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
|
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], |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
154 $snippet |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
155 ); |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
156 |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
157 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
|
158 } |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
159 |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
160 /** |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
161 * 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
|
162 * |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
163 * @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
|
164 * @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
|
165 */ |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
166 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
|
167 { |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
168 $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
|
169 // 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
|
170 $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
|
171 $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
|
172 $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
|
173 |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
174 // 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
|
175 $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
|
176 ['{{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
|
177 [$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
|
178 $snippet |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
179 ); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
180 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
181 return $string; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
182 } |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
183 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
184 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
|
185 { |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
186 $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
|
187 $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
|
188 $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
|
189 $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
|
190 $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
|
191 $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
|
192 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
193 // 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
|
194 $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
|
195 ['{{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
|
196 [$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
|
197 $snippet |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
198 ); |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
199 |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
200 return $string; |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
201 } |
|
28
f88d2d5dee30
Updated inputs and routes
Luka Sitas <sitas.luka.97@gmail.com>
parents:
26
diff
changeset
|
202 |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
203 /** |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
204 * 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
|
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 array |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
207 */ |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
208 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
|
209 { |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
210 // 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
|
211 $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
|
212 |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
213 // Gather and render relationships for this model |
|
28
f88d2d5dee30
Updated inputs and routes
Luka Sitas <sitas.luka.97@gmail.com>
parents:
26
diff
changeset
|
214 $relations = $this->gatherRelations(); |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
215 $rendered = $this->renderRelations($relations); |
|
28
f88d2d5dee30
Updated inputs and routes
Luka Sitas <sitas.luka.97@gmail.com>
parents:
26
diff
changeset
|
216 $filters = $this->renderFilters(); |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
217 |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
218 // 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
|
219 $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
|
220 $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
|
221 $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
|
222 |
|
26
555bfaa500ac
Big update for view creation based on the column type.
Luka Sitas <sitas.luka.97@gmail.com>
parents:
24
diff
changeset
|
223 // 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
|
224 $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
|
225 |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
226 // 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
|
227 $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
|
228 $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
|
229 $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
|
230 $inserts['# {{ defaultRelationsInsertPoint }}'] = $default_relations; |
|
28
f88d2d5dee30
Updated inputs and routes
Luka Sitas <sitas.luka.97@gmail.com>
parents:
26
diff
changeset
|
231 $inserts['# {{ defaultFiltersInsertPoint }}'] = $filters; |
|
24
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
232 |
|
31109c61ce02
Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents:
23
diff
changeset
|
233 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
|
234 } |
| 4 | 235 } |
