Skip to content

Commit ca106b1

Browse files
authored
Merge pull request #25 from farshadth/refactor
refactor and add table name to cache tag
2 parents 775fb00 + ae3a6c9 commit ca106b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+210
-210
lines changed

src/Commands/MakeAll.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ class MakeAll extends Command
3737
*/
3838
public function handle()
3939
{
40-
41-
4240
$strategyNames = array("ClearableTemporaryCacheStrategy", "QueryCacheStrategy", "SingleKeyCacheStrategy", "TemporaryCacheStrategy");
4341
if (!in_array($this->option('strategy_name'), $strategyNames)) {
4442
$this->alert("This pattern strategy does not exist !!! ");

src/Commands/MakeInterfaceRepository.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,28 +70,28 @@ public function handle(): void
7070
$updateFunctionStub = file_get_contents($this->interfaceRepositoryStubsPath . 'update.stub');
7171
$deleteAndUndeleteStub = file_get_contents($this->interfaceRepositoryStubsPath . 'deleteAndUndelete.stub');
7272

73-
$baseContent = substr_replace($baseContent, $this->writeGetOneFunction($getOneStub, 'id', 'int'), -1, 0);
74-
$baseContent = substr_replace($baseContent, $this->writeGetAllFunction($getAllStub, 'id', 'int'), -1, 0);
73+
$baseContent = substr_replace($baseContent, $this->writeGetOneFunction($getOneStub, 'id', 'int'), -2, 0);
74+
$baseContent = substr_replace($baseContent, $this->writeGetAllFunction($getAllStub, 'id', 'int'), -2, 0);
7575

7676
if ($this->detectForeignKeys) {
7777
foreach ($foreignKeys as $_foreignKey) {
78-
$baseContent = substr_replace($baseContent, $this->writeGetOneFunction($getOneStub, $_foreignKey->COLUMN_NAME, $this->entityName), -1, 0);
79-
$baseContent = substr_replace($baseContent, $this->writeGetAllFunction($getAllStub, $_foreignKey->COLUMN_NAME, $this->entityName), -1, 0);
78+
$baseContent = substr_replace($baseContent, $this->writeGetOneFunction($getOneStub, $_foreignKey->COLUMN_NAME, $this->entityName), -2, 0);
79+
$baseContent = substr_replace($baseContent, $this->writeGetAllFunction($getAllStub, $_foreignKey->COLUMN_NAME, $this->entityName), -2, 0);
8080
}
8181
}
8282

8383
$allColumns = $columns->pluck('COLUMN_NAME')->toArray();
8484

8585
if (in_array('created_at', $allColumns, true)) {
86-
$baseContent = substr_replace($baseContent, $createFunctionStub, -1, 0);
86+
$baseContent = substr_replace($baseContent, $createFunctionStub, -2, 0);
8787
}
8888

8989
if (in_array('updated_at', $allColumns, true)) {
90-
$baseContent = substr_replace($baseContent, $updateFunctionStub, -1, 0);
90+
$baseContent = substr_replace($baseContent, $updateFunctionStub, -2, 0);
9191
}
9292

9393
if (in_array('deleted_at', $allColumns, true)) {
94-
$baseContent = substr_replace($baseContent, $deleteAndUndeleteStub, -1, 0);
94+
$baseContent = substr_replace($baseContent, $deleteAndUndeleteStub, -2, 0);
9595
}
9696

9797
$baseContent = str_replace(['{{ EntityName }}', '{{ EntityNamespace }}', '{{ EntityVariableName }}', '{{ InterfaceRepositoryName }}', '{{ RepositoryNamespace }}'],

src/Creators/BaseCreator.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class BaseCreator extends BaseCommand
1313
const ENUM_TYPE = 'enum';
1414
const CLASS_TYPE = 'class';
1515
const ALL_OPTIONS = ['Current','New','Always keep current','Always replace with new'];
16+
const QUERY_CACHE_STRATEGY = 'QueryCacheStrategy';
17+
const SINGLE_KEY_CACHE_STRATEGY = 'SingleKeyCacheStrategy';
1618

1719
private $creator;
1820
private null|string $choice=null;
@@ -35,20 +37,22 @@ public function createClass(string $filenameWithPath,BaseCommand $command):strin
3537
$attributesArray = $this->checkDiffrence($filenameWithPath,$attributesArray,$command,$specificPattern,$generalPattern);
3638

3739
$attributes = trim(implode("\n\t",$attributesArray));
38-
$attributes = (!empty($functionsArray)) ? $attributes."\n" : $attributes;
39-
$functions = implode(' ',$functionsArray);
40+
$functions = trim(implode("\n",$functionsArray));
41+
$functions = (!empty($attributes)) ? "\n\n\t".$functions : $functions;
4042
$uses = implode(PHP_EOL,$usesArray);
4143

4244
$type = (isset($this->creator->enum)) ? self::ENUM_TYPE : self::CLASS_TYPE;
4345
$basePath = __DIR__ . "/../../stubs/base.$type.stub" ;
44-
$this->creator->baseContent = str_replace(['{{ Namespace }}', '{{ UseSection }}', '{{ ClassName }}', '{{ ExtendSection }}', '{{ Parameters }}', '{{ Functions }}'],
46+
47+
$this->creator->baseContent = str_replace(['{{ Namespace }}', '{{ UseSection }}', '{{ ClassName }}', '{{ ExtendSection }}', '{{ Parameters }}', '{{ Functions }}', '{{ CacheTag }}'],
4548
[
4649
$this->creator->getNameSpace(),
4750
$uses,
4851
$this->creator->getClassName(),
4952
$this->creator->getExtendSection(),
5053
$attributes,
51-
$functions
54+
$functions,
55+
$this->setCachetag($command)
5256
],
5357
file_get_contents($basePath));
5458

@@ -100,4 +104,9 @@ public function checkDiffrence(string $filenameWithPath,array $newParamsArray,Ba
100104
}
101105
return $newParamsArray;
102106
}
107+
108+
private function setCacheTag(BaseCommand $command)
109+
{
110+
return (isset($command->strategyName) && in_array($command->strategyName, [self::QUERY_CACHE_STRATEGY, self::SINGLE_KEY_CACHE_STRATEGY])) ? "'$command->tableName'" : "''";
111+
}
103112
}

src/Creators/CreatorFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function createFunctions(): array
3737
$setterStub = file_get_contents($this->factoryStubsPath . 'setter.stub');
3838
$setterFunctions = '';
3939
foreach ($this->columns as $_column) {
40-
$setterFunctions .= $this->writeSetter($setterStub, $_column->COLUMN_NAME);
40+
$setterFunctions .= trim($this->writeSetter($setterStub, $_column->COLUMN_NAME))."\n\t\t";
4141
}
4242
return ['makeEntityFromStdClass' =>
4343
str_replace(['{{ SetterFunctions }}', '{{ EntityName }}', '{{ EntityVariableName }}'],

src/Creators/CreatorMySqlRepository.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ public function createFunctions(): array
9797
// Create "create" function
9898
foreach ($this->columns as $_column) {
9999
if (!in_array($_column->COLUMN_NAME, ['id', 'deleted_at'])) {
100-
$getterFunctions .= $this->writeGetterFunction($getterStub, $_column->COLUMN_NAME);
100+
$getterFunctions .= trim($this->writeGetterFunction($getterStub, $_column->COLUMN_NAME))."\n\t\t\t\t";
101101
}
102102
if (in_array($_column->COLUMN_NAME, ['created_at', 'updated_at'], true)) {
103-
$setterFunctions .= $this->writeSetterFunction($setterStub, $_column->COLUMN_NAME);
103+
$setterFunctions .= trim($this->writeSetterFunction($setterStub, $_column->COLUMN_NAME))."\n\t\t";
104104
}
105105
}
106106
$createFunctionStub = str_replace(["{{ GetterFunctions }}", "{{ SetterFunctions }}"],
107-
[substr($getterFunctions, 0, -1), substr($setterFunctions, 0, -1)],
107+
[trim(substr($getterFunctions, 0, -1)), trim(substr($setterFunctions, 0, -1))],
108108
$createFunctionStub
109109
);
110110

@@ -115,14 +115,14 @@ public function createFunctions(): array
115115
// Create "update" function
116116
foreach ($this->columns as $_column) {
117117
if (!in_array($_column->COLUMN_NAME, ['id', 'created_at', 'deleted_at'])) {
118-
$getterFunctions .= $this->writeGetterFunction($getterStub, $_column->COLUMN_NAME);
118+
$getterFunctions .= trim($this->writeGetterFunction($getterStub, $_column->COLUMN_NAME))."\n\t\t\t\t";
119119
}
120120
if ($_column->COLUMN_NAME === 'updated_at') {
121-
$setterFunctions .= $this->writeSetterFunction($setterStub, $_column->COLUMN_NAME);
121+
$setterFunctions .= trim($this->writeSetterFunction($setterStub, $_column->COLUMN_NAME))."\n\t\t";
122122
}
123123
}
124124
$updateFunctionStub = str_replace(["{{ GetterFunctions }}", "{{ UpdateFieldSetter }}"],
125-
[substr($getterFunctions, 0, -1), substr($setterFunctions, 0, -1)],
125+
[trim(substr($getterFunctions, 0, -1)), trim(substr($setterFunctions, 0, -1))],
126126
$updateFunctionStub
127127
);
128128

@@ -133,6 +133,7 @@ public function createFunctions(): array
133133
$functions['remove'] = $deleteStub;
134134
$functions['restore'] = $undeleteStub;
135135
}
136+
136137
foreach ($functions as &$func) {
137138
$func = str_replace(["{{ EntityName }}", "{{ EntityVariableName }}"],
138139
[$this->entityName, $this->entityVariableName],

src/Creators/CreatorRepository.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ public function createFunctions(): array
152152
}
153153
public function getConstruct(string $setterSqlStub, string $constructStub)
154154
{
155-
return str_replace("{{ Setters }}", $this->writeSqlAttribute($setterSqlStub, $this->sqlRepositoryVariable, $this->sqlRepositoryName,$this->redisRepositoryVariable,$this->redisRepositoryName), $constructStub);
155+
return str_replace("{{ Setters }}", trim($this->writeSqlAttribute($setterSqlStub, $this->sqlRepositoryVariable, $this->sqlRepositoryName,$this->redisRepositoryVariable,$this->redisRepositoryName)), $constructStub);
156156
}
157157
public function getConstructRedis(string $setterSqlStub, string $constructStub)
158158
{
159-
return str_replace("{{ Setters }}", $this->writeRedisAttribute($setterSqlStub,$this->redisRepositoryVariable,$this->redisRepositoryName), $constructStub);
159+
return str_replace("{{ Setters }}", trim($this->writeRedisAttribute($setterSqlStub,$this->redisRepositoryVariable,$this->redisRepositoryName)), $constructStub);
160160
}
161161
private function getRedisCashFunctionGetOneBy($strategyName)
162162
{
@@ -165,7 +165,7 @@ private function getRedisCashFunctionGetOneBy($strategyName)
165165
'QueryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'query_cache_strategy.stub'),
166166
'SingleKeyCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'single_key_cache_strategy.stub'),
167167
'ClearableTemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'clearable_temporary_cache_strategy.stub'),
168-
'TemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'base.temporary_cache_strategy.stub'),
168+
'TemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'temporary_cache_strategy.stub'),
169169
};
170170
}
171171
private function getRedisCashFunctionGetAllBy($strategyName)
@@ -175,7 +175,7 @@ private function getRedisCashFunctionGetAllBy($strategyName)
175175
'QueryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'query_cache_strategy.stub'),
176176
'SingleKeyCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'single_key_cache_strategy.stub'),
177177
'ClearableTemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'clearable_temporary_cache_strategy.stub'),
178-
'TemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'base.temporary_cache_strategy.stub'),
178+
'TemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'temporary_cache_strategy.stub'),
179179
};
180180
}
181181
private function getRedisCashFunctionCreate($strategyName)
@@ -185,7 +185,7 @@ private function getRedisCashFunctionCreate($strategyName)
185185
'QueryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'query_cache_strategy.stub'),
186186
'SingleKeyCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'single_key_cache_strategy.stub'),
187187
'ClearableTemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'clearable_temporary_cache_strategy.stub'),
188-
'TemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'base.temporary_cache_strategy.stub'),
188+
'TemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'temporary_cache_strategy.stub'),
189189
};
190190
}
191191
private function getRedisCashFunctionUpdate($strategyName)
@@ -195,7 +195,7 @@ private function getRedisCashFunctionUpdate($strategyName)
195195
'QueryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'query_cache_strategy.stub'),
196196
'SingleKeyCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'single_key_cache_strategy.stub'),
197197
'ClearableTemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'clearable_temporary_cache_strategy.stub'),
198-
'TemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'base.temporary_cache_strategy.stub'),
198+
'TemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'temporary_cache_strategy.stub'),
199199
};
200200
}
201201
}

stubs/Entities/entity.getter.stub

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
public function get{{ GetterName }}(): {{ AttributeType }}
1+
public function get{{ GetterName }}(): {{ AttributeType }}
32
{
43
return $this->{{ AttributeName }};
54
}

stubs/Entities/entity.setter.stub

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
public function set{{ SetterName }}({{ AttributeType }} ${{ AttributeName }}): void
1+
public function set{{ SetterName }}({{ AttributeType }} ${{ AttributeName }}): void
32
{
43
$this->{{ AttributeName }} = ${{ AttributeName }};
54
}

stubs/Factories/factory.class.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
public function makeEntityFromStdClass(stdClass $entity): {{ EntityName }}
1+
public function makeEntityFromStdClass(stdClass $entity): {{ EntityName }}
22
{
33
${{ EntityVariableName }} = new {{ EntityName }}();
44

5-
{{ SetterFunctions }}
5+
{{ SetterFunctions }}
66
return ${{ EntityVariableName }};
77
}

stubs/Factories/factory.setter.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
${{ EntityVariableName }}->set{{ SetterName }}($entity->{{ AttributeName }});
1+
${{ EntityVariableName }}->set{{ SetterName }}($entity->{{ AttributeName }});

0 commit comments

Comments
 (0)