55use App \Models \Repositories \User \IUserRepository ;
66use Illuminate \Support \Collection ;
77use Nanvaie \DatabaseRepository \CustomMySqlQueries ;
8+ use Nanvaie \DatabaseRepository \Commands ;
9+ use Nanvaie \DatabaseRepository \Commands \MakeRedisRepository ;
810use Illuminate \Support \Str ;
911
12+
1013class 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