Skip to content

Commit 3ccc75a

Browse files
authored
Merge pull request #18 from mohammadRezaei1380/Feature_redis
add redis
2 parents dcbb444 + 7480c8a commit 3ccc75a

29 files changed

+407
-54
lines changed

src/Commands/BaseCommand.php

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

33
namespace Nanvaie\DatabaseRepository\Commands;
44

5+
use Illuminate\Container\Container;
56
use Illuminate\Support\Collection;
67
use Illuminate\Console\Command;
78
use Nanvaie\DatabaseRepository\CustomMySqlQueries;
@@ -35,6 +36,8 @@ class BaseCommand extends Command
3536
public string $relativeMysqlRepositoryPath;
3637
public string $mysqlRepositoryStubsPath;
3738

39+
public string $strategyName;
40+
3841
public function setArguments()
3942
{
4043
$this->selectedDb = $this->hasArgument('selected_db') && $this->argument('selected_db') ? $this->argument('selected_db') : config('repository.default_db');
@@ -63,7 +66,9 @@ public function setArguments()
6366
$this->mysqlRepositoryName = 'MySql' . $this->entityName . 'Repository';
6467
$this->relativeMysqlRepositoryPath = config('repository.path.relative.repositories') . "$this->entityName" . DIRECTORY_SEPARATOR;
6568
$this->mysqlRepositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.mysql');
66-
69+
if ($this->hasArgument('strategy')) {
70+
$this->strategyName = $this->argument('strategy');
71+
}
6772
}
6873

6974
public function checkDelete(string $filenameWithPath, string $entityName, string $objectName): void
@@ -99,15 +104,13 @@ public function finalized(string $filenameWithPath, string $entityName, string $
99104

100105
$this->info("\"$entityName\" has been created.");
101106
}
102-
103107
public function checkEmpty(Collection $columns, string $tableName): void
104108
{
105109
if ($columns->isEmpty()) {
106110
$this->alert("Couldn't retrieve columns from table \"$tableName\"! Perhaps table's name is misspelled.");
107111
exit;
108112
}
109113
}
110-
111114
public function setChoice($choice): void
112115
{
113116
\config(['replacement.choice' => $choice]);
@@ -117,4 +120,24 @@ public function getChoice(): null|string
117120
{
118121
return \config('replacement.choice');
119122
}
123+
124+
public function checkStrategyName()
125+
{
126+
$strategyNames = array("ClearableTemporaryCacheStrategy", "QueryCacheStrategy", "SingleKeyCacheStrategy", "TemporaryCacheStrategy");
127+
if (!in_array($this->argument('strategy'), $strategyNames)) {
128+
$this->alert("This pattern strategy does not exist !!! ");
129+
exit;
130+
}
131+
}
132+
public function checkDatabasesExist()
133+
{
134+
$entityName=Str::singular(ucfirst(Str::camel($this->argument('table_name'))));
135+
$mysql ="App\Models\Repositories". "\\". $entityName. "\\"."MySql".$entityName."Repository.php";
136+
$redis ="App\Models\Repositories". "\\". $entityName. "\\"."Redis".$entityName."Repository.php";
137+
if(!file_exists($mysql) && !file_exists($redis)) {
138+
$this->alert("First create the class databases!!!");
139+
exit;
140+
}
141+
}
142+
120143
}

