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

drago-ex / generator / 14082182676

26 Mar 2025 11:29AM UTC coverage: 83.929% (-0.09%) from 84.018%
14082182676

push

github

scrs_zdenek
dev

4 of 5 new or added lines in 2 files covered. (80.0%)

1 existing line in 1 file now uncovered.

188 of 224 relevant lines covered (83.93%)

1.68 hits per line

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

76.12
/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
2✔
34
        {
35
                if ($table !== null) {
2✔
36
                        $this->createPhpFile($table);
2✔
37
                } else {
38
                        foreach ($this->repository->getTableNames() as $table) {
2✔
39
                                $this->createPhpFile($table);
2✔
40
                        }
41
                }
42
        }
2✔
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
2✔
53
        {
54
                // Get options for the generator.
55
                $options = $this->options;
2✔
56

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

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

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

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

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

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

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

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

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

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

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

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

109
                                // Skip auto-increment columns for constant.
110
                                if (!$attr->isAutoIncrement()) {
2✔
111
                                        $class->addConstant($constant, $column)
2✔
112
                                                ->setType('string')
2✔
113
                                                ->setPublic();
2✔
114
                                }
115

116
                                // Add constant for column size if required.
117
                                if ($options->constantSize && !$attr->isAutoIncrement() && $attr->getSize() > 0) {
2✔
118
                                        $class->addConstant($constant . 'Size', $attr->getSize())
×
NEW
119
                                                ->setType('string')
×
UNCOV
120
                                                ->setPublic();
×
121
                                }
122
                        }
123

124
                        // Detect the column's native type.
125
                        $detectType = $this->detectType($attr->getNativeType());
2✔
126

127
                        // Add property for the column.
128
                        $create = $class->addProperty($column)
2✔
129
                                ->setType($detectType)
2✔
130
                                ->setNullable($attr->isNullable())
2✔
131
                                ->setInitialized($attr->isNullable())
2✔
132
                                ->setPublic();
2✔
133

134
                        // Add column info comments.
135
                        if ($this->options->columnInfo) {
2✔
136
                                if ($attr->isAutoIncrement()) {
×
137
                                        $create->addComment('Primary key');
×
138
                                }
139

140
                                if ($attr->getDefault()) {
×
141
                                        $create->addComment('Default value ' . $attr->getDefault());
×
142
                                }
143

144
                                if ($attr->getSize() > 0) {
×
145
                                        $create->addComment('Column size ' . $attr->getSize());
×
146
                                }
147
                        }
148

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

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

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