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
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;
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
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
9
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
10 #[AsCommand(name: 'mf:model')]
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
11 class ModelGenerator extends BaseGenerator
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
12 {
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
13 /**
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
14 * The name and signature of the console command.
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
15 *
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
16 * @var string
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
17 */
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
18 protected $name = 'mf:model';
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
19
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
20 /**
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
21 * The console command description.
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
22 *
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
23 * @var string
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
24 */
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
25 protected $description = 'Generates the Model File for a table.';
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
26
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
27 /**
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
28 * The type of class being generated.
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
29 *
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
30 * @var string
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
31 */
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
32 protected $type = 'Model';
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
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
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
36 /**
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
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
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
40 */
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
41 public function handle()
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
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
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
45 }
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 * Get the stub file for the generator.
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
49 *
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
50 * @return string
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
51 */
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
52 protected function getStub()
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
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
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
58 return $this->resolveStubPath('/stubs/model.stub');
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
59 }
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 * Resolve the fully-qualified path to the stub.
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
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
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
65 * @return string
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
66 */
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
67 protected function resolveStubPath($stub)
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
68 {
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
69 return is_file($customPath = $this->laravel->basePath(trim($stub, '/')))
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
70 ? $customPath
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
71 : __DIR__.$stub;
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
72 }
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
73
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
74 protected function getClassName($name)
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
75 {
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
76 return $this->model_name($name);
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
77 }
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 * Get the stub file for the generator.
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
81 *
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
82 * @return string
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
83 */
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
84 protected function getPath($name = null)
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
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
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
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
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
208 }