|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Filament\Infolists\Commands\FileGenerators\Concerns; |
| 4 | + |
| 5 | +use Filament\Infolists\Components\IconEntry; |
| 6 | +use Filament\Infolists\Components\ImageEntry; |
| 7 | +use Filament\Infolists\Components\TextEntry; |
| 8 | +use Illuminate\Database\Eloquent\Model; |
| 9 | +use Illuminate\Support\Str; |
| 10 | +use Nette\PhpGenerator\Literal; |
| 11 | + |
| 12 | +trait CanGenerateModelInfolists |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @param ?class-string<Model> $model |
| 16 | + */ |
| 17 | + public function generateInfolistMethodBody(?string $model = null): string |
| 18 | + { |
| 19 | + return <<<PHP |
| 20 | + return \$schema |
| 21 | + ->columns([ |
| 22 | + {$this->outputInfolistComponents($model)} |
| 23 | + ]); |
| 24 | + PHP; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @param ?class-string<Model> $model |
| 29 | + * @param array<string> $exceptColumns |
| 30 | + * @return array<string> |
| 31 | + */ |
| 32 | + public function getInfolistComponents(?string $model = null, array $exceptColumns = []): array |
| 33 | + { |
| 34 | + if (! $this->isGenerated()) { |
| 35 | + return []; |
| 36 | + } |
| 37 | + |
| 38 | + if (blank($model)) { |
| 39 | + return []; |
| 40 | + } |
| 41 | + |
| 42 | + if (! class_exists($model)) { |
| 43 | + return []; |
| 44 | + } |
| 45 | + |
| 46 | + $schema = $this->getModelSchema($model); |
| 47 | + $table = $this->getModelTable($model); |
| 48 | + |
| 49 | + $components = []; |
| 50 | + |
| 51 | + foreach ($schema->getColumns($table) as $column) { |
| 52 | + if ($column['auto_increment']) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + $type = $this->parseColumnType($column); |
| 57 | + |
| 58 | + if (in_array($type['name'], [ |
| 59 | + 'json', |
| 60 | + 'text', |
| 61 | + ])) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + $componentName = $column['name']; |
| 66 | + |
| 67 | + if (in_array($componentName, $exceptColumns)) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + if (str($componentName)->endsWith([ |
| 72 | + '_token', |
| 73 | + ])) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + if (str($componentName)->contains([ |
| 78 | + 'password', |
| 79 | + ])) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + if (str($componentName)->endsWith('_id')) { |
| 84 | + $guessedRelationshipName = $this->guessBelongsToRelationshipName($componentName, $model); |
| 85 | + |
| 86 | + if (filled($guessedRelationshipName)) { |
| 87 | + $guessedRelationshipTitleColumnName = $this->guessBelongsToRelationshipTitleColumnName($componentName, app($model)->{$guessedRelationshipName}()->getModel()::class); |
| 88 | + |
| 89 | + $componentName = "{$guessedRelationshipName}.{$guessedRelationshipTitleColumnName}"; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + $componentData = []; |
| 94 | + |
| 95 | + if (in_array($componentName, [ |
| 96 | + 'id', |
| 97 | + 'sku', |
| 98 | + 'uuid', |
| 99 | + ])) { |
| 100 | + $componentData['label'] = [Str::upper($componentName)]; |
| 101 | + } |
| 102 | + |
| 103 | + if ($type['name'] === 'boolean') { |
| 104 | + $componentData['type'] = IconEntry::class; |
| 105 | + $componentData['boolean'] = []; |
| 106 | + } else { |
| 107 | + $componentData['type'] = match (true) { |
| 108 | + $componentName === 'image', str($componentName)->startsWith('image_'), str($componentName)->contains('_image_'), str($componentName)->endsWith('_image') => ImageEntry::class, |
| 109 | + default => TextEntry::class, |
| 110 | + }; |
| 111 | + |
| 112 | + if ($type['name'] === 'date') { |
| 113 | + $componentData['date'] = []; |
| 114 | + } |
| 115 | + |
| 116 | + if ($type['name'] === 'time') { |
| 117 | + $componentData['time'] = []; |
| 118 | + } |
| 119 | + |
| 120 | + if (in_array($type['name'], [ |
| 121 | + 'datetime', |
| 122 | + 'timestamp', |
| 123 | + ])) { |
| 124 | + $componentData['dateTime'] = []; |
| 125 | + } |
| 126 | + |
| 127 | + if (in_array($type['name'], [ |
| 128 | + 'integer', |
| 129 | + 'decimal', |
| 130 | + 'float', |
| 131 | + 'double', |
| 132 | + 'money', |
| 133 | + ])) { |
| 134 | + $componentData[in_array($componentName, [ |
| 135 | + 'cost', |
| 136 | + 'money', |
| 137 | + 'price', |
| 138 | + ]) || $type['name'] === 'money' ? 'money' : 'numeric'] = []; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + $this->importUnlessPartial($componentData['type']); |
| 143 | + |
| 144 | + $components[$componentName] = $componentData; |
| 145 | + } |
| 146 | + |
| 147 | + return array_map( |
| 148 | + function (array $componentData, string $componentName): string { |
| 149 | + $component = (string) new Literal("{$this->simplifyFqn($componentData['type'])}::make(?)", [$componentName]); |
| 150 | + |
| 151 | + unset($componentData['type']); |
| 152 | + |
| 153 | + foreach ($componentData as $methodName => $parameters) { |
| 154 | + $component .= new Literal(PHP_EOL . " ->{$methodName}(...?:)", [$parameters]); |
| 155 | + } |
| 156 | + |
| 157 | + return "{$component},"; |
| 158 | + }, |
| 159 | + $components, |
| 160 | + array_keys($components), |
| 161 | + ); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @param ?class-string<Model> $model |
| 166 | + * @param array<string> $exceptColumns |
| 167 | + */ |
| 168 | + public function outputInfolistComponents(?string $model = null, array $exceptColumns = []): string |
| 169 | + { |
| 170 | + $columns = $this->getInfolistComponents($model, $exceptColumns); |
| 171 | + |
| 172 | + if (empty($columns)) { |
| 173 | + return '//'; |
| 174 | + } |
| 175 | + |
| 176 | + return implode(PHP_EOL . ' ', $columns); |
| 177 | + } |
| 178 | +} |
0 commit comments