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

drago-ex / generator / 14103222108

27 Mar 2025 09:30AM UTC coverage: 84.0%. Remained the same
14103222108

push

github

web-flow
Update EntityGenerator.php

0 of 1 new or added line in 1 file covered. (0.0%)

189 of 225 relevant lines covered (84.0%)

1.68 hits per line

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

76.47
/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
                        ->setType('string')
2✔
80
                        ->setPublic();
2✔
81

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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