Skip to content

Commit 02ac7da

Browse files
committed
Improve MakeEntity Command
1 parent f20d180 commit 02ac7da

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

src/Commands/MakeEntity.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,6 @@ public function handle(): int
9292
die;
9393
}
9494

95-
if ($detectForeignKeys) {
96-
$foreignKeys = $this->extractForeignKeys($tableName);
97-
}
98-
9995
foreach ($columns as $_column) {
10096
$_column->COLUMN_NAME = camel_case($_column->COLUMN_NAME);
10197
}
@@ -104,39 +100,44 @@ public function handle(): int
104100
$attributeStub = file_get_contents($entityStubsPath.'attribute.stub');
105101
$accessorsStub = file_get_contents($entityStubsPath.'accessors.stub');
106102

107-
// Initialize Class
108-
$baseContent = str_replace(['{{ EntityNamespace }}', '{{ EntityName }}'], [$entityNamespace, $entityName], $baseContent);
109-
110103
// Create Attributes
104+
$attributes = '';
111105
foreach ($columns as $_column) {
112-
$baseContent = substr_replace($baseContent,
106+
$attributes = substr_replace($attributes,
113107
$this->writeAttribute($attributeStub, $_column->COLUMN_NAME, $this->dataTypes[$_column->DATA_TYPE]),
114108
-1, 0);
115109
}
116-
// Create Additional Attributes from Foreign Keys
117-
if ($detectForeignKeys) {
118-
foreach ($foreignKeys as $_foreignKey) {
119-
$baseContent = substr_replace($baseContent,
120-
$this->writeAttribute($attributeStub, $_foreignKey->VARIABLE_NAME, $_foreignKey->ENTITY_DATA_TYPE),
121-
-1, 0);
122-
}
123-
}
124110

125111
// Create Setters and Getters
112+
$settersAndGetters = '';
126113
foreach ($columns as $_column) {
127-
$baseContent = substr_replace($baseContent,
114+
$settersAndGetters = substr_replace($settersAndGetters,
128115
$this->writeAccessors($accessorsStub, $_column->COLUMN_NAME, $this->dataTypes[$_column->DATA_TYPE]),
129116
-1, 0);
130117
}
131-
// Create Additional Setters and Getters from Foreign keys
118+
132119
if ($detectForeignKeys) {
120+
$foreignKeys = $this->extractForeignKeys($tableName);
121+
122+
// Create Additional Attributes from Foreign Keys
123+
foreach ($foreignKeys as $_foreignKey) {
124+
$attributes = substr_replace($attributes,
125+
$this->writeAttribute($attributeStub, $_foreignKey->VARIABLE_NAME, $_foreignKey->ENTITY_DATA_TYPE),
126+
-1, 0);
127+
}
128+
129+
// Create Additional Setters and Getters from Foreign keys
133130
foreach ($foreignKeys as $_foreignKey) {
134-
$baseContent = substr_replace($baseContent,
131+
$settersAndGetters = substr_replace($settersAndGetters,
135132
$this->writeAccessors($accessorsStub, $_foreignKey->VARIABLE_NAME, $_foreignKey->ENTITY_DATA_TYPE),
136133
-1, 0);
137134
}
138135
}
139136

137+
$baseContent = str_replace(['{{ EntityNamespace }}', '{{ EntityName }}', '{{ Attributes }}', '{{ SettersAndGetters }}'],
138+
[$entityNamespace, $entityName, $attributes, $settersAndGetters],
139+
$baseContent);
140+
140141
file_put_contents($filenameWithPath, $baseContent);
141142

142143
if ($this->option('add-to-git')) {

stubs/PHP7.4/repository.entity.class.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ namespace {{ EntityNamespace }};
44

55
class {{ EntityName }} extends Entity
66
{
7+
{{ Attributes }}{{ SettersAndGetters }}
78
}

0 commit comments

Comments
 (0)