Skip to content

Commit 1f86320

Browse files
authored
Fixed ordering of methods and update binding resolution.
1 parent 49d849d commit 1f86320

File tree

1 file changed

+127
-129
lines changed

1 file changed

+127
-129
lines changed

src/CacheKey.php

Lines changed: 127 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,66 @@ public function make(
5151
return $key;
5252
}
5353

54+
protected function getBindingsSlug() : string
55+
{
56+
if (! method_exists($this->model, 'query')) {
57+
return '';
58+
}
59+
60+
return Arr::query($this->model->query()->getBindings());
61+
}
62+
63+
protected function getColumnClauses(array $where) : string
64+
{
65+
if ($where["type"] !== "Column") {
66+
return "";
67+
}
68+
69+
return "-{$where["boolean"]}_{$where["first"]}_{$where["operator"]}_{$where["second"]}";
70+
}
71+
72+
protected function getCurrentBinding(string $type, string $bindingFallback): string
73+
{
74+
return data_get($this->query->bindings, "{$type}.{$this->currentBinding}", $bindingFallback);
75+
}
76+
5477
protected function getIdColumn(string $idColumn) : string
5578
{
5679
return $idColumn ? "_{$idColumn}" : "";
5780
}
5881

82+
protected function getInAndNotInClauses(array $where) : string
83+
{
84+
if (! in_array($where["type"], ["In", "NotIn", "InRaw"])) {
85+
return "";
86+
}
87+
88+
$type = strtolower($where["type"]);
89+
$subquery = $this->getValuesFromWhere($where);
90+
$values = collect($this->getCurrentBinding('where') ?? []);
91+
92+
if (Str::startsWith($subquery, $values->first())) {
93+
$this->currentBinding += count($where["values"]);
94+
}
95+
96+
if (! is_numeric($subquery) && ! is_numeric(str_replace("_", "", $subquery))) {
97+
try {
98+
$subquery = Uuid::fromBytes($subquery);
99+
$values = $this->recursiveImplode([$subquery], "_");
100+
101+
return "-{$where["column"]}_{$type}{$values}";
102+
} catch (Exception $exception) {
103+
// do nothing
104+
}
105+
}
106+
107+
$subquery = preg_replace('/\?(?=(?:[^"]*"[^"]*")*[^"]*\Z)/m', "_??_", $subquery);
108+
$subquery = collect(vsprintf(str_replace("_??_", "%s", $subquery), $values->toArray()));
109+
$values = $this->recursiveImplode($subquery->toArray(), "_");
110+
111+
return "-{$where["column"]}_{$type}{$values}";
112+
}
113+
59114
protected function getLimitClause() : string
60115
{
61116
if (! property_exists($this->query, "limit")
@@ -67,15 +122,18 @@ protected function getLimitClause() : string
67122
return "-limit_{$this->query->limit}";
68123
}
69124

70-
protected function getTableSlug() : string
125+
protected function getModelSlug() : string
71126
{
72-
return (new Str)->slug($this->query->from)
73-
. ":";
127+
return (new Str)->slug(get_class($this->model));
74128
}
75129

76-
protected function getModelSlug() : string
130+
protected function getNestedClauses(array $where) : string
77131
{
78-
return (new Str)->slug(get_class($this->model));
132+
if (! in_array($where["type"], ["Exists", "Nested", "NotExists"])) {
133+
return "";
134+
}
135+
136+
return "-" . strtolower($where["type"]) . $this->getWhereClauses($where["query"]->wheres);
79137
}
80138

81139
protected function getOffsetClause() : string
@@ -110,6 +168,18 @@ protected function getOrderByClauses() : string
110168
?: "";
111169
}
112170

171+
protected function getOtherClauses(array $where) : string
172+
{
173+
if (in_array($where["type"], ["Exists", "Nested", "NotExists", "Column", "raw", "In", "NotIn", "InRaw"])) {
174+
return "";
175+
}
176+
177+
$value = $this->getTypeClause($where);
178+
$value .= $this->getValuesClause($where);
179+
180+
return "-{$where["column"]}_{$value}";
181+
}
182+
113183
protected function getQueryColumns(array $columns) : string
114184
{
115185
if (($columns === ["*"]
@@ -129,6 +199,36 @@ protected function getQueryColumns(array $columns) : string
129199
return "_" . implode("_", $columns);
130200
}
131201

202+
protected function getRawClauses(array $where) : string
203+
{
204+
if (! in_array($where["type"], ["raw"])) {
205+
return "";
206+
}
207+
208+
$queryParts = explode("?", $where["sql"]);
209+
$clause = "_{$where["boolean"]}";
210+
211+
while (count($queryParts) > 1) {
212+
$clause .= "_" . array_shift($queryParts);
213+
$clause .= $this->getCurrentBinding('where');
214+
$this->currentBinding++;
215+
}
216+
217+
$lastPart = array_shift($queryParts);
218+
219+
if ($lastPart) {
220+
$clause .= "_" . $lastPart;
221+
}
222+
223+
return "-" . str_replace(" ", "_", $clause);
224+
}
225+
226+
protected function getTableSlug() : string
227+
{
228+
return (new Str)->slug($this->query->from)
229+
. ":";
230+
}
231+
132232
protected function getTypeClause($where) : string
133233
{
134234
$type = in_array($where["type"], ["InRaw", "In", "NotIn", "Null", "NotNull", "between", "NotInSub", "InSub", "JsonContains"])
@@ -174,9 +274,8 @@ protected function getValuesFromWhere(array $where) : string
174274

175275
protected function getValuesFromBindings(array $where, string $values) : string
176276
{
177-
// Fallback to this when the current binding does not exist in the bindings array
178277
$bindingFallback = __CLASS__ . ':UNKNOWN_BINDING';
179-
$currentBinding = $this->getCurrentBinding('where') ?? $bindingFallback;
278+
$currentBinding = $this->getCurrentBinding('where', $bindingFallback);
180279

181280
if ($currentBinding !== $bindingFallback) {
182281
$values = $currentBinding;
@@ -211,57 +310,41 @@ protected function getWhereClauses(array $wheres = []) : string
211310
return $value;
212311
});
213312
}
214-
215-
protected function getNestedClauses(array $where) : string
313+
314+
protected function getWheres(array $wheres) : Collection
216315
{
217-
if (! in_array($where["type"], ["Exists", "Nested", "NotExists"])) {
218-
return "";
219-
}
220-
221-
return "-" . strtolower($where["type"]) . $this->getWhereClauses($where["query"]->wheres);
222-
}
316+
$wheres = collect($wheres);
223317

224-
protected function getColumnClauses(array $where) : string
225-
{
226-
if ($where["type"] !== "Column") {
227-
return "";
318+
if ($wheres->isEmpty()
319+
&& property_exists($this->query, "wheres")
320+
) {
321+
$wheres = collect($this->query->wheres);
228322
}
229323

230-
return "-{$where["boolean"]}_{$where["first"]}_{$where["operator"]}_{$where["second"]}";
324+
return $wheres;
231325
}
232326

233-
protected function getInAndNotInClauses(array $where) : string
327+
protected function getWithModels() : string
234328
{
235-
if (! in_array($where["type"], ["In", "NotIn", "InRaw"])) {
236-
return "";
237-
}
238-
239-
$type = strtolower($where["type"]);
240-
$subquery = $this->getValuesFromWhere($where);
241-
$values = collect($this->getCurrentBinding('where') ?? []);
329+
$eagerLoads = collect($this->eagerLoad);
242330

243-
if (Str::startsWith($subquery, $values->first())) {
244-
$this->currentBinding += count($where["values"]);
331+
if ($eagerLoads->isEmpty()) {
332+
return "";
245333
}
246334

247-
if (! is_numeric($subquery) && ! is_numeric(str_replace("_", "", $subquery))) {
248-
try {
249-
$subquery = Uuid::fromBytes($subquery);
250-
$values = $this->recursiveImplode([$subquery], "_");
251-
252-
return "-{$where["column"]}_{$type}{$values}";
253-
} catch (Exception $exception) {
254-
// do nothing
335+
return $eagerLoads->keys()->reduce(function ($carry, $related) {
336+
if (! method_exists($this->model, $related)) {
337+
return "{$carry}-{$related}";
255338
}
256-
}
257339

258-
$subquery = preg_replace('/\?(?=(?:[^"]*"[^"]*")*[^"]*\Z)/m', "_??_", $subquery);
259-
$subquery = collect(vsprintf(str_replace("_??_", "%s", $subquery), $values->toArray()));
260-
$values = $this->recursiveImplode($subquery->toArray(), "_");
340+
$relatedModel = $this->model->$related()->getRelated();
341+
$relatedConnection = $relatedModel->getConnection()->getName();
342+
$relatedDatabase = $relatedModel->getConnection()->getDatabaseName();
261343

262-
return "-{$where["column"]}_{$type}{$values}";
344+
return "{$carry}-{$relatedConnection}:{$relatedDatabase}:{$related}";
345+
});
263346
}
264-
347+
265348
protected function recursiveImplode(array $items, string $glue = ",") : string
266349
{
267350
$result = "";
@@ -287,89 +370,4 @@ protected function recursiveImplode(array $items, string $glue = ",") : string
287370

288371
return $result;
289372
}
290-
291-
protected function getRawClauses(array $where) : string
292-
{
293-
if (! in_array($where["type"], ["raw"])) {
294-
return "";
295-
}
296-
297-
$queryParts = explode("?", $where["sql"]);
298-
$clause = "_{$where["boolean"]}";
299-
300-
while (count($queryParts) > 1) {
301-
$clause .= "_" . array_shift($queryParts);
302-
$clause .= $this->getCurrentBinding('where');
303-
$this->currentBinding++;
304-
}
305-
306-
$lastPart = array_shift($queryParts);
307-
308-
if ($lastPart) {
309-
$clause .= "_" . $lastPart;
310-
}
311-
312-
return "-" . str_replace(" ", "_", $clause);
313-
}
314-
315-
protected function getOtherClauses(array $where) : string
316-
{
317-
if (in_array($where["type"], ["Exists", "Nested", "NotExists", "Column", "raw", "In", "NotIn", "InRaw"])) {
318-
return "";
319-
}
320-
321-
$value = $this->getTypeClause($where);
322-
$value .= $this->getValuesClause($where);
323-
324-
return "-{$where["column"]}_{$value}";
325-
}
326-
327-
protected function getWheres(array $wheres) : Collection
328-
{
329-
$wheres = collect($wheres);
330-
331-
if ($wheres->isEmpty()
332-
&& property_exists($this->query, "wheres")
333-
) {
334-
$wheres = collect($this->query->wheres);
335-
}
336-
337-
return $wheres;
338-
}
339-
340-
protected function getWithModels() : string
341-
{
342-
$eagerLoads = collect($this->eagerLoad);
343-
344-
if ($eagerLoads->isEmpty()) {
345-
return "";
346-
}
347-
348-
return $eagerLoads->keys()->reduce(function ($carry, $related) {
349-
if (! method_exists($this->model, $related)) {
350-
return "{$carry}-{$related}";
351-
}
352-
353-
$relatedModel = $this->model->$related()->getRelated();
354-
$relatedConnection = $relatedModel->getConnection()->getName();
355-
$relatedDatabase = $relatedModel->getConnection()->getDatabaseName();
356-
357-
return "{$carry}-{$relatedConnection}:{$relatedDatabase}:{$related}";
358-
});
359-
}
360-
361-
protected function getBindingsSlug() : string
362-
{
363-
if (! method_exists($this->model, 'query')) {
364-
return '';
365-
}
366-
367-
return Arr::query($this->model->query()->getBindings());
368-
}
369-
370-
371-
private function getCurrentBinding(string $type)
372-
{
373-
return data_get($this->query->bindings, "$type.$this->currentBinding");
374-
}
375373
}

0 commit comments

Comments
 (0)