comparison src/Generator/Factory/FactoryGenerator.php @ 35:55d2e5c5dad9 ls_dev_2025_09

Working on the factory, it's in a semi working state but obviously not complete
author Luka Sitas <sitas.luka.97@gmail.com>
date Thu, 11 Sep 2025 21:25:51 -0400
parents
children 76584181267a
comparison
equal deleted inserted replaced
34:f65ab84ee47f 35:55d2e5c5dad9
1 <?php
2
3 namespace Wizard\MagicForger\Generator\Factory;
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:factory')]
11 class FactoryGenerator extends BaseGenerator
12 {
13 /**
14 * The name and signature of the console command.
15 *
16 * @var string
17 */
18 protected $name = 'mf:factory';
19
20 /**
21 * The console command description.
22 *
23 * @var string
24 */
25 protected $description = 'Generates the Factory File for a table.';
26
27 /**
28 * The type of class being generated.
29 *
30 * @var string
31 */
32 protected $type = 'Factory';
33
34 protected static $cached_snippets = [];
35
36 /**
37 * Execute the console command.
38 *
39 * @return mixed
40 */
41 public function handle()
42 {
43 // Delegate to parent handler (includes replacements and insertions)
44 return parent::handle();
45 }
46
47 /**
48 * Get the stub file for the generator.
49 *
50 * @return string
51 */
52 protected function getStub()
53 {
54 return $this->resolveStubPath('/stubs/factory.stub');
55 }
56
57 /**
58 * Resolve the fully-qualified path to the stub.
59 *
60 * @param string $stub
61 * @return string
62 */
63 protected function resolveStubPath($stub)
64 {
65 return is_file($customPath = $this->laravel->basePath(trim($stub, '/')))
66 ? $customPath
67 : __DIR__.$stub;
68 }
69
70 protected function getClassName($name)
71 {
72 return $this->factory_name($name);
73 }
74
75 /**
76 * Get the stub file for the generator.
77 *
78 * @return string
79 */
80 protected function getPath($name = null)
81 {
82 return str_replace(['App\\', '\\'], ['app/', '/'], $this->getFactoryNamespace().'/'.$this->factory_name($this->getTableInput()).'.php');
83 }
84
85 protected function gatherRelations() {
86 $relations = RelationshipNavigator::getRelations($this->getCurrentTable());
87
88 return $relations;
89
90 }
91
92 protected function renderColumns() {
93 $insert = '';
94 foreach ($this->get_columns() as $column) {
95 if (in_array($column['name'], $this->columns_to_ignore)) {
96 continue;
97 }
98
99
100 $type = $column['type_name'];
101 $nullable = ($column['nullable'] ? '->optional($weight = 0.5)' : '' );
102 $name = $column['name'];
103
104 // Get the expected header name
105 $replacements = [
106 '{{value}}' => 'fake()' . $nullable . '->text()',
107 '{{column_name}}' => $name,
108 ];
109
110 // date
111 if (in_array($type, ['date'])) {
112 $replacements['{{value}}'] = 'fake()' . $nullable . '->date()';
113 }
114 // time
115 if (in_array($type, ['timestamp'])) {
116 $replacements['{{value}}'] = 'fake()' . $nullable . '->timestamp()';
117 }
118 // checkbox
119 if (in_array($type, ['tinyint'])) {
120 $replacements['{{value}}'] = 'fake()' . $nullable . '->boolean()';
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 $tableName = $this->getCurrentTable();
146 $value = 'value'; // TODO: this should be determined based on column type
147 $columnName = $column['name'];
148
149 // Replace placeholders with actual values
150 $string = str_replace(
151 ['{{value}}', '{{columnName}}'],
152 [$value, $columnName],
153 $snippet
154 );
155 $insert .= sprintf("%s", $string);
156 }
157 dd('done');
158
159 return $insert;
160 }
161
162 /**
163 * Get available insertions including model relationships.
164 *
165 * @return array
166 */
167 public function get_available_inserts(): array
168 {
169 // Merge parent insertions (attributes, fillable, etc.)
170 $inserts = parent::get_available_inserts();
171
172 // Gather and render relationships for this model
173 $columns = $this->renderColumns();
174
175 // Assign to stub placeholders
176 $inserts['# {{ factoryInsertPoint }}'] = $columns;
177
178 return $inserts;
179 }
180 }