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

drago-ex / generator / 12947105156

24 Jan 2025 09:42AM UTC coverage: 84.018% (-3.1%) from 87.111%
12947105156

push

github

web-flow
Update DataClassGenerator.phpt

184 of 219 relevant lines covered (84.02%)

0.84 hits per line

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

76.56
/src/Drago/Generator/Generator/EntityGenerator.php
1
<?php
2

3
/**
4
 * Drago Extension
5
 * Package built on Nette Framework
6
 */
7

8
declare(strict_types=1);
9

10
namespace Drago\Generator\Generator;
11

12
use Dibi\Exception;
13
use Drago\Generator\Base;
14
use Nette\PhpGenerator\ClassType;
15
use Nette\PhpGenerator\PhpFile;
16
use Nette\Utils\FileSystem;
17
use Nette\Utils\Strings;
18
use Throwable;
19

20

21
/**
22
 * Generates entity class based on database schema.
23
 */
24
class EntityGenerator extends Base implements IGenerator
25
{
26
        /**
27
         * Runs the generation process for a specific table or all tables.
28
         *
29
         * @param string|null $table The table name to generate for, or null for all tables.
30
         * @throws Exception
31
         * @throws Throwable
32
         */
33
        public function runGeneration(?string $table = null): void
1✔
34
        {
35
                if ($table !== null) {
1✔
36
                        $this->createPhpFile($table);
1✔
37
                } else {
38
                        foreach ($this->repository->getTableNames() as $table) {
1✔
39
                                $this->createPhpFile($table);
1✔
40
                        }
41
                }
42
        }
1✔
43

44

45
        /**
46
         * Creates a PHP file for a given table.
47
         *
48
         * @param string $table The table name to generate the class for.
49
         * @throws Exception
50
         * @throws Throwable
51
         */
52
        public function createPhpFile(string $table): void
1✔
53
        {
54
                // Get options for the generator.
55
                $options = $this->options;
1✔
56

57
                // Create the class filename and add suffix.
58
                $name = $this->filename($table, $options->suffix);
1✔
59

60
                // Generate the class.
61
                $class = new ClassType($name);
1✔
62

63
                // Add final modifier if required.
64
                if ($options->final) {
1✔
65
                        $class->setFinal();
×
66
                }
67

68
                // Add extends class if required.
69
                if ($options->extendsOn) {
1✔
70
                        $class->setExtends($options->extends);
1✔
71
                }
72

73
                // Get references for the table.
74
                $references = $this->getReferencesTable($table);
1✔
75

76
                // Add the constant for table name.
77
                $tableName = $this->options->tableName ?? 'Table';
1✔
78
                $class->addConstant($tableName, $table)
1✔
79
                        ->setPublic();
1✔
80

81
                // Process each column from the table.
82
                foreach ($this->repository->getColumnNames($table) as $column) {
1✔
83

84
                        // Convert column name to lowercase if required.
85
                        if ($options->lower) {
1✔
86
                                $column = Strings::lower($column);
×
87
                        }
88

89
                        // Validate column names.
90
                        $this->validateColumn($table, $column);
1✔
91

92
                        // Get column attributes.
93
                        $attr = $this->repository->getColumn($table, $column);
1✔
94

95
                        // Add constant for primary key if applicable.
96
                        if ($attr->isAutoIncrement()) {
1✔
97
                                $primaryKey = $this->options->primaryKey ?? 'Id';
1✔
98
                                $class->addConstant($primaryKey, $column)
1✔
99
                                        ->setPublic();
1✔
100
                        }
101

102
                        // Add constants for other columns.
103
                        if ($options->constant) {
1✔
104
                                $constant = $this->options->constantPrefix
1✔
105
                                        ? $this->options->constantPrefix . $this->inflector->classify($column)
×
106
                                        : $this->inflector->classify($column);
1✔
107

108
                                // Skip auto-increment columns for constant.
109
                                if (!$attr->isAutoIncrement()) {
1✔
110
                                        $class->addConstant($constant, $column)
1✔
111
                                                ->setPublic();
1✔
112
                                }
113

114
                                // Add constant for column size if required.
115
                                if ($options->constantSize && !$attr->isAutoIncrement() && $attr->getSize() > 0) {
1✔
116
                                        $class->addConstant($constant . 'Size', $attr->getSize())
×
117
                                                ->setPublic();
×
118
                                }
119
                        }
120

121
                        // Detect the column's native type.
122
                        $detectType = $this->detectType($attr->getNativeType());
1✔
123

124
                        // Add property for the column.
125
                        $create = $class->addProperty($column)
1✔
126
                                ->setType($detectType)
1✔
127
                                ->setNullable($attr->isNullable())
1✔
128
                                ->setInitialized($attr->isNullable())
1✔
129
                                ->setPublic();
1✔
130

131
                        // Add column info comments.
132
                        if ($this->options->columnInfo) {
1✔
133
                                if ($attr->isAutoIncrement()) {
×
134
                                        $create->addComment('Primary key');
×
135
                                }
136

137
                                if ($attr->getDefault()) {
×
138
                                        $create->addComment('Default value ' . $attr->getDefault());
×
139
                                }
140

141
                                if ($attr->getSize() > 0) {
×
142
                                        $create->addComment('Column size ' . $attr->getSize());
×
143
                                }
144
                        }
145

146
                        // Add reference to another table if applicable.
147
                        if ($options->references && isset($references[$column])) {
1✔
148
                                $filename = $this->filename($references[$column], $options->suffix);
×
149
                                $class->addProperty($references[$column])
×
150
                                        ->setType($options->namespace . '\\' . $filename)
×
151
                                        ->setComment('Table join references');
×
152
                        }
153
                }
154

155
                // Generate the PHP file content.
156
                $file = new PhpFile;
1✔
157
                $file->addComment('This file was generated by Drago Generator.')
1✔
158
                        ->setStrictTypes()
1✔
159
                        ->addNamespace($options->namespace)
1✔
160
                        ->addUse('Drago')
1✔
161
                        ->add($class);
1✔
162

163
                // Write the generated PHP file to the filesystem.
164
                $path = $options->path . '/' . $name . '.php';
1✔
165
                FileSystem::write($path, $file->__toString());
1✔
166
        }
1✔
167
}
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