-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryRepositoryExtension.php
More file actions
589 lines (507 loc) · 18.1 KB
/
Copy pathQueryRepositoryExtension.php
File metadata and controls
589 lines (507 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<?php
namespace Bdf\Prime\Query;
use BadMethodCallException;
use Bdf\Prime\Collection\Indexer\EntityIndexer;
use Bdf\Prime\Connection\ConnectionInterface;
use Bdf\Prime\Connection\Result\ResultSetInterface;
use Bdf\Prime\Events;
use Bdf\Prime\Exception\EntityNotFoundException;
use Bdf\Prime\Exception\PrimeException;
use Bdf\Prime\Exception\QueryBuildingException;
use Bdf\Prime\Mapper\Mapper;
use Bdf\Prime\Mapper\Metadata;
use Bdf\Prime\Query\Closure\ClosureCompiler;
use Bdf\Prime\Query\Contract\Query\KeyValueQueryInterface;
use Bdf\Prime\Query\Contract\Whereable;
use Bdf\Prime\Relations\Relation;
use Bdf\Prime\Repository\EntityRepository;
use Bdf\Prime\Repository\RepositoryInterface;
use Closure;
use Doctrine\DBAL\Query\Expression\CompositeExpression;
use function array_diff;
use function array_keys;
use function count;
use function is_array;
/**
* QueryRepositoryExtension
*
* @template E as object
*/
class QueryRepositoryExtension extends QueryCompatExtension
{
/**
* @var RepositoryInterface<E>
*/
protected $repository;
/**
* @var Metadata
*/
protected $metadata;
/**
* @var Mapper<E>
*/
protected $mapper;
/**
* @var ClosureCompiler<E>|null
*/
protected $closureCompiler;
/**
* Array of relations to associate on entities
* Contains relations and subrelations. Can be load
* only on select queries
*
* @var array
*/
protected $withRelations = [];
/**
* Array of relations to discard
*
* @var array
*/
protected $withoutRelations = [];
/**
* Collect entities by attribute
*
* @var array
*/
protected $byOptions;
/**
* QueryRepositoryExtension constructor.
*
* @param RepositoryInterface<E> $repository
* @param ClosureCompiler<E>|null $closureCompiler
*/
public function __construct(RepositoryInterface $repository, ?ClosureCompiler $closureCompiler = null)
{
$this->repository = $repository;
$this->metadata = $repository->metadata();
$this->mapper = $repository->mapper();
$this->closureCompiler = $closureCompiler;
}
/**
* Gets associated repository
*
* @param ReadCommandInterface<ConnectionInterface, E> $query
* @param null|string $name
*
* @return RepositoryInterface|null
*/
public function repository(ReadCommandInterface $query, $name = null)
{
if ($name === null) {
return $this->repository;
}
return $this->repository->repository($name);
}
/**
* Get one entity by identifier
*
* @param ReadCommandInterface<ConnectionInterface, E>&Whereable $query
* @param mixed $id
* @param null|string|array $attributes
*
* @return E|null
* @deprecated Since 2.1. Use {@see findById()} instead.
*/
public function get(ReadCommandInterface $query, $id, $attributes = null)
{
@trigger_error('Query::get()/getOrFail()/getOrNew() is deprecated since 2.1. Use findById() instead.', E_USER_DEPRECATED);
if (empty($id)) {
return null;
}
if (!is_array($id)) {
list($identifierName) = $this->metadata->primary['attributes'];
$id = [$identifierName => $id];
} else {
foreach ($id as $key => $value) {
if (is_int($key)) {
throw new \InvalidArgumentException('Raw SQL expressions are not allowed in get() method');
}
}
}
return $query->where($id)->first($attributes);
}
/**
* Get one entity or throws entity not found
*
* @param ReadCommandInterface<ConnectionInterface, E>&Whereable $query
* @param mixed $id
* @param null|string|array $attributes
*
* @return E
*
* @throws EntityNotFoundException If entity is not found
* @deprecated Since 2.1. Use {@see findByIdOrFail()} instead.
*/
public function getOrFail(ReadCommandInterface $query, $id, $attributes = null)
{
$entity = $this->get($query, $id, $attributes);
if ($entity !== null) {
return $entity;
}
throw new EntityNotFoundException('Cannot resolve entity identifier "'.implode('", "', (array)$id).'"');
}
/**
* Get one entity or return a new one if not found in repository
*
* @param ReadCommandInterface<ConnectionInterface, E>&Whereable $query
* @param mixed $id
* @param null|string|array $attributes
*
* @return E
* @deprecated Since 2.1. Use {@see findByIdOrNew()} instead.
*/
public function getOrNew(ReadCommandInterface $query, $id, $attributes = null)
{
$entity = $this->get($query, $id, $attributes);
if ($entity !== null) {
return $entity;
}
return $this->repository->entity();
}
/**
* Find entity by its primary key
* In case of composite primary key, the primary key can be resolved by previous where() call
*
* Note: If criterion which is not part of the primary key is passed, or if the primary key is not complete, the query will throw an {@see QueryBuildingException}.
*
* <code>
* $queries->findById(2);
* $queries->findById(['key1' => 1, 'key2' => 5]);
* $queries->where('key1', 1)->findById(5); // Same as above: the composite key is completed by previous where() call
* </code>
*
* @param ReadCommandInterface<ConnectionInterface, E>&Whereable $query
* @param mixed|array<string, mixed> $id The entity PK. Use an array for composite PK
*
* @return E|null The entity or null if not found
* @throws PrimeException When query fail
*/
public function findById(ReadCommandInterface $query, $id)
{
$pkAttributes = $this->metadata->primary['attributes'];
$criteria = null;
// Scalar id is used, so resolve the primary key attribute name
if (!is_array($id)) {
if (count($pkAttributes) === 1) {
// Single primary key
$id = [$pkAttributes[0] => $id];
} else {
// Composite primary key : resolve missing primary key attributes
$criteria = $this->toCriteria($query) ?? [];
foreach ($pkAttributes as $key) {
if (!isset($criteria[$key])) {
$id = [$key => $id];
break;
}
}
if (!is_array($id)) {
throw new QueryBuildingException('Ambiguous findById() call : All primary key attributes are already defined on query, so missing part of the primary key cannot be resolved. Use an array as parameter instead to explicitly define the primary key attribute name.');
}
}
}
$keys = array_keys($id);
// Some criteria are not part of the primary key
if ($extraKeys = array_diff($keys, $pkAttributes)) {
throw new QueryBuildingException('Only primary keys must be passed to findById(). Unexpected keys : ' . implode(', ', $extraKeys));
}
$missingPk = array_diff($pkAttributes, $keys);
if ($missingPk) {
// Some primary key attributes are missing
// so check if they are defined in the query on previous where() call
$criteria ??= $this->toCriteria($query) ?? [];
foreach ($missingPk as $i => $key) {
if (isset($criteria[$key])) {
unset($missingPk[$i]);
}
}
if ($missingPk) {
throw new QueryBuildingException('Only primary keys must be passed to findById(). Missing keys : ' . implode(', ', $missingPk));
}
}
return $query->where($id)->first();
}
/**
* Find entity by its primary key, or throws exception if not found in repository
* In case of composite primary key, the primary key can be resolved by previous where() call
*
* Note: If criterion which is not part of the primary key is passed, or if the primary key is not complete, the query will throw an {@see QueryBuildingException}.
*
* @param ReadCommandInterface<ConnectionInterface, E>&Whereable $query
* @param mixed|array<string, mixed> $id The entity PK. Use an array for composite PK
*
* @return E
*
* @throws EntityNotFoundException If entity is not found
* @throws PrimeException When query fail
*/
public function findByIdOrFail(ReadCommandInterface $query, $id)
{
$entity = $this->findById($query, $id);
if ($entity !== null) {
return $entity;
}
throw new EntityNotFoundException('Cannot resolve entity identifier "'.implode('", "', (array)$id).'"');
}
/**
* Find entity by its primary key, or throws exception if not found in repository
* In case of composite primary key, the primary key can be resolved by previous where() call
*
* Note: If criterion which is not part of the primary key is passed, or if the primary key is not complete, the query will throw an {@see QueryBuildingException}.
*
* @param ReadCommandInterface<ConnectionInterface, E>&Whereable $query
* @param mixed|array<string, mixed> $id The entity PK. Use an array for composite PK
*
* @return E
*
* @throws EntityNotFoundException If entity is not found
* @throws PrimeException When query fail
*/
public function findByIdOrNew(ReadCommandInterface $query, $id)
{
$entity = $this->findById($query, $id);
if ($entity !== null) {
return $entity;
}
return $this->repository->entity($this->toCriteria($query) ?? []);
}
/**
* Execute the query and return the first result, or throw {@see EntityNotFoundException} if no result
*
* @param ReadCommandInterface<ConnectionInterface, E> $query
*
* @return E
*/
public function firstOrFail(ReadCommandInterface $query)
{
$entity = $query->first();
if ($entity !== null) {
return $entity;
}
throw new EntityNotFoundException('Cannot resolve entity');
}
/**
* Execute the query and return the first result, or instantiate a new entity if no result
*
* @param ReadCommandInterface<ConnectionInterface, E> $query
* @param bool $useCriteriaAsDefault If true, the criteria of the query will be used as default attributes
*
* @return E
*/
public function firstOrNew(ReadCommandInterface $query, bool $useCriteriaAsDefault = true)
{
$entity = $query->first();
if ($entity !== null) {
return $entity;
}
$attributes = $useCriteriaAsDefault ? $this->toCriteria($query) : null;
return $this->repository->entity($attributes ?? []);
}
/**
* Filter entities by a predicate
*
* The predicate will be compiled to a where clause, instead of be called on each entity
*
* <code>
* $query->filter(fn (User $user) => $user->enabled()); // WHERE enabled = 1
* $query->filter(fn (User $user) => $user->enabled() && $user->age() > 18); // WHERE enabled = 1 AND age > 18
* </code>
*
* @param ReadCommandInterface<ConnectionInterface, E> $query
* @param Closure(E):bool $predicate The predicate. Must take the entity as parameter, and return a boolean.
*
* @return ReadCommandInterface<ConnectionInterface, E>
*/
public function filter(ReadCommandInterface $query, Closure $predicate)
{
if (!$this->closureCompiler) {
throw new BadMethodCallException('Closure filter is not enabled.');
}
if (!$query instanceof Whereable) {
throw new BadMethodCallException('The query must implement ' . Whereable::class . ' to use filter with a closure.');
}
return $query->where($this->closureCompiler->compile($predicate));
}
/**
* Relations to load.
*
* Relations with their sub relations
* <code>
* $query->with([
* 'customer.packs',
* 'permissions'
* ]);
* </code>
*
* Use char '#' for polymorphic sub relation
* <code>
* $query->with('target#customer.packs');
* </code>
*
* @param ReadCommandInterface<ConnectionInterface, E> $query
* @param string|array $relations
*
* @return ReadCommandInterface<ConnectionInterface, E>
*/
public function with(ReadCommandInterface $query, $relations)
{
$this->withRelations = Relation::sanitizeRelations((array)$relations);
return $query;
}
/**
* Relations to discard
*
* @param ReadCommandInterface<ConnectionInterface, E> $query
* @param string|array $relations
*
* @return ReadCommandInterface<ConnectionInterface, E>
*/
public function without(ReadCommandInterface $query, $relations)
{
$this->withoutRelations = Relation::sanitizeWithoutRelations((array)$relations);
return $query;
}
/**
* Indexing entities by an attribute value
* Use combine for multiple entities with same attribute value
*
* @param ReadCommandInterface<ConnectionInterface, E> $query
* @param string $attribute
* @param boolean $combine
*
* @return ReadCommandInterface<ConnectionInterface, E>
*/
public function by(ReadCommandInterface $query, $attribute, $combine = false)
{
$this->byOptions = [
'attribute' => $attribute,
'combine' => $combine,
];
return $query;
}
/**
* Convert query where clause to key-value criteria
*
* @param ReadCommandInterface $query
*
* @return array<string, mixed>|null
*/
public function toCriteria(ReadCommandInterface $query): ?array
{
if ($query instanceof KeyValueQueryInterface) {
return $query->statement('where');
}
$criteria = [];
$statements = [];
// Flatten query with a single level of nesting
foreach ($query->statement('where') as $statement) {
if (CompositeExpression::TYPE_AND !== ($statement['glue'] ?? null)) {
// Only AND composite expression are supported
return null;
}
if (!isset($statement['nested'])) {
$statements[] = $statement;
} else {
/** @psalm-suppress InvalidOperand */
$statements = [...$statements, ...$statement['nested']];
}
}
foreach ($statements as $statement) {
if (
!isset($statement['column'], $statement['glue'], $statement['operator'])
|| $statement['glue'] !== CompositeExpression::TYPE_AND
|| $statement['operator'] !== '=' // @todo support :eq etc...
) {
return null; // Cannot extract complex criteria
}
$value = $statement['value'] ?? null;
if (is_array($value)) {
return null;
}
$criteria[$statement['column']] = $value;
}
return $criteria;
}
/**
* Post processor for hydrating entities
*
* @param ResultSetInterface<array<string, mixed>> $data
*
* @return array
* @throws PrimeException
*/
public function processEntities(ResultSetInterface $data)
{
/** @var EntityRepository $repository */
$repository = $this->repository;
$hasLoadEvent = $repository->hasListeners(Events::POST_LOAD);
// Save into local vars to ensure that value will not be changed during execution
$withRelations = $this->withRelations;
$withoutRelations = $this->withoutRelations;
$byOptions = $this->byOptions;
$entities = new EntityIndexer($this->mapper, $byOptions ? [$byOptions['attribute']] : []);
// Force loading of eager relations
if (!empty($this->metadata->eagerRelations)) {
$withRelations = array_merge($this->metadata->eagerRelations, $withRelations);
// Skip relation that should not be loaded.
foreach ($withoutRelations as $relationName => $nestedRelations) {
// Only a leaf concerns this query.
if (empty($nestedRelations)) {
unset($withRelations[$relationName]);
}
}
}
foreach ($data as $result) {
$entities->push($entity = $this->mapper->prepareFromRepository($result, $repository->connection()->platform()));
if ($hasLoadEvent) {
$repository->notify(Events::POST_LOAD, [$entity, $repository]);
}
}
foreach ($withRelations as $relationName => $relationInfos) {
$repository->relation($relationName)->load(
$entities,
$relationInfos['relations'],
$relationInfos['constraints'],
$withoutRelations[$relationName] ?? []
);
}
switch (true) {
case $byOptions === null:
return $entities->all();
case $byOptions['combine']:
return $entities->by($byOptions['attribute']);
default:
return $entities->byOverride($byOptions['attribute']);
}
}
/**
* Scope call
* run a scope defined in repository
*
* @param string $name Scope name
* @param array $arguments
*
* @return mixed
*/
public function __call($name, $arguments)
{
/** @var EntityRepository $this->repository */
$scopes = $this->repository->scopes();
if (!isset($scopes[$name])) {
throw new BadMethodCallException('Scope "' . get_class($this->mapper) . '::' . $name . '" not found');
}
return $scopes[$name](...$arguments);
}
/**
* Configure the query
*
* @param ReadCommandInterface $query
*
* @return void
*/
public function apply(ReadCommandInterface $query): void
{
$query->setExtension($this);
$query->post([$this, 'processEntities'], false);
}
}