comparison src/Generator/View/CreateEditViewGenerator.php @ 34:f65ab84ee47f default

merge with codex
author luka
date Wed, 10 Sep 2025 21:00:47 -0400
parents 555bfaa500ac
children
comparison
equal deleted inserted replaced
10:a9ff874afdbd 34:f65ab84ee47f
1 <?php
2
3 namespace Wizard\MagicForger\Generator\View;
4
5 use Symfony\Component\Console\Attribute\AsCommand;
6 use Wizard\MagicForger\Generator\BaseGenerator;
7 use Wizard\MagicForger\Helpers\RelationshipNavigator;
8 use Illuminate\Support\Str;
9
10 #[AsCommand(name: 'mf:create_edit_view')]
11 class CreateEditViewGenerator extends BaseGenerator
12 {
13 /**
14 * The name and signature of the console command.
15 *
16 * @var string
17 */
18 protected $name = 'mf:create_edit_view';
19
20 /**
21 * The console command description.
22 *
23 * @var string
24 */
25 protected $description = 'Generates the CreateEditView File for a table.';
26
27 /**
28 * The type of class being generated.
29 *
30 * @var string
31 */
32 protected $type = 'CreateEditView';
33
34 /**
35 * Execute the console command.
36 */
37 public function handle()
38 {
39 parent::handle();
40 }
41
42 /**
43 * Get the stub file for the generator.
44 *
45 * @return string
46 */
47 protected function getStub()
48 {
49 return $this->resolveStubPath('/stubs/create_edit.stub');
50 }
51
52 /**
53 * Resolve the fully-qualified path to the stub.
54 *
55 * @param string $stub
56 * @return string
57 */
58 protected function resolveStubPath($stub)
59 {
60 return is_file($customPath = $this->laravel->basePath(trim($stub, '/')))
61 ? $customPath
62 : __DIR__.$stub;
63 }
64
65 protected function getClassName($name)
66 {
67 return $this->create_edit_view_name($name);
68 }
69
70 /**
71 * Get the stub file for the generator.
72 *
73 * @return string
74 */
75 protected function getPath($name = null)
76 {
77 return str_replace(['Resources\\', '\\'], ['resources/', '/'], $this->getViewNamespace($this->getTableInput()).'create_edit.blade.php');
78 }
79
80 protected function renderColumns() {
81 $renders = [];
82
83 $columns = $this->getTableColumns($this->getCurrentTable());
84 $relations = RelationshipNavigator::getRelations($this->getCurrentTable());
85 //gether the select columns based on the relations
86 $selects = [];
87
88 foreach($relations['belongsTo'] as $relation) {
89 $selects[$relation['column']] = $relation['table'];
90 }
91
92 foreach($columns as $column) {
93 $name = $column['name'];
94 if(in_array($name, $this->columns_to_ignore)) continue;
95
96 $type = $column['type_name'];
97
98 $replacements = [
99 '{{fieldName}}' => $name,
100 '{{fieldLabel}}' => Str::headline($name),
101 '{{required}}' => $column['nullable'] ? 'false' : 'true',
102 ];
103 $snippet = '';
104
105 //date
106 if(in_array($type, ['date', 'timestamp'])) {
107 $snippet = $this->getSnippet('input/date');
108 }
109 //checkbox
110 elseif(in_array($type, ['tinyint']) && array_key_exists($name, $selects)) {
111 $snippet = $this->getSnippet('input/checkbox');
112 }
113
114 //select
115 elseif(in_array($type, ['bigint']) && array_key_exists($name, $selects)) {
116 $snippet = $this->getSnippet('input/select');
117 $replacements['{{fieldLabel}}'] = Str::headline(Str::singular($selects[$name]));
118 $replacements['{{options}}'] = '$'.$selects[$name];
119 }
120
121 //text area
122 elseif(in_array($type, ['text'])) {
123 $snippet = $this->getSnippet('input/textarea');
124 }
125 else {
126 //varchar, bigint, float, etc
127 $snippet = $this->getSnippet('input/text');
128 }
129
130
131 // Replace placeholders with actual values
132 $renders[] = str_replace(
133 array_keys($replacements),
134 $replacements,
135 $snippet
136 );
137 }
138 return $renders;
139 }
140
141
142
143 /**
144 * Get available insertions including model relationships.
145 *
146 * @return array
147 */
148 public function get_available_inserts(): array
149 {
150 // Merge parent insertions (attributes, fillable, etc.)
151 $inserts = parent::get_available_inserts();
152
153 // Gather and render relationships for this model
154 $rendered = $this->renderColumns();
155
156 // Build code blocks for each relation type
157 $columns = !empty($rendered) ? implode("\n ", $rendered) : '';
158
159 // Assign to stub placeholders
160 $inserts['{{ fieldsInsertPoint }}'] = $columns;
161
162 return $inserts;
163 }
164 }