1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shamaseen\Repository\Utility\Models; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Database\Connection; |
7
|
|
|
use Illuminate\Database\ConnectionInterface; |
8
|
|
|
use Illuminate\Database\Eloquent\Model as LaravelModel; |
9
|
|
|
use Illuminate\Database\Query\Builder as QueryBuilder; |
10
|
|
|
use Illuminate\Support\Str; |
11
|
|
|
use Psr\SimpleCache\InvalidArgumentException; |
12
|
|
|
use Shamaseen\Repository\Utility\Model; |
13
|
|
|
|
14
|
|
|
class ConnectionProxy implements ConnectionInterface |
15
|
|
|
{ |
16
|
|
|
public Connection $realConnection; |
17
|
|
|
|
18
|
|
|
public function __construct(Connection $realConnection, private readonly Model|LaravelModel $model) |
19
|
|
|
{ |
20
|
|
|
$this->realConnection = $realConnection; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
// Proxy methods >>> |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @throws InvalidArgumentException |
27
|
|
|
*/ |
28
|
|
|
public function cacheOrNext($fullQuery, callable $next) |
29
|
|
|
{ |
30
|
|
|
if(config('repository.disable_cache')) { |
31
|
|
|
return $next(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if ($this->model->requestCacheEnabled) { |
35
|
|
|
$cache = $this->model->requestCache; |
36
|
|
|
|
37
|
|
|
// if the key exist then return the cached version |
38
|
|
|
if ($fromCache = $cache->get($fullQuery)) { |
|
|
|
|
39
|
|
|
return $fromCache; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$result = $next(); |
43
|
|
|
|
44
|
|
|
$cache->set($fullQuery, $result); |
45
|
|
|
|
46
|
|
|
return $result; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $next(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Laravel methods >>> |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @throws InvalidArgumentException |
56
|
|
|
*/ |
57
|
|
|
public function selectOne($query, $bindings = [], $useReadPdo = true) |
58
|
|
|
{ |
59
|
|
|
$fullQuery = Str::replaceArray('?', $bindings, $query); |
60
|
|
|
|
61
|
|
|
return $this->cacheOrNext($fullQuery, function () use ($query, $bindings, $useReadPdo) { |
62
|
|
|
return $this->realConnection->selectOne($query, $bindings, $useReadPdo); |
63
|
|
|
}); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @throws InvalidArgumentException |
68
|
|
|
*/ |
69
|
|
|
public function select($query, $bindings = [], $useReadPdo = true) |
70
|
|
|
{ |
71
|
|
|
$fullQuery = Str::replaceArray('?', $bindings, $query); |
72
|
|
|
|
73
|
|
|
return $this->cacheOrNext($fullQuery, function () use ($query, $bindings, $useReadPdo) { |
|
|
|
|
74
|
|
|
return $this->realConnection->select($query, $bindings, $useReadPdo); |
75
|
|
|
}); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function table($table, $as = null) |
79
|
|
|
{ |
80
|
|
|
return $this->realConnection->table($table, $as); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function raw($value) |
84
|
|
|
{ |
85
|
|
|
return $this->realConnection->raw($value); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function cursor($query, $bindings = [], $useReadPdo = true) |
89
|
|
|
{ |
90
|
|
|
return $this->realConnection->cursor($query, $bindings, $useReadPdo); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function insert($query, $bindings = []) |
94
|
|
|
{ |
95
|
|
|
return $this->realConnection->insert($query, $bindings); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function update($query, $bindings = []) |
99
|
|
|
{ |
100
|
|
|
return $this->realConnection->update($query, $bindings); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function delete($query, $bindings = []) |
104
|
|
|
{ |
105
|
|
|
return $this->realConnection->delete($query, $bindings); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function statement($query, $bindings = []) |
109
|
|
|
{ |
110
|
|
|
return $this->realConnection->statement($query, $bindings); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function affectingStatement($query, $bindings = []) |
114
|
|
|
{ |
115
|
|
|
return $this->realConnection->affectingStatement($query, $bindings); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function unprepared($query) |
119
|
|
|
{ |
120
|
|
|
return $this->realConnection->unprepared($query); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function prepareBindings(array $bindings) |
124
|
|
|
{ |
125
|
|
|
return $this->realConnection->prepareBindings($bindings); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function transaction(Closure $callback, $attempts = 1) |
129
|
|
|
{ |
130
|
|
|
return $this->realConnection->transaction($callback, $attempts); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function beginTransaction() |
134
|
|
|
{ |
135
|
|
|
return $this->realConnection->beginTransaction(); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function commit() |
139
|
|
|
{ |
140
|
|
|
return $this->realConnection->commit(); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function rollBack() |
144
|
|
|
{ |
145
|
|
|
return $this->realConnection->rollBack(); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function transactionLevel() |
149
|
|
|
{ |
150
|
|
|
return $this->realConnection->transactionLevel(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function pretend(Closure $callback) |
154
|
|
|
{ |
155
|
|
|
return $this->realConnection->pretend($callback); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getDatabaseName() |
159
|
|
|
{ |
160
|
|
|
return $this->realConnection->getDatabaseName(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function __set(string $name, $value): void |
164
|
|
|
{ |
165
|
|
|
$this->realConnection->{$name} = $value; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function __get(string $name) |
169
|
|
|
{ |
170
|
|
|
return $this->realConnection->{$name}; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function __call(string $name, array $arguments) |
174
|
|
|
{ |
175
|
|
|
return $this->realConnection->{$name}(...$arguments); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get a new query builder instance. |
180
|
|
|
* |
181
|
|
|
* @return QueryBuilder |
182
|
|
|
*/ |
183
|
|
|
public function query() |
184
|
|
|
{ |
185
|
|
|
return new QueryBuilder( |
186
|
|
|
// pass our connection instead of laravel one. |
187
|
|
|
$this, |
188
|
|
|
$this->realConnection->getQueryGrammar(), |
189
|
|
|
$this->realConnection->getPostProcessor() |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function scalar($query, $bindings = [], $useReadPdo = true) |
194
|
|
|
{ |
195
|
|
|
return $this->scalar($query, $bindings, $useReadPdo); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.