src/Commands/MakeRedisRepository.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class MakeRedisRepository extends BaseCommand
1515
*
1616
* @var string
1717
*/
18-
protected $signature = 'repository:make-redis-repository {table_name}
18+
protected $signature = 'repository:make-redis-repository {table_name} {strategy}
1919
{--k|foreign-keys : Detect foreign keys}
2020
{--d|delete : Delete resource}
2121
{--f|force : Override/Delete existing redis repository}
@@ -35,9 +35,12 @@ class MakeRedisRepository extends BaseCommand
3535
*
3636
* @return int
3737
*/
38-
public function handle(): void
38+
public function handle()
3939
{
40+
41+
$this->checkStrategyName();
4042
$this->setArguments();
43+
4144
$redisRepositoryName = "Redis$this->entityName"."Repository";
4245
$redisRepositoryNamespace = config('repository.path.namespace.repositories');
4346
$relativeRedisRepositoryPath = config('repository.path.relative.repositories') . "$this->entityName" . DIRECTORY_SEPARATOR;
@@ -46,19 +49,17 @@ public function handle(): void
4649
$this->checkDelete($filenameWithPath,$redisRepositoryName,"Redis Repository");
4750
$this->checkDirectory($relativeRedisRepositoryPath);
4851
$this->checkClassExist($this->repositoryNamespace,$redisRepositoryName,"Redis Repository");
49-
5052
$columns = $this->getAllColumnsInTable($this->tableName);
5153
$this->checkEmpty($columns,$this->tableName);
5254

5355
if ($this->detectForeignKeys) {
5456
$foreignKeys = $this->extractForeignKeys($this->tableName);
5557
}
5658

57-
$mysqlRepoCreator = new CreatorRedisRepository($redisRepositoryName,$redisRepositoryNamespace, $this->entityName);
59+
$repositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.base');
60+
$mysqlRepoCreator = new CreatorRedisRepository($redisRepositoryName,$redisRepositoryNamespace, $this->entityName,$this->strategyName,$repositoryStubsPath);
5861
$creator = new BaseCreator($mysqlRepoCreator);
5962
$baseContent = $creator->createClass($filenameWithPath,$this);
60-
6163
$this->finalized($filenameWithPath, $redisRepositoryName, $baseContent);
62-
6364
}
6465
}

src/Commands/MakeRepository.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class MakeRepository extends BaseCommand
1515
*
1616
* @var string
1717
*/
18-
protected $signature = 'repository:make-repository {table_name} {selected_db?}
18+
protected $signature = 'repository:make-repository {table_name} {strategy} {selected_db?}
1919
{--k|foreign-keys : Detect foreign keys}
2020
{--d|delete : Delete resource}
2121
{--f|force : Override/Delete existing repository class}
@@ -37,23 +37,24 @@ class MakeRepository extends BaseCommand
3737
*/
3838
public function handle(): void
3939
{
40+
$this->checkDatabasesExist();
41+
$this->checkStrategyName();
42+
4043
$this->setArguments();
41-
// dd($this->selectedDb);
4244
$repositoryName = $this->entityName.'Repository';
4345
// $sqlRepositoryName = 'MySql'.$this->entityName.'Repository';
4446
$sqlRepositoryName = ucwords($this->selectedDb).$this->entityName.'Repository';
4547
$sqlRepositoryVariable = 'repository';
48+
$redisRepositoryVariable ='redisRepository';
49+
$redisRepositoryName = 'Redis'.$this->entityName.'Repository';
4650
$relativeRepositoryPath = config('repository.path.relative.repositories') . "$this->entityName" . DIRECTORY_SEPARATOR;
4751
$repositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.base');
4852
$filenameWithPath = $relativeRepositoryPath . $repositoryName . '.php';
49-
5053
$this->checkDelete($filenameWithPath,$repositoryName,"Repository");
5154
$this->checkDirectory($relativeRepositoryPath);
5255
$this->checkClassExist($this->repositoryNamespace,$repositoryName,"Repository");
53-
5456
$columns = $this->getAllColumnsInTable($this->tableName);
5557
$this->checkEmpty($columns,$this->tableName);
56-
5758
$RepoCreator = new CreatorRepository(
5859
$columns,
5960
$sqlRepositoryVariable,
@@ -67,11 +68,13 @@ public function handle(): void
6768
$repositoryName,
6869
$this->interfaceName,
6970
$this->repositoryNamespace,
70-
$this->selectedDb
71+
$this->selectedDb,
72+
$redisRepositoryVariable,
73+
$redisRepositoryName,
74+
$this->strategyName
7175
);
7276
$creator = new BaseCreator($RepoCreator);
7377
$baseContent = $creator->createClass($filenameWithPath,$this);
74-
7578
$this->finalized($filenameWithPath, $repositoryName, $baseContent);
7679
}
7780
}

