annotate src/Generator/Model/ModelGenerator.php @ 24:31109c61ce02 codex

Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
author Luka Sitas <sitas.luka.97@gmail.com>
date Tue, 22 Apr 2025 21:37:44 -0400
parents 827efbf4d73c
children 555bfaa500ac
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 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
90 {
24
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
91 // Cache snippet contents to avoid re-reading files
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
92 if (! isset(self::$cached_snippets[$snippet_name])) {
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
93 self::$cached_snippets[$snippet_name] = $this->files->get(
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
94 $this->resolveStubPath("/snippets/$snippet_name.stub"));
23
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
24
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
97 return self::$cached_snippets[$snippet_name];
23
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 ];
24
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
113 // 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
114 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
115 $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
116 }
23
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
117
24
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
118 // 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
119 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
120 $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
121 }
23
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
122
24
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
123 // 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
124 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
125 $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
126 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
127 return $renders;
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
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
130 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
131 {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
132 $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
133 $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
134 $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
135 $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
136
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
137 // 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
138 $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
139 ['{{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
140 [$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
141 $snippet
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
142 );
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 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
145 }
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
146
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
147 /**
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
148 * 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
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 * @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
151 * @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
152 */
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
153 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
154 {
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
155 $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
156 // 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
157 $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
158 $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
159 $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
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 // 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
162 $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
163 ['{{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
164 [$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
165 $snippet
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
166 );
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
167
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
168 return $string;
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
169 }
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
170
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
171 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
172 {
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
173 $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
174 $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
175 $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
176 $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
177 $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
178 $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
179
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
180 // 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
181 $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
182 ['{{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
183 [$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
184 $snippet
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
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
187 return $string;
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
188 }
24
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
189 /**
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
190 * 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
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 * @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
193 */
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
194 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
195 {
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
196 // 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
197 $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
198
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
199 // 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
200 $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
201 $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
202
31109c61ce02 Refactor RelationshipNavigator formatting and implement belongsTo, hasMany, belongsToMany injection in ModelGenerator
Luka Sitas <sitas.luka.97@gmail.com>
parents: 23
diff changeset
203 // 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
204 $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
205 $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
206 $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
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 // 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
209 $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
210 $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
211 $inserts['# {{ has_many_through_relationships }}'] = $belongsMany;
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 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
214 }
4
a20439b1c9d3 Added Model generator and other updates.
luka
parents:
diff changeset
215 }