This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Sprocketbox\Eloquent\Identity\Query; |
||
4 | |||
5 | use Closure; |
||
6 | use Illuminate\Contracts\Support\Arrayable; |
||
7 | use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
||
8 | use Illuminate\Database\Eloquent\Collection; |
||
9 | use Illuminate\Database\Eloquent\Model; |
||
10 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
||
11 | use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
||
12 | use Illuminate\Support\Str; |
||
13 | use Sprocketbox\Eloquent\Identity\Concerns\MapsIdentity; |
||
14 | use Sprocketbox\Eloquent\Identity\Facades\Identity; |
||
15 | use Sprocketbox\Eloquent\Identity\ModelIdentity; |
||
16 | |||
17 | class Builder extends EloquentBuilder |
||
18 | { |
||
19 | /** |
||
20 | * @var bool |
||
21 | */ |
||
22 | protected bool $identityIsMapped = false; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
23 | |||
24 | protected bool $refreshIdentityMap = false; |
||
25 | |||
26 | protected array $noConstraintEagerLoads = []; |
||
27 | |||
28 | public function useIdentityMap(): self |
||
29 | { |
||
30 | $this->refreshIdentityMap = false; |
||
31 | |||
32 | return $this; |
||
33 | } |
||
34 | |||
35 | public function refreshIdentityMap(): self |
||
36 | { |
||
37 | $this->refreshIdentityMap = true; |
||
38 | |||
39 | return $this; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Find an instance of the model from the identity map or database. |
||
44 | * |
||
45 | * @param $id |
||
46 | * |
||
47 | * @param array $columns |
||
48 | * |
||
49 | * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|null |
||
50 | */ |
||
51 | public function findOrIdentify($id, array $columns = ['*']) |
||
52 | { |
||
53 | if (is_array($id) || $id instanceof Arrayable) { |
||
54 | return $this->findMany($id, $columns); |
||
55 | } |
||
56 | |||
57 | $model = $this->identifyModel($id); |
||
58 | |||
59 | if ($model !== null) { |
||
60 | return $model; |
||
61 | } |
||
62 | |||
63 | return $this->whereKey($id)->first($columns); |
||
64 | } |
||
65 | |||
66 | public function find($id, $columns = ['*']) |
||
67 | { |
||
68 | if ($this->shouldUseIdentityMap()) { |
||
69 | return $this->findOrIdentify($id, $columns); |
||
70 | } |
||
71 | |||
72 | return parent::find($id, $columns); |
||
73 | } |
||
74 | |||
75 | public function findMany($ids, $columns = ['*']): Collection |
||
76 | { |
||
77 | $ids = $ids instanceof Arrayable ? $ids->toArray() : $ids; |
||
78 | |||
79 | if (empty($ids)) { |
||
80 | return $this->model->newCollection(); |
||
81 | } |
||
82 | |||
83 | if ($this->shouldUseIdentityMap()) { |
||
84 | $models = []; |
||
85 | $newIds = []; |
||
86 | |||
87 | foreach ($ids as $id) { |
||
88 | $models[$id] = $this->identifyModel($id); |
||
89 | |||
90 | if ($models[$id] === null) { |
||
91 | $newIds[] = $id; |
||
92 | } |
||
93 | } |
||
94 | |||
95 | $newModels = $this->whereKey($newIds)->get($columns); |
||
96 | $newModels->each(fn(Model $model) => $models[$model->getKey()] = $model); |
||
97 | |||
98 | return $this->model->newCollection(array_values($models)); |
||
99 | } |
||
100 | |||
101 | return parent::findMany($ids, $columns); |
||
102 | } |
||
103 | |||
104 | public function setModel(Model $model): self |
||
105 | { |
||
106 | if (in_array(MapsIdentity::class, class_uses($model), true)) { |
||
107 | $this->identityIsMapped = true; |
||
108 | } |
||
109 | |||
110 | return parent::setModel($model); |
||
111 | } |
||
112 | |||
113 | protected function shouldUseIdentityMap(): bool |
||
114 | { |
||
115 | return ! $this->refreshIdentityMap |
||
116 | && empty($this->query->bindings['where']) |
||
117 | && empty($this->query->bindings['join'] ?? []) |
||
118 | && empty($this->query->bindings['having'] ?? []); |
||
119 | } |
||
120 | |||
121 | protected function identifyModel($id): ?Model |
||
122 | { |
||
123 | if (! $this->identityIsMapped || ! $this->shouldUseIdentityMap()) { |
||
124 | return null; |
||
125 | } |
||
126 | |||
127 | if ($id instanceof ModelIdentity) { |
||
128 | $identity = $id; |
||
129 | } else { |
||
130 | $identity = $this->model->getModelIdentity($id, $this->getConnection()->getName()); |
||
131 | } |
||
132 | |||
133 | if (Identity::hasIdentity($identity)) { |
||
134 | return Identity::getIdentity($identity); |
||
135 | } |
||
136 | |||
137 | return null; |
||
138 | } |
||
139 | |||
140 | protected function eagerLoadRelation(array $models, $name, Closure $constraints) |
||
141 | { |
||
142 | $relation = $this->getRelation($name); |
||
143 | $loadedModels = []; |
||
144 | $newModels = $models; |
||
145 | |||
146 | if ($this->eagerLoadHasNoConstraints($name) && $this->shouldUseIdentityMap()) { |
||
147 | /** |
||
148 | * This is intentionally empty so that the relation doesn't get caught in the |
||
149 | * belongs to block below it. |
||
150 | * |
||
151 | * @noinspection PhpStatementHasEmptyBodyInspection |
||
152 | * @noinspection MissingOrEmptyGroupStatementInspection |
||
153 | */ |
||
154 | if ($relation instanceof BelongsToMany) { |
||
155 | // |
||
156 | } else if ($relation instanceof BelongsTo) { |
||
157 | $loadedModels = $this->eagerLoadBelongsToIdentities($relation, $newModels); |
||
158 | } |
||
159 | } |
||
160 | |||
161 | if (empty($newModels)) { |
||
162 | $eagerModels = $relation->getRelated()->newCollection($loadedModels); |
||
163 | } else { |
||
164 | $relation->addEagerConstraints($newModels); |
||
165 | $constraints($relation); |
||
166 | $eagerModels = $relation->getEager()->merge($loadedModels); |
||
167 | } |
||
168 | |||
169 | // Once we have the results, we just match those back up to their parent models |
||
170 | // using the relationship instance. Then we just return the finished arrays |
||
171 | // of models which have been eagerly hydrated and are readied for return. |
||
172 | return $relation->match( |
||
173 | $relation->initRelation($models, $name), |
||
174 | $eagerModels, $name |
||
175 | ); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Parse a list of relations into individuals. |
||
180 | * |
||
181 | * @param array $relations |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | protected function parseWithRelations(array $relations): array |
||
186 | { |
||
187 | $results = []; |
||
188 | |||
189 | foreach ($relations as $name => $constraints) { |
||
190 | // If the "name" value is a numeric key, we can assume that no constraints |
||
191 | // have been specified. We will just put an empty Closure there so that |
||
192 | // we can treat these all the same while we are looping through them. |
||
193 | if (is_numeric($name)) { |
||
194 | $name = $constraints; |
||
195 | |||
196 | if (Str::contains($name, ':')) { |
||
197 | [$name, $constraints] = $this->createSelectWithConstraint($name); |
||
198 | } else { |
||
199 | $this->noConstraintEagerLoads[] = $name; |
||
200 | $constraints = static function () { |
||
201 | // |
||
202 | }; |
||
203 | } |
||
204 | } |
||
205 | |||
206 | // We need to separate out any nested includes, which allows the developers |
||
207 | // to load deep relationships using "dots" without stating each level of |
||
208 | // the relationship with its own key in the array of eager-load names. |
||
209 | $results = $this->addNestedWiths($name, $results); |
||
210 | |||
211 | $results[$name] = $constraints; |
||
212 | } |
||
213 | |||
214 | return $results; |
||
215 | } |
||
216 | |||
217 | protected function eagerLoadHasNoConstraints(string $name): bool |
||
218 | { |
||
219 | return in_array($name, $this->noConstraintEagerLoads, true); |
||
220 | } |
||
221 | |||
222 | protected function eagerLoadBelongsToIdentities(BelongsTo $relation, array &$models): array |
||
223 | { |
||
224 | $newModels = []; |
||
225 | $loadedModels = []; |
||
226 | |||
227 | foreach ($models as $i => $model) { |
||
228 | $key = $model->getAttribute($relation->getForeignKeyName()); |
||
229 | |||
230 | if ($key !== null) { |
||
231 | if (method_exists($relation->getRelated(), 'getModelIdentity')) { |
||
232 | $loadedModel = $this->identifyModel( |
||
233 | $relation->getRelated()->getModelIdentity($key, $this->getConnection()->getName()) |
||
234 | ); |
||
235 | |||
236 | if ($loadedModel !== null) { |
||
237 | $loadedModels[] = $loadedModel; |
||
238 | continue; |
||
239 | } |
||
240 | } |
||
241 | |||
242 | $newModels[] = $key; |
||
243 | } |
||
244 | } |
||
245 | |||
246 | $models = $newModels; |
||
247 | |||
248 | return array_unique($loadedModels); |
||
249 | } |
||
250 | } |