src/Commands/MakeResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,5 @@ public function handle(): void
6565
$this->finalized($filenameWithPath,$resourceName,$baseContent);
6666

6767
}
68+
6869
}

src/Creators/CreatorRedisRepository.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
namespace Nanvaie\DatabaseRepository\Creators;
44

5+
use Illuminate\Support\Str;
6+
use Nanvaie\DatabaseRepository\CustomMySqlQueries;
7+
58
class CreatorRedisRepository implements IClassCreator
69
{
710
public function __construct(
811
public string $redisRepositoryName,
912
public string $redisRepositoryNamespace,
10-
public string $entityName
13+
public string $entityName,
14+
public string $strategyName,
15+
public string $repositoryStubsPath,
1116
)
1217
{
1318

@@ -18,22 +23,33 @@ public function getNameSpace(): string
1823
}
1924
public function createUses(): array
2025
{
21-
return ["use Nanvaie\DatabaseRepository\Models\Repositories\RedisRepository;"];
26+
return [
27+
"use Nanvaie\DatabaseRepository\Models\Repositories\RedisRepository;",
28+
"use App\Models\Repositories\Redis"."\\".$this->strategyName.";"
29+
];
2230
}
2331
public function getClassName(): string
2432
{
2533
return $this->redisRepositoryName;
2634
}
2735
public function getExtendSection(): string
2836
{
29-
return "extends RedisRepository implements IUserRepository";
37+
return "extends RedisRepository";
3038
}
3139
public function createAttributs(): array
3240
{
3341
return [];
3442
}
3543
public function createFunctions(): array
3644
{
37-
return [];
45+
$constructStub = file_get_contents($this->repositoryStubsPath . 'construct_redis.stub');
46+
$functions = [];
47+
$functions['__construct'] = $this->getConstructRedis($constructStub);
48+
return $functions ;
3849
}
50+
public function getConstructRedis(string $constructStub)
51+
{
52+
return str_replace("{{Strategy}}",$this->strategyName,$constructStub);
53+
}
54+
3955
}

src/Creators/CreatorRepository.php

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
use App\Models\Repositories\User\IUserRepository;
66
use Illuminate\Support\Collection;
77
use Nanvaie\DatabaseRepository\CustomMySqlQueries;
8+
use Nanvaie\DatabaseRepository\Commands;
9+
use Nanvaie\DatabaseRepository\Commands\MakeRedisRepository;
810
use Illuminate\Support\Str;
911

