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

merge with codex
author luka
date Wed, 10 Sep 2025 21:00:47 -0400
parents 45f384a24553
children
comparison
equal deleted inserted replaced
10:a9ff874afdbd 34:f65ab84ee47f
1 <?php
2
3 namespace Wizard\MagicForger\Generator\View;
4
5 use Illuminate\Support\Str;
6 use Symfony\Component\Console\Attribute\AsCommand;
7 use Wizard\MagicForger\Generator\BaseGenerator;
8 use Wizard\MagicForger\Helpers\RelationshipNavigator;
9
10 #[AsCommand(name: 'mf:index_view')]
11 class IndexViewGenerator extends BaseGenerator
12 {
13 /**
14 * The name and signature of the console command.
15 *
16 * @var string
17 */
18 protected $name = 'mf:index_view';
19
20 /**
21 * The console command description.
22 *
23 * @var string
24 */
25 protected $description = 'Generates the IndexView File for a table.';
26
27 /**
28 * The type of class being generated.
29 *
30 * @var string
31 */
32 protected $type = 'IndexView';
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/index.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->index_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()).'index.blade.php');
78 }
79
80 protected function renderColumns()
81 {
82 $renders = [];
83 $values = [];
84
85 $columns = $this->getTableColumns($this->getCurrentTable());
86 $relations = RelationshipNavigator::getRelations($this->getCurrentTable());
87 // gether the select columns based on the relations
88 $selects = [];
89
90 foreach ($relations['belongsTo'] as $relation) {
91 $selects[$relation['column']] = $relation['table'];
92 }
93
94 foreach ($columns as $column) {
95 $name = $column['name'];
96 if (in_array($name, $this->columns_to_ignore)) {
97 continue;
98 }
99
100 // Get the expected header name
101 $replacements = [
102 '{{header}}' => Str::headline($name),
103 '{{column_name}}' => $name,
104 '{{valueClass}}' => 'p-2',
105 ];
106
107 $type = $column['type_name'];
108
109 // date
110 if (in_array($type, ['date'])) {
111 $replacements['{{value}}'] = '{{ $item->'.$name.'?->format(\'Y-m-d\') ?? "" }}';
112 }
113 // time
114 if (in_array($type, ['timestamp'])) {
115 $replacements['{{value}}'] = '{{ $item->'.$name.'?->format(\'Y-m-d H:i\') ?? "" }}';
116 }
117 // checkbox
118 if (in_array($type, ['tinyint'])) {
119 $replacements['{{valueClass}}'] .= ' text-center';
120 $replacements['{{value}}'] = '{{ $item->'.$name.' ?? "0" }}';
121 }
122 // select
123 elseif (in_array($type, ['bigint']) && array_key_exists($name, $selects)) {
124 $replacements['{{header}}'] = Str::headline(Str::singular($selects[$name]));
125 $replacements['{{value}}'] = '{{ $item->'.Str::singular($selects[$name]).'?->name ?? "" }}';
126 }
127 // bigint, float
128 elseif (in_array($type, ['bigint', 'float', 'int'])) {
129 $replacements['{{valueClass}}'] .= ' text-start';
130 } else {
131 // text area
132 // varchar, , etc
133 }
134
135 $snippet = $this->getSnippet('index/value');
136 // Replace placeholders with actual values
137 $values[] = str_replace(
138 array_keys($replacements),
139 $replacements,
140 $snippet
141 );
142
143 }
144
145 return ['values' => $values];
146 }
147
148 /**
149 * Get available insertions including model relationships.
150 */
151 public function get_available_inserts(): array
152 {
153 // Merge parent insertions (attributes, fillable, etc.)
154 $inserts = parent::get_available_inserts();
155
156 // Gather and render relationships for this model
157 $rendered = $this->renderColumns();
158
159 // Build code blocks for each relation type
160 $headers = '';
161 $values = '';
162 $colCount = '';
163
164 if (! empty($rendered)) {
165 $values = ! empty($rendered['values']) ? implode("\n ", $rendered['values']) : '';
166 }
167
168 // Assign to stub placeholders
169 $inserts['{{ columnInsertPoint }}'] = $values;
170
171 return $inserts;
172 }
173 }