Mercurial > packages > magicforger
comparison src/Generator/Factory/FactoryGenerator.php @ 39:b5c6ebd33547 ls_dev_2025_09
Improving the factories, tests, and requests
| author | Luka Sitas <sitas.luka.97@gmail.com> |
|---|---|
| date | Thu, 25 Sep 2025 23:16:13 -0400 |
| parents | 76584181267a |
| children | 2cf26b593f4a |
comparison
equal
deleted
inserted
replaced
| 38:c062f013fd19 | 39:b5c6ebd33547 |
|---|---|
| 1 <?php | 1 <?php |
| 2 | 2 |
| 3 namespace Wizard\MagicForger\Generator\Factory; | 3 namespace Wizard\MagicForger\Generator\Factory; |
| 4 | 4 |
| 5 use Illuminate\Support\Str; | |
| 5 use Symfony\Component\Console\Attribute\AsCommand; | 6 use Symfony\Component\Console\Attribute\AsCommand; |
| 6 use Wizard\MagicForger\Generator\BaseGenerator; | 7 use Wizard\MagicForger\Generator\BaseGenerator; |
| 7 use Wizard\MagicForger\Helpers\RelationshipNavigator; | 8 use Wizard\MagicForger\Helpers\RelationshipNavigator; |
| 8 use Illuminate\Support\Str; | |
| 9 | 9 |
| 10 #[AsCommand(name: 'mf:factory')] | 10 #[AsCommand(name: 'mf:factory')] |
| 11 class FactoryGenerator extends BaseGenerator | 11 class FactoryGenerator extends BaseGenerator |
| 12 { | 12 { |
| 13 /** | 13 /** |
| 80 protected function getPath($name = null) | 80 protected function getPath($name = null) |
| 81 { | 81 { |
| 82 return str_replace(['App\\', '\\'], ['app/', '/'], $this->getFactoryNamespace().'/'.$this->factory_name($this->getTableInput()).'.php'); | 82 return str_replace(['App\\', '\\'], ['app/', '/'], $this->getFactoryNamespace().'/'.$this->factory_name($this->getTableInput()).'.php'); |
| 83 } | 83 } |
| 84 | 84 |
| 85 protected function gatherRelations() { | 85 protected function gatherRelations() |
| 86 $relations = RelationshipNavigator::getRelations($this->getCurrentTable()); | 86 { |
| 87 $relations = RelationshipNavigator::getRelations($this->getCurrentTable()); | |
| 87 | 88 |
| 88 return $relations; | 89 return $relations; |
| 89 | |
| 90 } | |
| 91 | 90 |
| 92 protected function renderColumns() { | 91 } |
| 92 | |
| 93 protected function renderColumns() | |
| 94 { | |
| 93 $insert = ''; | 95 $insert = ''; |
| 94 $values = []; | 96 $values = []; |
| 95 foreach ($this->get_columns() as $column) { | 97 foreach ($this->get_columns() as $column) { |
| 96 if (in_array($column['name'], $this->columns_to_ignore)) { | 98 if (in_array($column['name'], $this->columns_to_ignore)) { |
| 97 continue; | 99 continue; |
| 98 } | 100 } |
| 99 | 101 |
| 100 $type = $column['type_name']; | 102 $type = $column['type_name']; |
| 101 $nullable = ($column['nullable'] ? '->optional($weight = 0.5)' : '' ); | 103 $nullable = ($column['nullable'] ? '->optional($weight = 0.5)' : ''); |
| 102 $value = 'fake()' . $nullable; | 104 $value = 'fake()'.$nullable; |
| 103 $name = $column['name']; | 105 $name = $column['name']; |
| 104 | 106 |
| 105 // Get the expected header name | 107 // Get the expected header name |
| 106 $replacements = [ | 108 $replacements = [ |
| 107 '{{value}}' => $value . '->text()', | 109 '{{value}}' => $value.'->text()', |
| 108 '{{column_name}}' => $name, | 110 '{{column_name}}' => $name, |
| 109 ]; | 111 ]; |
| 110 | 112 |
| 111 // date | 113 // date |
| 112 if (in_array($type, ['date'])) { | 114 if (in_array($type, ['date'])) { |
| 113 $replacements['{{value}}'] = $value . '->date()'; | 115 $replacements['{{value}}'] = $value.'->date()'; |
| 114 } | 116 } |
| 115 // time | 117 // time |
| 116 if (in_array($type, ['timestamp'])) { | 118 if (in_array($type, ['timestamp'])) { |
| 117 $replacements['{{value}}'] = $value . '->timestamp()'; | 119 $replacements['{{value}}'] = $value.'->timestamp()'; |
| 118 } | 120 } |
| 119 // checkbox | 121 // checkbox |
| 120 if (in_array($type, ['tinyint'])) { | 122 if (in_array($type, ['tinyint'])) { |
| 121 $replacements['{{value}}'] = $value . '->boolean()'; | 123 $replacements['{{value}}'] = $value.'->numberBetween(0,1)'; |
| 122 } | 124 } |
| 123 // select | 125 // select |
| 124 elseif (in_array($type, ['bigint']) && array_key_exists($name, $selects)) { | 126 elseif (in_array($type, ['bigint']) && array_key_exists($name, $selects)) { |
| 125 $replacements['{{header}}'] = Str::headline(Str::singular($selects[$name])); | 127 $replacements['{{header}}'] = Str::headline(Str::singular($selects[$name])); |
| 126 $replacements['{{value}}'] = '{{ $item->'.Str::singular($selects[$name]).'?->name ?? "" }}'; | 128 $replacements['{{value}}'] = '{{ $item->'.Str::singular($selects[$name]).'?->name ?? "" }}'; |
| 127 } | 129 } |
| 128 // bigint, float | 130 // bigint, float |
| 129 elseif (in_array($type, ['bigint', 'int'])) { | 131 elseif (in_array($type, ['bigint', 'int'])) { |
| 130 $replacements['{{value}}'] = $value . '->randomNumber()'; | 132 $replacements['{{value}}'] = $value.'->randomNumber()'; |
| 131 } | 133 } elseif (in_array($type, ['float'])) { |
| 132 elseif (in_array($type, ['float'])) { | 134 $replacements['{{value}}'] = $value.'->randomFloat()'; |
| 133 $replacements['{{value}}'] = $value . '->randomFloat()'; | |
| 134 } else { | 135 } else { |
| 135 // text area | 136 // text area |
| 136 // varchar, , etc | 137 // varchar, , etc |
| 137 } | 138 } |
| 138 | 139 |
| 142 array_keys($replacements), | 143 array_keys($replacements), |
| 143 $replacements, | 144 $replacements, |
| 144 $snippet | 145 $snippet |
| 145 ); | 146 ); |
| 146 | 147 |
| 147 // Replace placeholders with actual values | 148 // Replace placeholders with actual values |
| 148 } | 149 } |
| 149 $insert = implode("\n", $values); | 150 $insert = implode("\n", $values); |
| 150 | 151 |
| 151 return $insert; | 152 return $insert; |
| 152 } | 153 } |
| 153 | 154 |
| 154 /** | 155 /** |
| 155 * Get available insertions including model relationships. | 156 * Get available insertions including model relationships. |
| 156 * | |
| 157 * @return array | |
| 158 */ | 157 */ |
| 159 public function get_available_inserts(): array | 158 public function get_available_inserts(): array |
| 160 { | 159 { |
| 161 // Merge parent insertions (attributes, fillable, etc.) | 160 // Merge parent insertions (attributes, fillable, etc.) |
| 162 $inserts = parent::get_available_inserts(); | 161 $inserts = parent::get_available_inserts(); |
| 163 | 162 |
| 164 // Gather and render relationships for this model | 163 // Gather and render relationships for this model |
| 165 $columns = $this->renderColumns(); | 164 $columns = $this->renderColumns(); |
| 166 | 165 |
| 167 // Assign to stub placeholders | 166 // Assign to stub placeholders |
| 168 $inserts['# {{ factoryInsertPoint }}'] = $columns; | 167 $inserts['# {{ factoryInsertPoint }}'] = $columns; |
| 169 | 168 |
| 170 return $inserts; | 169 return $inserts; |
| 171 } | 170 } |
| 172 } | 171 } |