12+
1013
class CreatorRepository implements IClassCreator
1114
{
1215
public function __construct(
@@ -22,7 +25,10 @@ public function __construct(
2225
public string $repositoryName,
2326
public string $interfaceName,
2427
public string $repositoryNamespace,
25-
public string $selectedDb
28+
public string $selectedDb,
29+
public string $redisRepositoryVariable,
30+
public string $redisRepositoryName,
31+
public string $strategyName
2632
)
2733
{
2834
}
@@ -35,27 +41,41 @@ private function writeFunction(string $functionStub, string $functionName, strin
3541
$functionReturnType = 'null|{{ EntityName }}';
3642
$functionName .= ucfirst(Str::camel($columnName));
3743
$columnName = Str::camel($columnName);
44+
$redisCashFunction = $this->getRedisCashFunctionGetOneBy($this->strategyName);
45+
46+
3847
} elseif ($functionName === 'getAllBy') {
3948
$functionReturnType = 'Collection';
4049
$functionName .= ucfirst(Str::plural(Str::camel($columnName)));
4150
$columnName = Str::plural(Str::camel($columnName));
51+
$redisCashFunction = $this->getRedisCashFunctionGetAllBy($this->strategyName);
52+
53+
4254
} elseif ($functionName === 'create') {
4355
$functionReturnType = $attributeType;
56+
$redisCashFunction = $this->getRedisCashFunctionCreate($this->strategyName);
57+
4458
} elseif (in_array($functionName, ['update', 'remove', 'restore'])) {
4559
$functionReturnType = 'int';
46-
}
60+
$redisCashFunction = $this->getRedisCashFunctionUpdate($this->strategyName);
4761

48-
return str_replace(['{{ FunctionName }}', '{{ AttributeType }}', '{{ AttributeName }}', '{{ FunctionReturnType }}'],
49-
[$functionName, $attributeType, Str::camel($columnName), $functionReturnType],
62+
}
63+
return str_replace(['{{ FunctionName }}', '{{ AttributeType }}', '{{ AttributeName }}', '{{ FunctionReturnType }}','{{redisFunction}}'],
64+
[$functionName, $attributeType, Str::camel($columnName), $functionReturnType,$redisCashFunction],
5065
$functionStub);
5166
}
52-
5367
private function writeSqlAttribute(string $attributeStub, string $sqlRepositoryVariable, string $sqlRepositoryName): string
5468
{
55-
return str_replace(['{{ SqlRepositoryVariable }}', '{{ SqlRepositoryName }}'],
69+
return str_replace(['{{ SqlRepositoryVariable }}', '{{ SqlRepositoryName }}'],
5670
[$sqlRepositoryVariable, $sqlRepositoryName],
5771
$attributeStub);
5872
}
73+
public function writeRedisAttribute(string $attributeStub,string $redisRepositoryVariable,string $redisRepositoryName):string
74+
{
75+
return str_replace(['{{ RedisRepositoryVariable }}', '{{ RedisRepositoryName }}'],
76+
[$redisRepositoryVariable, $redisRepositoryName],
77+
$attributeStub);
78+
}
5979

6080
public function getNameSpace(): string
6181
{
@@ -85,6 +105,7 @@ public function createAttributs(): array
85105
$attributeSqlStub = file_get_contents($this->repositoryStubsPath . 'attribute.sql.stub');
86106
$attributes = [];
87107
$attributes['repository'] = 'private '.$this->interfaceName.' $repository;';
108+
$attributes['redisRepository'] = 'private '.$this->redisRepositoryName.' $redisRepository;';
88109
return $attributes;
89110
}
90111

@@ -93,9 +114,9 @@ public function createFunctions(): array
93114
$constructStub = file_get_contents($this->repositoryStubsPath . 'construct.stub');
94115
$functionStub = file_get_contents($this->repositoryStubsPath . 'function.stub');
95116
$setterSqlStub = file_get_contents($this->repositoryStubsPath . 'setter.sql.stub');
96-
97117
$functions = [];
98118
$functions['__construct'] = $this->getConstruct($setterSqlStub, $constructStub);
119+
$functions['__construct'] = $this->getConstructRedis($setterSqlStub, $constructStub);
99120
$functions['getOneById'] = $this->writeFunction($functionStub, 'getOneBy', 'id', 'int');
100121
$functions['getAllByIds'] = $this->writeFunction($functionStub, 'getAllBy', 'id', 'array');
101122
$indexes = $this->extractIndexes($this->tableName);
@@ -105,7 +126,6 @@ public function createFunctions(): array
105126
$fun_name = ucfirst(Str::camel($index->COLUMN_NAME));
106127
$functions['getOneBy' . $fun_name] = $this->writeFunction($functionStub, 'getOneBy', $index->COLUMN_NAME, 'int');
107128
}
108-
109129
if ($this->detectForeignKeys) {
110130
$foreignKeys = $this->extractForeignKeys($this->tableName);
111131

@@ -116,14 +136,12 @@ public function createFunctions(): array
116136
$functions['getAllBy' . $fun_name] = $this->writeFunction($functionStub, 'getAllBy', $_foreignKey->COLUMN_NAME, 'array');
117137
}
118138
}
119-
120139
$functions['create'] = $this->writeFunction($functionStub, 'create', $this->entityVariableName, $this->entityName);
121140
$functions['update'] = $this->writeFunction($functionStub, 'update', $this->entityVariableName, $this->entityName);
122141
if (in_array('deleted_at', $this->columns->pluck('COLUMN_NAME')->toArray(), true)) {
123142
$functions['remove'] = $this->writeFunction($functionStub, 'remove', $this->entityVariableName, $this->entityName);
124143
$functions['restore'] = $this->writeFunction($functionStub, 'restore', $this->entityVariableName, $this->entityName);
125144
}
126-
127145
foreach ($functions as &$func) {
128146
$func = str_replace(["{{ SqlRepositoryVariable }}", '{{ SqlRepositoryName }}', '{{ EntityName }}'],
129147
[$this->sqlRepositoryVariable, $this->sqlRepositoryName, $this->entityName],
@@ -132,9 +150,52 @@ public function createFunctions(): array
132150
}
133151
return $functions;
134152
}
135-
136153
public function getConstruct(string $setterSqlStub, string $constructStub)
137154
{
138-
return str_replace("{{ Setters }}", $this->writeSqlAttribute($setterSqlStub, $this->sqlRepositoryVariable, $this->sqlRepositoryName), $constructStub);
155+
return str_replace("{{ Setters }}", $this->writeSqlAttribute($setterSqlStub, $this->sqlRepositoryVariable, $this->sqlRepositoryName,$this->redisRepositoryVariable,$this->redisRepositoryName), $constructStub);
156+
}
157+
public function getConstructRedis(string $setterSqlStub, string $constructStub)
158+
{
159+
return str_replace("{{ Setters }}", $this->writeRedisAttribute($setterSqlStub,$this->redisRepositoryVariable,$this->redisRepositoryName), $constructStub);
160+
}
161+
private function getRedisCashFunctionGetOneBy($strategyName)
162+
{
163+
$repositoryRedisStubsPath=__DIR__ . '/../../'.'stubs/Repositories/Redis/getOneBy/base.';
164+
return match ($strategyName) {
165+
'QueryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'query_cache_strategy.stub'),
166+
'SingleKeyCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'single_key_cache_strategy.stub'),
167+
'ClearableTemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'clearable_temporary_cache_strategy.stub'),
168+
'TemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'base.temporary_cache_strategy.stub'),
169+
};
170+
}
171+
private function getRedisCashFunctionGetAllBy($strategyName)
172+
{
173+
$repositoryRedisStubsPath=__DIR__ . '/../../'.'stubs/Repositories/Redis/getAllBy/base.';
174+
return match ($strategyName) {
175+
'QueryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'query_cache_strategy.stub'),
176+
'SingleKeyCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'single_key_cache_strategy.stub'),
177+
'ClearableTemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'clearable_temporary_cache_strategy.stub'),
178+
'TemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'base.temporary_cache_strategy.stub'),
179+
};
180+
}
181+
private function getRedisCashFunctionCreate($strategyName)
182+
{
183+
$repositoryRedisStubsPath=__DIR__ . '/../../'.'stubs/Repositories/Redis/create/base.';
184+
return match ($strategyName) {
185+
'QueryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'query_cache_strategy.stub'),
186+
'SingleKeyCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'single_key_cache_strategy.stub'),
187+
'ClearableTemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'clearable_temporary_cache_strategy.stub'),
188+
'TemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'base.temporary_cache_strategy.stub'),
189+
};
190+
}
191+
private function getRedisCashFunctionUpdate($strategyName)
192+
{
193+
$repositoryRedisStubsPath=__DIR__ . '/../../'.'stubs/Repositories/Redis/update/base.';
194+
return match ($strategyName) {
195+
'QueryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'query_cache_strategy.stub'),
196+
'SingleKeyCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'single_key_cache_strategy.stub'),
197+
'ClearableTemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'clearable_temporary_cache_strategy.stub'),
198+
'TemporaryCacheStrategy' => file_get_contents($repositoryRedisStubsPath . 'base.temporary_cache_strategy.stub'),
199+
};
139200
}
140201
}

0 commit comments

Comments
 (0)