1
- <?php namespace GeneaLabs \LaravelModelCaching \ Traits ;
1
+ <?php namespace GeneaLabs \LaravelModelCaching ;
2
2
3
- use GeneaLabs \LaravelModelCaching \CachedBuilder ;
3
+ use Illuminate \Database \Eloquent \Model ;
4
+ use Illuminate \Database \Query \Builder ;
4
5
use Illuminate \Support \Collection ;
5
6
6
- trait CacheKeyable
7
+ class CacheKey
7
8
{
8
- protected function makeCacheKey (
9
- CachedBuilder $ builder ,
10
- array $ columns = ['* ' ],
11
- $ idColumn = null
12
- ) : string {
13
- $ key = $ this ->getModelSlug ($ builder );
9
+ protected $ eagerLoad ;
10
+ protected $ model ;
11
+ protected $ query ;
12
+
13
+ public function __construct (array $ eagerLoad , Model $ model , Builder $ query )
14
+ {
15
+ $ this ->eagerLoad = $ eagerLoad ;
16
+ $ this ->model = $ model ;
17
+ $ this ->query = $ query ;
18
+ }
19
+
20
+ public function make (array $ columns = ['* ' ], $ idColumn = null ) : string
21
+ {
22
+ $ key = $ this ->getModelSlug ();
14
23
$ key .= $ this ->getIdColumn ($ idColumn ?: '' );
15
24
$ key .= $ this ->getQueryColumns ($ columns );
16
- $ key .= $ this ->getWhereClauses ($ builder );
17
- $ key .= $ this ->getWithModels ($ builder );
18
- $ key .= $ this ->getOrderByClauses ($ builder );
19
- $ key .= $ this ->getOffsetClause ($ builder );
20
- $ key .= $ this ->getLimitClause ($ builder );
25
+ $ key .= $ this ->getWhereClauses ();
26
+ $ key .= $ this ->getWithModels ();
27
+ $ key .= $ this ->getOrderByClauses ();
28
+ $ key .= $ this ->getOffsetClause ();
29
+ $ key .= $ this ->getLimitClause ();
21
30
22
31
return $ key ;
23
32
}
@@ -27,32 +36,32 @@ protected function getIdColumn(string $idColumn) : string
27
36
return $ idColumn ? "_ {$ idColumn }" : '' ;
28
37
}
29
38
30
- protected function getLimitClause (CachedBuilder $ builder ) : string
39
+ protected function getLimitClause () : string
31
40
{
32
- if (! $ builder ->query ->limit ) {
41
+ if (! $ this ->query ->limit ) {
33
42
return '' ;
34
43
}
35
44
36
- return "-limit_ {$ builder ->query ->limit }" ;
45
+ return "-limit_ {$ this ->query ->limit }" ;
37
46
}
38
47
39
- protected function getModelSlug (CachedBuilder $ builder ) : string
48
+ protected function getModelSlug () : string
40
49
{
41
- return str_slug (get_class ($ builder ->model ));
50
+ return str_slug (get_class ($ this ->model ));
42
51
}
43
52
44
- protected function getOffsetClause (CachedBuilder $ builder ) : string
53
+ protected function getOffsetClause () : string
45
54
{
46
- if (! $ builder ->query ->offset ) {
55
+ if (! $ this ->query ->offset ) {
47
56
return '' ;
48
57
}
49
58
50
- return "-offset_ {$ builder ->query ->offset }" ;
59
+ return "-offset_ {$ this ->query ->offset }" ;
51
60
}
52
61
53
- protected function getOrderByClauses (CachedBuilder $ builder ) : string
62
+ protected function getOrderByClauses () : string
54
63
{
55
- $ orders = collect ($ builder ->query ->orders );
64
+ $ orders = collect ($ this ->query ->orders );
56
65
57
66
return $ orders ->reduce (function ($ carry , $ order ){
58
67
return $ carry . '_orderBy_ ' . $ order ['column ' ] . '_ ' . $ order ['direction ' ];
@@ -83,12 +92,12 @@ protected function getValuesClause(array $where = null) : string
83
92
: '' ;
84
93
}
85
94
86
- protected function getWhereClauses (CachedBuilder $ builder , array $ wheres = []) : string
95
+ protected function getWhereClauses (array $ wheres = []) : string
87
96
{
88
- return $ this ->getWheres ($ builder , $ wheres )
89
- ->reduce (function ($ carry , $ where ) use ( $ builder ) {
97
+ return $ this ->getWheres ($ wheres )
98
+ ->reduce (function ($ carry , $ where ) {
90
99
if (in_array ($ where ['type ' ], ['Exists ' , 'Nested ' , 'NotExists ' ])) {
91
- return '_ ' . strtolower ($ where ['type ' ]) . $ this ->getWhereClauses ($ builder , $ where ['query ' ]->wheres );
100
+ return '_ ' . strtolower ($ where ['type ' ]) . $ this ->getWhereClauses ($ where ['query ' ]->wheres );
92
101
}
93
102
94
103
if ($ where ['type ' ] === 'Column ' ) {
@@ -108,20 +117,20 @@ protected function getWhereClauses(CachedBuilder $builder, array $wheres = []) :
108
117
. '' ;
109
118
}
110
119
111
- protected function getWheres (CachedBuilder $ builder , array $ wheres ) : Collection
120
+ protected function getWheres (array $ wheres ) : Collection
112
121
{
113
122
$ wheres = collect ($ wheres );
114
123
115
124
if ($ wheres ->isEmpty ()) {
116
- $ wheres = collect ($ builder ->query ->wheres );
125
+ $ wheres = collect ($ this ->query ->wheres );
117
126
}
118
127
119
128
return $ wheres ;
120
129
}
121
130
122
- protected function getWithModels (CachedBuilder $ builder ) : string
131
+ protected function getWithModels () : string
123
132
{
124
- $ eagerLoads = collect ($ builder ->eagerLoad );
133
+ $ eagerLoads = collect ($ this ->eagerLoad );
125
134
126
135
if ($ eagerLoads ->isEmpty ()) {
127
136
return '' ;
0 commit comments