Skip to content

Commit 4bc0dea

Browse files
committed
Create Entity Content From Stub Files
1 parent d9f7928 commit 4bc0dea

File tree

3 files changed

+56
-38
lines changed

3 files changed

+56
-38
lines changed

src/Commands/MakeEntity.php

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,17 @@ public function __construct()
3434
use CustomMySqlQueries;
3535

3636
/**
37-
* Generate a getter for given attribute.
37+
* Generate getter and setter for given attribute.
38+
* @param string $accessorStub
3839
* @param string $attributeName
3940
* @param string $attributeType
4041
* @return string
4142
*/
42-
private function writeGetter(string $attributeName, string $attributeType): string
43+
private function writeAccessors(string $accessorStub, string $attributeName, string $attributeType): string
4344
{
44-
return "\n\t/**\n\t * @return $attributeType\n\t */\n\t" .
45-
"public function get" . ucfirst($attributeName) . "(): $attributeType\n\t" .
46-
"{\n\t\treturn \$this->$attributeName;\n\t}\n";
47-
}
48-
49-
/**
50-
* Generate a setter for given attribute.
51-
* @param string $attributeName
52-
* @param string $attributeType
53-
* @return string
54-
*/
55-
private function writeSetter(string $attributeName, string $attributeType): string
56-
{
57-
return "\n\t/**\n\t * @param $attributeType \$$attributeName\n\t */\n\t" .
58-
"public function set" . ucfirst($attributeName) . "($attributeType \$$attributeName): void\n\t" .
59-
"{\n\t\t\$this->$attributeName = \$$attributeName;\n\t}\n";
45+
return str_replace(['AttributeType', 'AttributeName', 'GetterName', 'SetterName'],
46+
[$attributeType, $attributeName, ucfirst($attributeName), ucfirst($attributeName)],
47+
$accessorStub);
6048
}
6149

