• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RonasIT / laravel-entity-generator / 21166733718

20 Jan 2026 09:43AM UTC coverage: 99.432% (-0.6%) from 100.0%
21166733718

Pull #241

github

web-flow
Merge 2b61df918 into 962b971b7
Pull Request #241: [239]: append field modifiers instead of uppercase options

165 of 167 new or added lines in 17 files covered. (98.8%)

4 existing lines in 2 files now uncovered.

1051 of 1057 relevant lines covered (99.43%)

11.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.44
/src/Generators/NovaResourceGenerator.php
1
<?php
2

3
namespace RonasIT\Support\Generators;
4

5
use Doctrine\DBAL\DriverManager;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Facades\DB;
8
use Laravel\Nova\NovaServiceProvider;
9
use RonasIT\Support\Events\SuccessCreateMessage;
10
use RonasIT\Support\Support\CommandLineNovaField;
11
use RonasIT\Support\Support\DatabaseNovaField;
12

13
class NovaResourceGenerator extends EntityGenerator
14
{
15
    protected $novaFieldTypesMap = [
16
        'boolean' => 'Boolean',
17
        'timestamp' => 'DateTime',
18
        'string' => 'Text',
19
        'json' => 'Text',
20
        'integer' => 'Number',
21
        'float' => 'Number',
22
    ];
23

24
    protected $novaFieldsDatabaseMap = [
25
        'integer' => 'Number',
26
        'smallint' => 'Number',
27
        'bigint' => 'Number',
28
        'float' => 'Number',
29
        'decimal' => 'Number',
30
        'string' => 'Text',
31
        'text' => 'Text',
32
        'guid' => 'Text',
33
        'json' => 'Text',
34
        'date' => 'Date',
35
        'datetime' => 'DateTime',
36
        'datetimetz' => 'DateTime',
37
        'boolean' => 'Boolean',
38
    ];
39

40
    protected $specialFieldNamesMap = [
41
        'id' => 'ID',
42
        'country_code' => 'Country',
43
        'city' => 'City',
44
        'time_zone' => 'Timezone',
45
    ];
46

47
    public function generate(): void
48
    {
49
        if (class_exists(NovaServiceProvider::class)) {
8✔
50
            $this->checkResourceNotExists('models', "{$this->model}Resource", $this->model, $this->modelSubFolder);
7✔
51

52
            $this->checkResourceExists('nova', "{$this->model}Resource", $this->modelSubFolder);
6✔
53

54
            if (!$this->isStubExists('nova_resource')) {
5✔
55
                return;
1✔
56
            }
57

58
            $this->createNamespace('nova');
4✔
59

60
            $novaFields = $this->prepareNovaFields();
4✔
61

62
            $fileContent = $this->getStub('nova_resource', [
4✔
63
                'model' => $this->model,
4✔
64
                'fields' => $novaFields,
4✔
65
                'types' => array_unique(data_get($novaFields, '*.type')),
4✔
66
                'imports' => $this->getImports(),
4✔
67
                'namespace' => $this->generateNamespace($this->paths['nova'], $this->modelSubFolder),
4✔
68
            ]);
4✔
69

70
            $this->saveClass('nova', "{$this->model}Resource", $fileContent, $this->modelSubFolder);
4✔
71

72
            event(new SuccessCreateMessage("Created a new Nova Resource: {$this->model}Resource"));
4✔
73
        } else {
74
            event(new SuccessCreateMessage('Nova is not installed and NovaResource is skipped'));
1✔
75
        }
76
    }
77

78
    protected function prepareNovaFields(): array
79
    {
80
        $result = [];
4✔
81
        list($fields, $fieldTypesMap) = $this->getFieldsForCreation();
4✔
82

83
        foreach ($fields as $field) {
4✔
84
            if (!Arr::has($fieldTypesMap, $field->type)) {
4✔
UNCOV
85
                event(new SuccessCreateMessage("Field '{$field->name}' had been skipped cause has an unhandled type {$field->type}."));
×
86
            } elseif (Arr::has($this->specialFieldNamesMap, $field->name)) {
4✔
87
                $result[$field->name] = [
4✔
88
                    'type' => $this->specialFieldNamesMap[$field->name],
4✔
89
                    'is_required' => $field->isRequired,
4✔
90
                ];
4✔
91
            } else {
92
                $result[$field->name] = [
4✔
93
                    'type' => $fieldTypesMap[$field->type],
4✔
94
                    'is_required' => $field->isRequired,
4✔
95
                ];
4✔
96
            }
97
        }
98

99
        return $result;
4✔
100
    }
101

102
    protected function getFieldsForCreation(): array
103
    {
104
        if ($this->commandFieldsExists()) {
4✔
105
            return $this->getFieldsFromCommandLineArguments();
1✔
106
        }
107

108
        return $this->getFieldsFromDatabase();
3✔
109
    }
110

111
    protected function getFieldsFromCommandLineArguments(): array
112
    {
113
        $fields = [];
1✔
114

115
        foreach ($this->fields as $commandLineField) {
1✔
116
            $fields[] = new CommandLineNovaField($commandLineField->type, $commandLineField);
1✔
117
        }
118

119
        return [$fields, $this->novaFieldTypesMap];
1✔
120
    }
121

122
    protected function getFieldsFromDatabase(): array
123
    {
124
        $modelClass = $this->getModelClass($this->model);
3✔
125
        $model = app($modelClass);
3✔
126

127
        $columns = $this->getColumnList($model->getTable(), $model->getConnectionName());
3✔
128

129
        $fields = array_map(function ($column) {
3✔
130
            return new DatabaseNovaField($column);
3✔
131
        }, $columns);
3✔
132

133
        return [$fields, $this->novaFieldsDatabaseMap];
3✔
134
    }
135

136
    protected function commandFieldsExists(): bool
137
    {
138
        return !empty(Arr::flatten($this->fields));
4✔
139
    }
140

141
    protected function getColumnList(string $table, ?string $connectionName = null): array
142
    {
143
        $config = DB::connection($connectionName)->getConfig();
3✔
144

145
        $dbalConnection = DriverManager::getConnection([
3✔
146
            'dbname' => $config['database'],
3✔
147
            'user' => $config['username'],
3✔
148
            'password' => $config['password'],
3✔
149
            'host' => $config['host'],
3✔
150
            'driver' => "pdo_{$config['driver']}",
3✔
151
        ]);
3✔
152

153
        return $dbalConnection
3✔
154
            ->createSchemaManager()
3✔
155
            ->listTableColumns($table);
3✔
156
    }
157

158
    protected function getImports(): array
159
    {
160
        $imports = [
4✔
161
            "{$this->generateNamespace($this->paths['models'], $this->modelSubFolder)}\\{$this->model}",
4✔
162
        ];
4✔
163

164
        if (!empty($this->modelSubFolder)) {
4✔
165
            $imports[] = "{$this->generateNamespace($this->paths['nova'])}\\Resource";
1✔
166
        }
167

168
        return $imports;
4✔
169
    }
170
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc