Skip to content

Commit 7213a54

Browse files
authored
Merge pull request #28 from GeneaLabs/laravel-5.5
Laravel 5.5
2 parents 1f821b6 + ee0b047 commit 7213a54

File tree

6 files changed

+263
-197
lines changed

6 files changed

+263
-197
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Please observe and respect all aspects of the included Code of Conduct <https://
7474

7575
### Reporting Issues
7676
When reporting issues, please fill out the included template as completely as
77-
possible. Incomplete issues may be ignore or closed if there is not enough
77+
possible. Incomplete issues may be ignored or closed if there is not enough
7878
information included to be actionable.
7979

8080
### Submitting Pull Requests

Diff for: src/CacheKey.php

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
}

Diff for: src/CacheTags.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching;
2+
3+
use GeneaLabs\LaravelModelCaching\CachedBuilder;
4+
use Illuminate\Database\Eloquent\Model;
5+
use Illuminate\Database\Eloquent\Relations\Relation;
6+
7+
class CacheTags
8+
{
9+
protected $eagerLoad;
10+
protected $model;
11+
12+
public function __construct(array $eagerLoad, Model $model)
13+
{
14+
$this->eagerLoad = $eagerLoad;
15+
$this->model = $model;
16+
}
17+
18+
public function make() : array
19+
{
20+
return collect($this->eagerLoad)
21+
->keys()
22+
->map(function ($relationName) {
23+
$relation = collect(explode('.', $relationName))
24+
->reduce(function ($carry, $name) {
25+
if (! $carry) {
26+
$carry = $this->model;
27+
}
28+
29+
if ($carry instanceof Relation) {
30+
$carry = $carry->getQuery()->getModel();
31+
}
32+
33+
return $carry->{$name}();
34+
});
35+
36+
return str_slug(get_class($relation->getQuery()->getModel()));
37+
})
38+
->prepend(str_slug(get_class($this->model)))
39+
->values()
40+
->toArray();
41+
}
42+
}

0 commit comments

Comments
 (0)