6250
/**
@@ -69,27 +57,31 @@ public function handle(): int
6957
$tableName = $this->argument('table_name');
7058
$detectForeignKeys = $this->option('foreign-keys');
7159
$entityName = str_singular(ucfirst(camel_case($tableName)));
60+
$entityNamespace = config('repository.path.namespace.entities');
7261
$relativeEntitiesPath = config('repository.path.relative.entities');
62+
$entityStubsPath = config('repository.path.stubs.entity');
63+
$filenameWithPath = $relativeEntitiesPath.$entityName.'.php';
7364

7465
if ($this->option('delete')) {
75-
unlink("$relativeEntitiesPath/$entityName.php");
66+
unlink($filenameWithPath);
7667
$this->info("Entity \"$entityName\" has been deleted.");
7768
return 0;
7869
}
7970

80-
if (!file_exists($relativeEntitiesPath)) {
81-
mkdir($relativeEntitiesPath, 775, true);
71+
if ( ! file_exists($relativeEntitiesPath) && ! mkdir($relativeEntitiesPath, 775, true) && ! is_dir($relativeEntitiesPath)) {
72+
$this->alert("Directory \"$relativeEntitiesPath\" was not created");
73+
return 0;
8274
}
8375

84-
if (class_exists("$relativeEntitiesPath\\$entityName") && !$this->option('force')) {
85-
$this->alert("Entity $entityName is already exist!");
86-
die;
76+
if (class_exists($entityNamespace.'\\'.$entityName) && ! $this->option('force')) {
77+
$this->alert("Entity \"$entityName\" is already exist!");
78+
return 0;
8779
}
8880

8981
$columns = $this->getAllColumnsInTable($tableName);
9082

9183
if ($columns->isEmpty()) {
92-
$this->alert("Couldn't retrieve columns from table " . $tableName . "! Perhaps table's name is misspelled.");
84+
$this->alert("Couldn't retrieve columns from table \"$tableName\"! Perhaps table's name is misspelled.");
9385
die;
9486
}
9587

@@ -101,40 +93,43 @@ public function handle(): int
10193
$_column->COLUMN_NAME = camel_case($_column->COLUMN_NAME);
10294
}
10395

96+
$baseContent = file_get_contents($entityStubsPath.'Class.stub');
97+
$accessorsStub = file_get_contents($entityStubsPath.'Accessors.stub');
98+
10499
// Initialize Class
105-
$entityContent = "<?php\n\nnamespace $relativeEntitiesPath;\n\n";
106-
$entityContent .= "class $entityName extends Entity\n{\n";
100+
$baseContent = str_replace(['EntityNameSpace', 'EntityName'], [$entityNamespace, $entityName], $baseContent);
107101

108102
// Create Attributes
109103
foreach ($columns as $_column) {
110-
$entityContent .= "\tprotected \$$_column->COLUMN_NAME;\n";
104+
$baseContent = substr_replace($baseContent,
105+
"\tprotected " . $this->dataTypes[$_column->DATA_TYPE] . " \$$_column->COLUMN_NAME;\n",
106+
-1, 0);
111107
}
112108
// Create Additional Attributes from Foreign Keys
113109
if ($detectForeignKeys) {
114110
foreach ($foreignKeys as $_foreignKey) {
115-
$entityContent .= "\n\tprotected \$" . $_foreignKey->VARIABLE_NAME . ";";
111+
$baseContent = substr_replace($baseContent,
112+
"\tprotected " . $_foreignKey->ENTITY_DATA_TYPE . " \$$_foreignKey->VARIABLE_NAME;\n",
113+
-1, 0);
116114
}
117-
$entityContent .= "\n";
118115
}
119116

120117
// Create Setters and Getters
121118
foreach ($columns as $_column) {
122-
$dataType = $this->dataTypes[$_column->DATA_TYPE];
123-
$entityContent .= $this->writeGetter($_column->COLUMN_NAME, $dataType);
124-
$entityContent .= $this->writeSetter($_column->COLUMN_NAME, $dataType);
119+
$baseContent = substr_replace($baseContent,
120+
$this->writeAccessors($accessorsStub, $_column->COLUMN_NAME, $this->dataTypes[$_column->DATA_TYPE]),
121+
-1, 0);
125122
}
126123
// Create Additional Setters and Getters from Foreign keys
127124
if ($detectForeignKeys) {
128125
foreach ($foreignKeys as $_foreignKey) {
129-
$entityContent .= $this->writeGetter($_foreignKey->VARIABLE_NAME, $_foreignKey->ENTITY_DATA_TYPE);
130-
$entityContent .= $this->writeSetter($_foreignKey->VARIABLE_NAME, $_foreignKey->ENTITY_DATA_TYPE);
126+
$baseContent = substr_replace($baseContent, $this->writeAccessors($accessorsStub, $_foreignKey->VARIABLE_NAME, $_foreignKey->ENTITY_DATA_TYPE), -1, 0);
131127
}
132128
}
133-
$entityContent .= "}";
134129

135-
file_put_contents("$relativeEntitiesPath/$entityName.php", $entityContent);
130+
file_put_contents($filenameWithPath, $baseContent);
136131

137-
shell_exec("git add $relativeEntitiesPath/$entityName.php");
132+
shell_exec('git add '.$filenameWithPath);
138133

139134
$this->info("Entity \"$entityName\" has been created.");
140135

src/Stubs/Entity/Accessors.stub

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
/**
3+
* @return AttributeType
4+
*/
5+
public function getGetterName(): AttributeType
6+
{
7+
return $this->AttributeName;
8+
}
9+
10+
/**
11+
* @param AttributeType $AttributeName
12+
*/
13+
public function setSetterName(AttributeType $AttributeName): void
14+
{
15+
$this->AttributeName = $AttributeName;
16+
}

src/Stubs/Entity/Class.stub

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EntityNameSpace;
4+
5+
class EntityName extends Entity
6+
{
7+
}

0 commit comments

Comments
 (0)