Skip to content

Commit 0f95a4d

Browse files
authored
Merge pull request #35 from saMahmoudzadeh/refactor/creators
modify and Refactor creators classes
2 parents cc9d8b6 + c8d307d commit 0f95a4d

File tree

8 files changed

+196
-126
lines changed

8 files changed

+196
-126
lines changed

src/Commands/BaseCommand.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
namespace Eghamat24\DatabaseRepository\Commands;
44

5-
use Illuminate\Container\Container;
65
use Illuminate\Support\Collection;
76
use Illuminate\Console\Command;
8-
use Eghamat24\DatabaseRepository\CustomMySqlQueries;
97
use Illuminate\Support\Str;
108

11-
class BaseCommand extends Command
9+
abstract class BaseCommand extends Command
1210
{
13-
// use CustomMySqlQueries;
1411
public string $selectedDb;
1512
public string $tableName;
1613
public string $detectForeignKeys;
@@ -74,7 +71,7 @@ public function setArguments()
7471
public function checkDelete(string $filenameWithPath, string $entityName, string $objectName): void
7572
{
7673
if (file_exists($filenameWithPath) && $this->option('delete')) {
77-
unlink($filenameWithPath);
74+
\unlink($filenameWithPath);
7875
$this->info("$objectName '$entityName' has been deleted.");
7976
}
8077
}
@@ -125,19 +122,25 @@ public function getChoice(): null|string
125122

126123
public function checkStrategyName()
127124
{
128-
$strategyNames = array("ClearableTemporaryCacheStrategy", "QueryCacheStrategy", "SingleKeyCacheStrategy", "TemporaryCacheStrategy");
125+
$strategyNames = [
126+
'ClearableTemporaryCacheStrategy',
127+
'QueryCacheStrategy',
128+
'SingleKeyCacheStrategy',
129+
'TemporaryCacheStrategy'
130+
];
131+
129132
if (!in_array($this->argument('strategy'), $strategyNames)) {
130-
$this->alert("This pattern strategy does not exist !!! ");
133+
$this->alert('This pattern strategy does not exist !!! ');
131134
exit;
132135
}
133136
}
134137

135138
public function checkDatabasesExist()
136139
{
137-
138140
$entityName = Str::singular(ucfirst(Str::camel($this->argument('table_name'))));
139-
$mysql = config('repository.path.relative.repositories') . DIRECTORY_SEPARATOR . $entityName . DIRECTORY_SEPARATOR . "MySql" . $entityName . "Repository.php";
140-
$redis = config('repository.path.relative.repositories') . DIRECTORY_SEPARATOR . $entityName . DIRECTORY_SEPARATOR . "Redis" . $entityName . "Repository.php";
141+
$mysql = config('repository.path.relative.repositories') . DIRECTORY_SEPARATOR . $entityName . DIRECTORY_SEPARATOR . 'MySql' . $entityName . 'Repository.php';
142+
$redis = config('repository.path.relative.repositories') . DIRECTORY_SEPARATOR . $entityName . DIRECTORY_SEPARATOR . 'Redis' . $entityName . 'Repository.php';
143+
141144
if (!(file_exists($mysql) && file_exists($redis))) {
142145
$this->alert("First create the class databases!!!");
143146
exit;

src/Creators/BaseCreator.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
namespace Eghamat24\DatabaseRepository\Creators;
44

5-
use Illuminate\Support\Str;
6-
use Illuminate\Console\Command;
75
use Eghamat24\DatabaseRepository\Commands\BaseCommand;
8-
use Eghamat24\DatabaseRepository\Utility;
9-
use function Eghamat24\DatabaseRepository\Commands\config;
106

117
class BaseCreator extends BaseCommand
128
{
@@ -17,10 +13,10 @@ class BaseCreator extends BaseCommand
1713
const SINGLE_KEY_CACHE_STRATEGY = 'SingleKeyCacheStrategy';
1814

1915
private $creator;
20-
private null|string $choice = null;
2116

2217
public function __construct(IClassCreator $creator)
2318
{
19+
parent::__construct();
2420
$this->creator = $creator;
2521
}
2622

src/Creators/CreatorEntity.php

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Eghamat24\DatabaseRepository\Creators;
44

5+
use Eghamat24\DatabaseRepository\Models\Enums\DataTypeEnum;
56
use Illuminate\Support\Collection;
67
use Eghamat24\DatabaseRepository\CustomMySqlQueries;
78

@@ -31,25 +32,21 @@ public function getExtendSection(): string
3132

3233
public function createAttributes(): array
3334
{
34-
$columns = $this->columns;
35-
$entityStubsPath = $this->entityStubsPath;
36-
$detectForeignKeys = $this->detectForeignKeys;
37-
$tableName = $this->tableName;
3835
$attributes = [];
3936

40-
foreach ($columns as $_column) {
37+
foreach ($this->columns as $_column) {
4138

4239
$dataType = $this->getDataType($_column->COLUMN_TYPE, $_column->DATA_TYPE);
4340

4441
$defaultValue = null;
4542
if ($_column->COLUMN_DEFAULT !== null) {
4643
$defaultValue = $_column->COLUMN_DEFAULT;
4744

48-
if ($dataType == 'int') {
45+
if ($dataType === DataTypeEnum::INTEGER_TYPE) {
4946
$defaultValue = intval($defaultValue);
5047
}
5148

52-
if ($dataType == self::BOOL_TYPE) {
49+
if ($dataType === self::BOOL_TYPE) {
5350
if (in_array($defaultValue, [0, '', "''"])) {
5451
$defaultValue = 'false';
5552
} elseif (in_array($defaultValue, [1, '1'])) {
@@ -59,29 +56,31 @@ public function createAttributes(): array
5956
}
6057

6158
$columnString = $_column->COLUMN_NAME;
59+
6260
if (!in_array($_column->COLUMN_DEFAULT, [null, 'NULL'])) {
6361
$columnString .= ' = ' . $defaultValue;
6462
}
63+
6564
if ($_column->IS_NULLABLE === 'YES') {
6665
$columnString .= ' = null';
6766
}
6867

6968
$attributes[$_column->COLUMN_NAME] =
7069
$this->writeAttribute(
71-
$entityStubsPath,
70+
$this->entityStubsPath,
7271
$columnString,
7372
($_column->IS_NULLABLE === 'YES' ? 'null|' : '') . $dataType
7473
);
7574
}
7675

77-
if ($detectForeignKeys) {
78-
$foreignKeys = $this->extractForeignKeys($tableName);
76+
if ($this->detectForeignKeys) {
77+
$foreignKeys = $this->extractForeignKeys($this->tableName);
7978

8079
// Create Additional Attributes from Foreign Keys
8180
foreach ($foreignKeys as $_foreignKey) {
8281
$attributes[$_column->COLUMN_NAME] =
8382
$this->writeAttribute(
84-
$entityStubsPath,
83+
$this->entityStubsPath,
8584
$_foreignKey->VARIABLE_NAME,
8685
$_foreignKey->ENTITY_DATA_TYPE
8786
);
@@ -93,73 +92,85 @@ public function createAttributes(): array
9392

9493
public function createUses(): array
9594
{
96-
return ["use Eghamat24\DatabaseRepository\Models\Entity\Entity;"];
95+
return ['use Eghamat24\DatabaseRepository\Models\Entity\Entity;'];
9796
}
9897

9998
public function createFunctions(): array
10099
{
101-
$columns = $this->columns;
102-
$entityStubsPath = $this->entityStubsPath;
103-
$detectForeignKeys = $this->detectForeignKeys;
104-
$tableName = $this->tableName;
105100
$settersAndGetters = [];
106-
foreach ($columns as $_column) {
101+
102+
foreach ($this->columns as $_column) {
107103
$dataType = $this->getDataType($_column->COLUMN_TYPE, $_column->DATA_TYPE);
108104

109105
$settersAndGetters['get' . ucwords($_column->COLUMN_NAME)] =
110106
$this->writeAccessors(
111-
$entityStubsPath,
107+
$this->entityStubsPath,
112108
$_column->COLUMN_NAME,
113109
($_column->IS_NULLABLE === 'YES' ? 'null|' : '') . $dataType,
114110
'getter'
115111
);
112+
116113
$settersAndGetters['set' . ucwords($_column->COLUMN_NAME)] =
117114
$this->writeAccessors(
118-
$entityStubsPath,
115+
$this->entityStubsPath,
119116
$_column->COLUMN_NAME,
120117
($_column->IS_NULLABLE === 'YES' ? 'null|' : '') . $dataType,
121118
'setter'
122119
);
123120

124121
}
125-
if ($detectForeignKeys) {
126-
$foreignKeys = $this->extractForeignKeys($tableName);
122+
123+
if ($this->detectForeignKeys) {
124+
$foreignKeys = $this->extractForeignKeys($this->tableName);
127125

128126
// Create Additional Setters and Getters from Foreign keys
129127
foreach ($foreignKeys as $_foreignKey) {
128+
130129
$settersAndGetters['get' . ucwords($_foreignKey->COLUMN_NAME)] =
131130
$this->writeAccessors(
132-
$entityStubsPath,
131+
$this->entityStubsPath,
133132
$_foreignKey->VARIABLE_NAME,
134133
$_foreignKey->ENTITY_DATA_TYPE,
135134
'getter'
136135
);
136+
137137
$settersAndGetters['set' . ucwords($_foreignKey->COLUMN_NAME)] =
138138
$this->writeAccessors(
139-
$entityStubsPath,
139+
$this->entityStubsPath,
140140
$_foreignKey->VARIABLE_NAME,
141141
$_foreignKey->ENTITY_DATA_TYPE,
142142
'setter'
143143
);
144144
}
145145
}
146+
146147
return $settersAndGetters;
147148
}
148149

149150
private function writeAttribute(string $entityStubsPath, string $attributeName, string $attributeType): string
150151
{
151152
$attributeStub = file_get_contents($entityStubsPath . 'attribute.stub');
152-
return str_replace(['{{ AttributeType }}', '{{ AttributeName }}'],
153-
[$attributeType, $attributeName],
154-
$attributeStub);
153+
154+
$replaceMapping = [
155+
'{{ AttributeType }}' => $attributeType,
156+
'{{ AttributeName }}' => $attributeName,
157+
];
158+
159+
return str_replace(array_keys($replaceMapping), array_values($replaceMapping), $attributeStub);
155160
}
156161

157162
private function writeAccessors(string $entityStubsPath, string $attributeName, string $attributeType, string $type): string
158163
{
159164
$accessorStub = file_get_contents($entityStubsPath . $type . '.stub');
160-
return str_replace(['{{ AttributeType }}', '{{ AttributeName }}', '{{ GetterName }}', '{{ SetterName }}'],
161-
[$attributeType, $attributeName, ucfirst($attributeName), ucfirst($attributeName)],
162-
$accessorStub);
165+
166+
$replaceMapping = [
167+
'{{ AttributeType }}' => $attributeType,
168+
'{{ AttributeName }}' => $attributeName,
169+
'{{ GetterName }}' => ucfirst($attributeName),
170+
'{{ SetterName }}' => ucfirst($attributeName)
171+
];
172+
173+
return str_replace(array_keys($replaceMapping), array_values($replaceMapping), $accessorStub);
163174
}
164175

165176
public function getNameSpace(): string

src/Creators/CreatorEnum.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Eghamat24\DatabaseRepository\Creators;
44

55
use Illuminate\Support\Collection;
6-
use Eghamat24\DatabaseRepository\Commands\MakeEnum;
76

87
class CreatorEnum implements IClassCreator
98
{
@@ -22,12 +21,14 @@ public function createAttributes(): array
2221
{
2322
$attributes = [];
2423
foreach ($this->enum as $_enum) {
24+
2525
$attributes[strtoupper($_enum)] = $this->writeAttribute(
2626
$this->attributeStub,
2727
strtoupper($_enum),
2828
$_enum
2929
);
3030
}
31+
3132
return $attributes;
3233
}
3334

@@ -58,8 +59,11 @@ public function getClassName(): string
5859

5960
private function writeAttribute(string $attributeStub, string $attributeName, string $attributeString): string
6061
{
61-
return str_replace(['{{ AttributeName }}', '{{ AttributeString }}'],
62-
[$attributeName, $attributeString],
63-
$attributeStub);
62+
$replaceMapping = [
63+
'{{ AttributeName }}' => $attributeName,
64+
'{{ AttributeString }}' => $attributeString,
65+
];
66+
67+
return str_replace(array_keys($replaceMapping), array_values($replaceMapping), $attributeStub);
6468
}
6569
}

src/Creators/CreatorFactory.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function createFunctions(): array
3939
foreach ($this->columns as $_column) {
4040
$setterFunctions .= trim($this->writeSetter($setterStub, $_column->COLUMN_NAME)) . "\n\t\t";
4141
}
42+
4243
return ['makeEntityFromStdClass' =>
4344
str_replace(['{{ SetterFunctions }}', '{{ EntityName }}', '{{ EntityVariableName }}'],
4445
[$setterFunctions, $this->entityName, $this->entityVariableName],
@@ -50,8 +51,8 @@ public function createUses(): array
5051
{
5152
return [
5253
"use $this->entityNamespace\\$this->entityName;",
53-
"use Eghamat24\DatabaseRepository\Models\Factories\Factory;",
54-
"use stdClass;"
54+
'use Eghamat24\DatabaseRepository\Models\Factories\Factory;',
55+
'use stdClass;'
5556
];
5657

5758
}
@@ -63,9 +64,12 @@ public function getExtendSection(): string
6364

6465
public function writeSetter(string $setterStub, string $columnName): string
6566
{
66-
return str_replace(['{{ SetterName }}', '{{ AttributeName }}'],
67-
[ucfirst($columnName), Str::snake($columnName)],
68-
$setterStub);
67+
$replacementTokens = [
68+
'{{ SetterName }}' => ucfirst($columnName),
69+
'{{ AttributeName }}' => Str::snake($columnName)
70+
];
71+
72+
return str_replace(array_keys($replacementTokens), array_values($replacementTokens), $setterStub);
6973
}
7074

7175
public function getClassName(): string

0 commit comments

Comments
 (0)