1- <?php namespace GeneaLabs \LaravelModelCaching \ Traits ;
1+ <?php namespace GeneaLabs \LaravelModelCaching ;
22
3- use GeneaLabs \LaravelModelCaching \CachedBuilder ;
3+ use Illuminate \Database \Eloquent \Model ;
4+ use Illuminate \Database \Query \Builder ;
45use Illuminate \Support \Collection ;
56
6- trait CacheKeyable
7+ class CacheKey
78{
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 ();
1423 $ key .= $ this ->getIdColumn ($ idColumn ?: '' );
1524 $ 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 ();
2130
2231 return $ key ;
2332 }
@@ -27,32 +36,32 @@ protected function getIdColumn(string $idColumn) : string
2736 return $ idColumn ? "_ {$ idColumn }" : '' ;
2837 }
2938
30- protected function getLimitClause (CachedBuilder $ builder ) : string
39+ protected function getLimitClause () : string
3140 {
32- if (! $ builder ->query ->limit ) {
41+ if (! $ this ->query ->limit ) {
3342 return '' ;
3443 }
3544
36- return "-limit_ {$ builder ->query ->limit }" ;
45+ return "-limit_ {$ this ->query ->limit }" ;
3746 }
3847
39- protected function getModelSlug (CachedBuilder $ builder ) : string
48+ protected function getModelSlug () : string
4049 {
41- return str_slug (get_class ($ builder ->model ));
50+ return str_slug (get_class ($ this ->model ));
4251 }
4352
44- protected function getOffsetClause (CachedBuilder $ builder ) : string
53+ protected function getOffsetClause () : string
4554 {
46- if (! $ builder ->query ->offset ) {
55+ if (! $ this ->query ->offset ) {
4756 return '' ;
4857 }
4958
50- return "-offset_ {$ builder ->query ->offset }" ;
59+ return "-offset_ {$ this ->query ->offset }" ;
5160 }
5261
53- protected function getOrderByClauses (CachedBuilder $ builder ) : string
62+ protected function getOrderByClauses () : string
5463 {
55- $ orders = collect ($ builder ->query ->orders );
64+ $ orders = collect ($ this ->query ->orders );
5665
5766 return $ orders ->reduce (function ($ carry , $ order ){
5867 return $ carry . '_orderBy_ ' . $ order ['column ' ] . '_ ' . $ order ['direction ' ];
@@ -83,12 +92,12 @@ protected function getValuesClause(array $where = null) : string
8392 : '' ;
8493 }
8594
86- protected function getWhereClauses (CachedBuilder $ builder , array $ wheres = []) : string
95+ protected function getWhereClauses (array $ wheres = []) : string
8796 {
88- return $ this ->getWheres ($ builder , $ wheres )
89- ->reduce (function ($ carry , $ where ) use ( $ builder ) {
97+ return $ this ->getWheres ($ wheres )
98+ ->reduce (function ($ carry , $ where ) {
9099 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 );
92101 }
93102
94103 if ($ where ['type ' ] === 'Column ' ) {
@@ -108,20 +117,20 @@ protected function getWhereClauses(CachedBuilder $builder, array $wheres = []) :
108117 . '' ;
109118 }
110119
111- protected function getWheres (CachedBuilder $ builder , array $ wheres ) : Collection
120+ protected function getWheres (array $ wheres ) : Collection
112121 {
113122 $ wheres = collect ($ wheres );
114123
115124 if ($ wheres ->isEmpty ()) {
116- $ wheres = collect ($ builder ->query ->wheres );
125+ $ wheres = collect ($ this ->query ->wheres );
117126 }
118127
119128 return $ wheres ;
120129 }
121130
122- protected function getWithModels (CachedBuilder $ builder ) : string
131+ protected function getWithModels () : string
123132 {
124- $ eagerLoads = collect ($ builder ->eagerLoad );
133+ $ eagerLoads = collect ($ this ->eagerLoad );
125134
126135 if ($ eagerLoads ->isEmpty ()) {
127136 return '' ;
0 commit comments