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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
1 <?php
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
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
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
4
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
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
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
8
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
9 #[AsCommand(name: 'mf:model')]
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
10 class ModelGenerator extends BaseGenerator
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
11 {
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
12 /**
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
13 * The name and signature of the console command.
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
14 *
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
15 * @var string
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
16 */
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
17 protected $name = 'mf:model';
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
18
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
19 /**
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
20 * The console command description.
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
21 *
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
22 * @var string
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
23 */
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
24 protected $description = 'Generates the Model File for a table.';
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
25
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
26 /**
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
27 * The type of class being generated.
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
28 *
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
29 * @var string
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
30 */
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
31 protected $type = 'Model';
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
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
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
35 /**
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
36 * Execute the console command.
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
37 */
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
38 public function handle()
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
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
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
46 }
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
47
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
48 /**
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
49 * Get the stub file for the generator.
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
50 *
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
51 * @return string
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
52 */
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
53 protected function getStub()
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
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
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
59 return $this->resolveStubPath('/stubs/model.stub');
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
60 }
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
61
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
62 /**
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
63 * Resolve the fully-qualified path to the stub.
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
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
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
66 * @return string
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
67 */
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
68 protected function resolveStubPath($stub)
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
69 {
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
70 return is_file($customPath = $this->laravel->basePath(trim($stub, '/')))
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
71 ? $customPath
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
72 : __DIR__.$stub;
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
73 }
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
74
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
75 protected function getClassName($name)
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
76 {
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
77 return $this->model_name($name);
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
78 }
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
79
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
80 /**
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
81 * Get the stub file for the generator.
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
82 *
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
83 * @return string
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
84 */
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
85 protected function getPath($name = null)
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
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
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
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
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
162 }