5
5
use App \Models \Repositories \User \IUserRepository ;
6
6
use Illuminate \Support \Collection ;
7
7
use Nanvaie \DatabaseRepository \CustomMySqlQueries ;
8
+ use Nanvaie \DatabaseRepository \Commands ;
9
+ use Nanvaie \DatabaseRepository \Commands \MakeRedisRepository ;
8
10
use Illuminate \Support \Str ;
9
11
12
+
10
13
class CreatorRepository implements IClassCreator
11
14
{
12
15
public function __construct (
@@ -22,7 +25,10 @@ public function __construct(
22
25
public string $ repositoryName ,
23
26
public string $ interfaceName ,
24
27
public string $ repositoryNamespace ,
25
- public string $ selectedDb
28
+ public string $ selectedDb ,
29
+ public string $ redisRepositoryVariable ,
30
+ public string $ redisRepositoryName ,
31
+ public string $ strategyName
26
32
)
27
33
{
28
34
}
@@ -35,27 +41,41 @@ private function writeFunction(string $functionStub, string $functionName, strin
35
41
$ functionReturnType = 'null|{{ EntityName }} ' ;
36
42
$ functionName .= ucfirst (Str::camel ($ columnName ));
37
43
$ columnName = Str::camel ($ columnName );
44
+ $ redisCashFunction = $ this ->getRedisCashFunctionGetOneBy ($ this ->strategyName );
45
+
46
+
38
47
} elseif ($ functionName === 'getAllBy ' ) {
39
48
$ functionReturnType = 'Collection ' ;
40
49
$ functionName .= ucfirst (Str::plural (Str::camel ($ columnName )));
41
50
$ columnName = Str::plural (Str::camel ($ columnName ));
51
+ $ redisCashFunction = $ this ->getRedisCashFunctionGetAllBy ($ this ->strategyName );
52
+
53
+
42
54
} elseif ($ functionName === 'create ' ) {
43
55
$ functionReturnType = $ attributeType ;
56
+ $ redisCashFunction = $ this ->getRedisCashFunctionCreate ($ this ->strategyName );
57
+
44
58
} elseif (in_array ($ functionName , ['update ' , 'remove ' , 'restore ' ])) {
45
59
$ functionReturnType = 'int ' ;
46
- }
60
+ $ redisCashFunction = $ this -> getRedisCashFunctionUpdate ( $ this -> strategyName );
47
61
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 ],
50
65
$ functionStub );
51
66
}
52
-
53
67
private function writeSqlAttribute (string $ attributeStub , string $ sqlRepositoryVariable , string $ sqlRepositoryName ): string
54
68
{
55
- return str_replace (['{{ SqlRepositoryVariable }} ' , '{{ SqlRepositoryName }} ' ],
69
+ return str_replace (['{{ SqlRepositoryVariable }} ' , '{{ SqlRepositoryName }} ' ],
56
70
[$ sqlRepositoryVariable , $ sqlRepositoryName ],
57
71
$ attributeStub );
58
72
}
73
+ public function writeRedisAttribute (string $ attributeStub ,string $ redisRepositoryVariable ,string $ redisRepositoryName ):string
74
+ {
75
+ return str_replace (['{{ RedisRepositoryVariable }} ' , '{{ RedisRepositoryName }} ' ],
76
+ [$ redisRepositoryVariable , $ redisRepositoryName ],
77
+ $ attributeStub );
78
+ }
59
79
60
80
public function getNameSpace (): string
61
81
{
@@ -85,6 +105,7 @@ public function createAttributs(): array
85
105
$ attributeSqlStub = file_get_contents ($ this ->repositoryStubsPath . 'attribute.sql.stub ' );
86
106
$ attributes = [];
87
107
$ attributes ['repository ' ] = 'private ' .$ this ->interfaceName .' $repository; ' ;
108
+ $ attributes ['redisRepository ' ] = 'private ' .$ this ->redisRepositoryName .' $redisRepository; ' ;
88
109
return $ attributes ;
89
110
}
90
111
@@ -93,9 +114,9 @@ public function createFunctions(): array
93
114
$ constructStub = file_get_contents ($ this ->repositoryStubsPath . 'construct.stub ' );
94
115
$ functionStub = file_get_contents ($ this ->repositoryStubsPath . 'function.stub ' );
95
116
$ setterSqlStub = file_get_contents ($ this ->repositoryStubsPath . 'setter.sql.stub ' );
96
-
97
117
$ functions = [];
98
118
$ functions ['__construct ' ] = $ this ->getConstruct ($ setterSqlStub , $ constructStub );
119
+ $ functions ['__construct ' ] = $ this ->getConstructRedis ($ setterSqlStub , $ constructStub );
99
120
$ functions ['getOneById ' ] = $ this ->writeFunction ($ functionStub , 'getOneBy ' , 'id ' , 'int ' );
100
121
$ functions ['getAllByIds ' ] = $ this ->writeFunction ($ functionStub , 'getAllBy ' , 'id ' , 'array ' );
101
122
$ indexes = $ this ->extractIndexes ($ this ->tableName );
@@ -105,7 +126,6 @@ public function createFunctions(): array
105
126
$ fun_name = ucfirst (Str::camel ($ index ->COLUMN_NAME ));
106
127
$ functions ['getOneBy ' . $ fun_name ] = $ this ->writeFunction ($ functionStub , 'getOneBy ' , $ index ->COLUMN_NAME , 'int ' );
107
128
}
108
-
109
129
if ($ this ->detectForeignKeys ) {
110
130
$ foreignKeys = $ this ->extractForeignKeys ($ this ->tableName );
111
131
@@ -116,14 +136,12 @@ public function createFunctions(): array
116
136
$ functions ['getAllBy ' . $ fun_name ] = $ this ->writeFunction ($ functionStub , 'getAllBy ' , $ _foreignKey ->COLUMN_NAME , 'array ' );
117
137
}
118
138
}
119
-
120
139
$ functions ['create ' ] = $ this ->writeFunction ($ functionStub , 'create ' , $ this ->entityVariableName , $ this ->entityName );
121
140
$ functions ['update ' ] = $ this ->writeFunction ($ functionStub , 'update ' , $ this ->entityVariableName , $ this ->entityName );
122
141
if (in_array ('deleted_at ' , $ this ->columns ->pluck ('COLUMN_NAME ' )->toArray (), true )) {
123
142
$ functions ['remove ' ] = $ this ->writeFunction ($ functionStub , 'remove ' , $ this ->entityVariableName , $ this ->entityName );
124
143
$ functions ['restore ' ] = $ this ->writeFunction ($ functionStub , 'restore ' , $ this ->entityVariableName , $ this ->entityName );
125
144
}
126
-
127
145
foreach ($ functions as &$ func ) {
128
146
$ func = str_replace (["{{ SqlRepositoryVariable }} " , '{{ SqlRepositoryName }} ' , '{{ EntityName }} ' ],
129
147
[$ this ->sqlRepositoryVariable , $ this ->sqlRepositoryName , $ this ->entityName ],
@@ -132,9 +150,52 @@ public function createFunctions(): array
132
150
}
133
151
return $ functions ;
134
152
}
135
-
136
153
public function getConstruct (string $ setterSqlStub , string $ constructStub )
137
154
{
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
+ };
139
200
}
140
201
}
0 commit comments