comparison 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
comparison
equal deleted inserted replaced
22:ee8ef14e158d 23:827efbf4d73c
2 2
3 namespace Wizard\MagicForger\Generator\Model; 3 namespace Wizard\MagicForger\Generator\Model;
4 4
5 use Symfony\Component\Console\Attribute\AsCommand; 5 use Symfony\Component\Console\Attribute\AsCommand;
6 use Wizard\MagicForger\Generator\BaseGenerator; 6 use Wizard\MagicForger\Generator\BaseGenerator;
7 use Wizard\MagicForger\Helpers\RelationshipNavigator;
7 8
8 #[AsCommand(name: 'mf:model')] 9 #[AsCommand(name: 'mf:model')]
9 class ModelGenerator extends BaseGenerator 10 class ModelGenerator extends BaseGenerator
10 { 11 {
11 /** 12 /**
27 * 28 *
28 * @var string 29 * @var string
29 */ 30 */
30 protected $type = 'Model'; 31 protected $type = 'Model';
31 32
33 protected static $cached_snippets = [];
34
32 /** 35 /**
33 * Execute the console command. 36 * Execute the console command.
34 */ 37 */
35 public function handle() 38 public function handle()
36 { 39 {
37 parent::handle(); 40
41 $belongs_to_many_relations = RelationshipNavigator::getRelations($this->getTableInput());
42 dd($belongs_to_many_relations);
43 echo $this->renderBelongsToMany($belongs_to_many_relations['hasManyThrough'][0]);
44 dd('here');
45 /* parent::handle(); */
38 } 46 }
39 47
40 /** 48 /**
41 * Get the stub file for the generator. 49 * Get the stub file for the generator.
42 * 50 *
43 * @return string 51 * @return string
44 */ 52 */
45 protected function getStub() 53 protected function getStub()
46 { 54 {
55 if (! is_null(RelationshipNavigator::isPivot($this->getCurrentTable()))) {
56 return $this->resolveStubPath('/stubs/model.pivot.stub');
57 }
58
47 return $this->resolveStubPath('/stubs/model.stub'); 59 return $this->resolveStubPath('/stubs/model.stub');
48 } 60 }
49 61
50 /** 62 /**
51 * Resolve the fully-qualified path to the stub. 63 * Resolve the fully-qualified path to the stub.
52 * 64 *
53 * @param string $stub 65 * @param string $stub
54 *
55 * @return string 66 * @return string
56 */ 67 */
57 protected function resolveStubPath($stub) 68 protected function resolveStubPath($stub)
58 { 69 {
59 return is_file($customPath = $this->laravel->basePath(trim($stub, '/'))) 70 return is_file($customPath = $this->laravel->basePath(trim($stub, '/')))
73 */ 84 */
74 protected function getPath($name = null) 85 protected function getPath($name = null)
75 { 86 {
76 return str_replace(['App\\', '\\'], ['app/', '/'], $this->getModelNamespace().'/'.$this->model_name($this->getTableInput()).'.php'); 87 return str_replace(['App\\', '\\'], ['app/', '/'], $this->getModelNamespace().'/'.$this->model_name($this->getTableInput()).'.php');
77 } 88 }
89
90 protected function getSnippet($snippet_name)
91 {
92 if (! isset($cached_snippets[$snippet_name])) {
93 $cached_snippets[$snippet_name] = $this->files->get(
94 $this->resolveStubPath("/snippets/$snippet_name.stub"));
95 }
96
97 return $cached_snippets[$snippet_name];
98 }
99
100 protected function gatherRelations() {
101 $relations = RelationshipNavigator::getRelations($this->getCurrentTable());
102
103 return renderRelations($relations);
104
105 }
106
107 protected function renderRelations($relations) {
108 $renders = [
109 'belongsTo' => [],
110 'hasMany' => [],
111 'belongsToMany' => [],
112 ];
113 foreach($relations['belongsTo'] as $belongs_to_relation) {
114 $renders['belongsTo'] = $this->renderBelongsTo($belongs_to_relation);
115 }
116
117 foreach($relations['hasMany'] as $has_many_relation) {
118 $renders['hasMany'] = $this->renderHasMany($has_many_relation);
119 }
120
121 foreach($relations['belongsToMany'] as $belongs_to_many_relation) {
122 $renders['belongsToMany'] = $this->renderBelongsToMany($belongs_to_many_relation);
123 }
124 return $renders;
125 }
126
127 protected function renderBelongsTo($relationship)
128 {
129 $snippet = $this->getSnippet('belongs_to_relation');
130 $relationName = Str::singular($relationship['table']);
131 $relatedModel = $this->getClassName($relationship['table']);
132 $columnName = $relationship['column'];
133
134 // Replace placeholders with actual values
135 $string = str_replace(
136 ['{{relationName}}', '{{relatedModel}}', '{{pivotTable}}', '{{foreignPivotKey}}', '{{relatedPivotKey}}'],
137 [$relationName, $relatedModel, $pivotTable, $foreignPivotKey, $relatedPivotKey],
138 $snippet
139 );
140
141 return $string;
142 }
143
144 protected function renderBelongsToMany($relationship)
145 {
146 $snippet = $this->getSnippet('belongs_to_many_relation');
147 $relationName = $relationship['table'];
148 $relatedModel = $this->getClassName($relationship['table']);
149 $pivotTable = $relationship['through']['table'];
150 $foreignPivotKey = $relationship['through']['external_column'];
151 $relatedPivotKey = $relationship['through']['internal_column'];
152
153 // Replace placeholders with actual values
154 $string = str_replace(
155 ['{{relationName}}', '{{relatedModel}}', '{{pivotTable}}', '{{foreignPivotKey}}', '{{relatedPivotKey}}'],
156 [$relationName, $relatedModel, $pivotTable, $foreignPivotKey, $relatedPivotKey],
157 $snippet
158 );
159
160 return $string;
161 }
78 } 162 }