1
|
|
|
<?php namespace GeneaLabs\LaravelModelCaching; |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Eloquent\Model; |
4
|
|
|
use Illuminate\Database\Query\Builder; |
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
|
7
|
|
|
class CacheKey |
8
|
|
|
{ |
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(); |
23
|
|
|
$key .= $this->getIdColumn($idColumn ?: ''); |
24
|
|
|
$key .= $this->getQueryColumns($columns); |
25
|
|
|
$key .= $this->getWhereClauses(); |
26
|
|
|
$key .= $this->getWithModels(); |
27
|
|
|
$key .= $this->getOrderByClauses(); |
28
|
|
|
$key .= $this->getOffsetClause(); |
29
|
|
|
$key .= $this->getLimitClause(); |
30
|
|
|
|
31
|
|
|
return $key; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function getIdColumn(string $idColumn) : string |
35
|
|
|
{ |
36
|
|
|
return $idColumn ? "_{$idColumn}" : ''; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function getLimitClause() : string |
40
|
|
|
{ |
41
|
|
|
if (! $this->query->limit) { |
42
|
|
|
return ''; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return "-limit_{$this->query->limit}"; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function getModelSlug() : string |
49
|
|
|
{ |
50
|
|
|
return str_slug(get_class($this->model)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function getOffsetClause() : string |
54
|
|
|
{ |
55
|
|
|
if (! $this->query->offset) { |
56
|
|
|
return ''; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return "-offset_{$this->query->offset}"; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function getOrderByClauses() : string |
63
|
|
|
{ |
64
|
|
|
$orders = collect($this->query->orders); |
65
|
|
|
|
66
|
|
|
return $orders->reduce(function($carry, $order){ |
67
|
|
|
return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction']; |
68
|
|
|
}) |
69
|
|
|
?: ''; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function getQueryColumns(array $columns) : string |
73
|
|
|
{ |
74
|
|
|
if ($columns === ['*'] || $columns === []) { |
75
|
|
|
return ''; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return '_' . implode('_', $columns); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function getTypeClause($where) : string |
82
|
|
|
{ |
83
|
|
|
return in_array($where['type'], ['In', 'Null', 'NotNull']) |
84
|
|
|
? strtolower($where['type']) |
85
|
|
|
: ''; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function getValuesClause(array $where = null) : string |
89
|
|
|
{ |
90
|
|
|
return is_array(array_get($where, 'values')) |
91
|
|
|
? '_' . implode('_', $where['values']) |
92
|
|
|
: ''; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function getWhereClauses(array $wheres = []) : string |
96
|
|
|
{ |
97
|
|
|
return $this->getWheres($wheres) |
98
|
|
|
->reduce(function ($carry, $where) { |
99
|
|
|
if (in_array($where['type'], ['Exists', 'Nested', 'NotExists'])) { |
100
|
|
|
return '_' . strtolower($where['type']) . $this->getWhereClauses($where['query']->wheres); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($where['type'] === 'Column') { |
104
|
|
|
return "_{$where['boolean']}_{$where['first']}_{$where['operator']}_{$where['second']}"; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($where['type'] === 'raw') { |
108
|
|
|
return "_{$where['boolean']}_" . str_slug($where['sql']); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$value = array_get($where, 'value'); |
112
|
|
|
$value .= $this->getTypeClause($where); |
113
|
|
|
$value .= $this->getValuesClause($where); |
114
|
|
|
|
115
|
|
|
return "{$carry}-{$where['column']}_{$value}"; |
116
|
|
|
}) |
117
|
|
|
. ''; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function getWheres(array $wheres) : Collection |
121
|
|
|
{ |
122
|
|
|
$wheres = collect($wheres); |
123
|
|
|
|
124
|
|
|
if ($wheres->isEmpty()) { |
125
|
|
|
$wheres = collect($this->query->wheres); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $wheres; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function getWithModels() : string |
132
|
|
|
{ |
133
|
|
|
$eagerLoads = collect($this->eagerLoad); |
134
|
|
|
|
135
|
|
|
if ($eagerLoads->isEmpty()) { |
136
|
|
|
return ''; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return '-' . implode('-', $eagerLoads->keys()->toArray()); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|