|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cycle\ORM\Select; |
|
6
|
|
|
|
|
7
|
|
|
use Cycle\Database\Query\SelectQuery; |
|
8
|
|
|
use Cycle\ORM\Exception\FactoryException; |
|
9
|
|
|
use Cycle\ORM\Exception\LoaderException; |
|
10
|
|
|
use Cycle\ORM\Exception\SchemaException; |
|
11
|
|
|
use Cycle\ORM\FactoryInterface; |
|
12
|
|
|
use Cycle\ORM\Parser\AbstractNode; |
|
13
|
|
|
use Cycle\ORM\Service\SourceProviderInterface; |
|
14
|
|
|
use Cycle\ORM\Relation; |
|
15
|
|
|
use Cycle\ORM\SchemaInterface; |
|
16
|
|
|
use Cycle\ORM\Select\Loader\ParentLoader; |
|
17
|
|
|
use Cycle\ORM\Select\Loader\SubclassLoader; |
|
18
|
|
|
use Cycle\ORM\Select\Traits\AliasTrait; |
|
19
|
|
|
use Cycle\ORM\Select\Traits\ChainTrait; |
|
20
|
|
|
use JetBrains\PhpStorm\Deprecated; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* ORM Loaders used to load an compile data tree based on results fetched from SQL databases, |
|
24
|
|
|
* loaders can communicate with SelectQuery by providing it's own set of conditions, columns |
|
25
|
|
|
* joins and etc. In some cases loader may create additional selector to load data using information |
|
26
|
|
|
* fetched from previous query. |
|
27
|
|
|
* |
|
28
|
|
|
* Attention, AbstractLoader can only work with ORM Records, you must implement LoaderInterface |
|
29
|
|
|
* in order to support external references (MongoDB and etc). |
|
30
|
|
|
* |
|
31
|
|
|
* Loaders can be used for both - loading and filtering of record data. |
|
32
|
|
|
* |
|
33
|
|
|
* Reference tree generation logic example: |
|
34
|
|
|
* User has many Posts (relation "posts"), user primary is ID, post inner key pointing to user |
|
35
|
|
|
* is USER_ID. Post loader must request User data loader to create references based on ID field |
|
36
|
|
|
* values. Once Post data were parsed we can mount it under parent user using mount method: |
|
37
|
|
|
* |
|
38
|
|
|
* @see Select::load() |
|
39
|
|
|
* @see Select::with() |
|
40
|
|
|
* |
|
41
|
|
|
* @internal |
|
42
|
|
|
*/ |
|
43
|
|
|
abstract class AbstractLoader implements LoaderInterface |
|
44
|
|
|
{ |
|
45
|
|
|
use AliasTrait; |
|
46
|
|
|
use ChainTrait; |
|
47
|
|
|
|
|
48
|
|
|
// Loading methods for data loaders. |
|
49
|
|
|
public const INLOAD = 1; |
|
50
|
|
|
public const POSTLOAD = 2; |
|
51
|
|
|
public const JOIN = 3; |
|
52
|
|
|
public const LEFT_JOIN = 4; |
|
53
|
|
|
protected const SUBQUERY = 5; |
|
54
|
|
|
|
|
55
|
|
|
protected array $options = [ |
|
56
|
|
|
'load' => false, |
|
57
|
|
|
'scope' => true, |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
/** @var LoaderInterface[] */ |
|
61
|
|
|
protected array $load = []; |
|
62
|
|
|
|
|
63
|
|
|
/** @var AbstractLoader[] */ |
|
64
|
|
|
protected array $join = []; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Parent in class inheritance hierarchy |
|
68
|
|
|
*/ |
|
69
|
|
|
protected ?AbstractLoader $inherit = null; |
|
70
|
|
|
|
|
71
|
|
|
/** @var SubclassLoader[] */ |
|
72
|
|
|
protected array $subclasses = []; |
|
73
|
|
|
|
|
74
|
|
|
protected bool $loadSubclasses = true; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Loader that contains current loader |
|
78
|
|
|
*/ |
|
79
|
|
|
protected ?LoaderInterface $parent = null; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Children roles for Joined Table Inheritance |
|
83
|
|
|
* |
|
84
|
|
|
* @var array<string, array> |
|
85
|
|
|
*/ |
|
86
|
|
|
protected array $children; |
|
87
|
|
|
|
|
88
|
6834 |
|
protected SourceInterface $source; |
|
89
|
|
|
|
|
90
|
|
|
public function __construct( |
|
91
|
|
|
protected SchemaInterface $ormSchema, |
|
92
|
|
|
protected SourceProviderInterface $sourceProvider, |
|
93
|
|
|
protected FactoryInterface $factory, |
|
94
|
6834 |
|
protected string $target, |
|
95
|
6834 |
|
) { |
|
96
|
|
|
$this->children = $this->ormSchema->getInheritedRoles($target); |
|
97
|
|
|
$this->source = $this->sourceProvider->getSource($target); |
|
98
|
6834 |
|
} |
|
99
|
|
|
|
|
100
|
6834 |
|
public function isHierarchical(): bool |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->inherit !== null || ($this->loadSubclasses && $this->children !== []); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function setSubclassesLoading(bool $enabled): void |
|
106
|
6064 |
|
{ |
|
107
|
|
|
$this->loadSubclasses = $enabled; |
|
108
|
6064 |
|
} |
|
109
|
|
|
|
|
110
|
6064 |
|
public function getTarget(): string |
|
111
|
1386 |
|
{ |
|
112
|
|
|
return $this->target; |
|
113
|
|
|
} |
|
114
|
6064 |
|
|
|
115
|
72 |
|
public function withContext(LoaderInterface $parent, array $options = []): static |
|
116
|
|
|
{ |
|
117
|
|
|
// check that given options are known |
|
118
|
6064 |
|
if (!empty($wrong = \array_diff(\array_keys($options), \array_keys($this->options)))) { |
|
119
|
|
|
throw new LoaderException( |
|
120
|
6064 |
|
\sprintf( |
|
121
|
176 |
|
'Relation %s does not support option: %s', |
|
122
|
|
|
$this::class, |
|
123
|
|
|
\implode(',', $wrong), |
|
124
|
|
|
), |
|
125
|
1146 |
|
); |
|
126
|
|
|
} |
|
127
|
1146 |
|
|
|
128
|
|
|
$loader = clone $this; |
|
129
|
|
|
$loader->parent = $parent; |
|
130
|
264 |
|
$loader->options = $options + $this->options; |
|
131
|
|
|
|
|
132
|
264 |
|
return $loader; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
6358 |
|
/** |
|
136
|
|
|
* Load the relation. |
|
137
|
6358 |
|
* |
|
138
|
|
|
* @param LoaderInterface|string $relation Relation name, or chain of relations separated by. If you need to set |
|
139
|
|
|
* inheritance then pass LoaderInterface object |
|
140
|
4458 |
|
* @param array $options Loader options (to be applied to last chain element only). |
|
141
|
|
|
* @param bool $join When set to true loaders will be forced into JOIN mode. |
|
142
|
|
|
* @param bool $load Load relation data. |
|
143
|
4458 |
|
* |
|
144
|
|
|
* @return LoaderInterface Must return loader for a requested relation. |
|
145
|
|
|
* @throws LoaderException |
|
146
|
|
|
*/ |
|
147
|
|
|
public function loadRelation( |
|
148
|
|
|
string|LoaderInterface $relation, |
|
149
|
|
|
array $options, |
|
150
|
|
|
bool $join = false, |
|
151
|
|
|
bool $load = false, |
|
152
|
|
|
): LoaderInterface { |
|
153
|
4458 |
|
if ($relation instanceof ParentLoader) { |
|
154
|
4458 |
|
return $this->inherit = $relation->withContext($this); |
|
155
|
4458 |
|
} |
|
156
|
|
|
|
|
157
|
4458 |
|
if ($relation instanceof SubclassLoader) { |
|
158
|
|
|
$loader = $relation->withContext($this); |
|
159
|
|
|
$this->subclasses[] = $loader; |
|
160
|
|
|
return $loader; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
$relation = $this->resolvePath($relation); |
|
|
|
|
|
|
164
|
|
|
$alias ??= $options['alias'] ?? $relation; |
|
|
|
|
|
|
165
|
|
|
unset($options['alias']); |
|
166
|
|
|
if (!empty($options['as'])) { |
|
167
|
|
|
// ?? |
|
168
|
|
|
$this->registerPath($options['as'], $alias); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
// Check if relation contain dot, i.e. relation chain |
|
172
|
|
|
if ($this->isChain($relation)) { |
|
173
|
4554 |
|
return $this->loadChain($relation, $options, $join, $load, $alias); |
|
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/* |
|
177
|
|
|
* Joined loaders must be isolated from normal loaders due they would not load any data |
|
178
|
|
|
* and will only modify SelectQuery. |
|
179
|
4554 |
|
*/ |
|
180
|
744 |
|
if (!$join || $load) { |
|
181
|
|
|
$loaders = &$this->load; |
|
182
|
4394 |
|
} else { |
|
183
|
616 |
|
$loaders = &$this->join; |
|
184
|
616 |
|
} |
|
185
|
616 |
|
|
|
186
|
|
|
if ($load) { |
|
187
|
4082 |
|
$options['load'] ??= true; |
|
188
|
4082 |
|
} |
|
189
|
160 |
|
|
|
190
|
|
|
if (isset($loaders[$relation])) { |
|
191
|
|
|
// Overwrite existing loader options |
|
192
|
|
|
return $loaders[$alias] = $loaders[$alias]->withContext($this, $options); |
|
193
|
4082 |
|
} |
|
194
|
362 |
|
|
|
195
|
|
|
if ($join) { |
|
196
|
|
|
if (empty($options['method']) || !\in_array($options['method'], [self::JOIN, self::LEFT_JOIN], true)) { |
|
197
|
|
|
// let's tell our loaded that it's method is JOIN (forced) |
|
198
|
|
|
$options['method'] = self::JOIN; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
4082 |
|
|
|
202
|
3642 |
|
try { |
|
203
|
|
|
// Creating new loader. |
|
204
|
552 |
|
$loader = $this->factory->loader( |
|
205
|
|
|
$this->ormSchema, |
|
206
|
|
|
$this->sourceProvider, |
|
207
|
4082 |
|
$this->target, |
|
208
|
3642 |
|
$relation, |
|
209
|
|
|
); |
|
210
|
|
|
} catch (SchemaException | FactoryException $e) { |
|
211
|
4082 |
|
if ($this->inherit instanceof self) { |
|
212
|
|
|
return $this->inherit->loadRelation($relation, $options, $join, $load, $alias); |
|
|
|
|
|
|
213
|
416 |
|
} |
|
214
|
|
|
throw new LoaderException( |
|
215
|
|
|
\sprintf('Unable to create loader: %s', $e->getMessage()), |
|
216
|
4082 |
|
$e->getCode(), |
|
217
|
552 |
|
$e, |
|
218
|
|
|
); |
|
219
|
544 |
|
} |
|
220
|
|
|
|
|
221
|
|
|
return $loaders[$alias] = $loader->withContext($this, $options); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function createNode(): AbstractNode |
|
225
|
4082 |
|
{ |
|
226
|
4082 |
|
$node = $this->initNode(); |
|
227
|
4082 |
|
|
|
228
|
4082 |
|
if ($this->inherit !== null) { |
|
229
|
|
|
$node->joinNode(null, $this->inherit->createNode()); |
|
230
|
|
|
} |
|
231
|
64 |
|
|
|
232
|
64 |
|
foreach ($this->load as $relation => $loader) { |
|
233
|
56 |
|
if ($loader instanceof JoinableInterface && $loader->isJoined()) { |
|
234
|
|
|
$node->joinNode($relation, $loader->createNode()); |
|
235
|
8 |
|
continue; |
|
236
|
8 |
|
} |
|
237
|
8 |
|
|
|
238
|
|
|
$node->linkNode($relation, $loader->createNode()); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
if ($this->loadSubclasses) { |
|
242
|
4082 |
|
foreach ($this->subclasses as $loader) { |
|
243
|
|
|
$node->joinNode(null, $loader->createNode()); |
|
244
|
|
|
} |
|
245
|
6382 |
|
} |
|
246
|
|
|
|
|
247
|
6382 |
|
return $node; |
|
248
|
|
|
} |
|
249
|
6382 |
|
|
|
250
|
744 |
|
public function loadData(AbstractNode $node, bool $includeRole = false): void |
|
251
|
|
|
{ |
|
252
|
|
|
$this->loadChild($node, $includeRole); |
|
253
|
6382 |
|
} |
|
254
|
3554 |
|
|
|
255
|
1370 |
|
public function getSource(): SourceInterface |
|
256
|
1370 |
|
{ |
|
257
|
|
|
return $this->source; |
|
258
|
|
|
} |
|
259
|
2378 |
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Returns inheritance parent loader. |
|
262
|
6382 |
|
*/ |
|
263
|
6334 |
|
public function getParentLoader(): ?LoaderInterface |
|
264
|
336 |
|
{ |
|
265
|
|
|
return $this->inherit; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
6382 |
|
/** |
|
269
|
|
|
* Returns all loaders that are joined to the current loader. |
|
270
|
|
|
* |
|
271
|
3842 |
|
* @return LoaderInterface[] |
|
272
|
|
|
*/ |
|
273
|
3842 |
|
public function getJoinedLoaders(): array |
|
274
|
|
|
{ |
|
275
|
|
|
return $this->join; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Indicates that loader loads data. |
|
280
|
|
|
*/ |
|
281
|
|
|
abstract public function isLoaded(): bool; |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Ensure state of every nested loader. |
|
285
|
|
|
*/ |
|
286
|
3842 |
|
public function __clone() |
|
287
|
|
|
{ |
|
288
|
3842 |
|
$this->parent = null; |
|
289
|
730 |
|
|
|
290
|
|
|
foreach ($this->load as $name => $loader) { |
|
291
|
3842 |
|
$this->load[$name] = $loader->withContext($this); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
6334 |
|
foreach ($this->join as $name => $loader) { |
|
295
|
|
|
$this->join[$name] = $loader->withContext($this); |
|
296
|
6334 |
|
} |
|
297
|
168 |
|
|
|
298
|
|
|
$this->inherit = $this->inherit?->withContext($this); |
|
299
|
|
|
|
|
300
|
|
|
foreach ($this->subclasses as $i => $loader) { |
|
301
|
6286 |
|
$this->subclasses[$i] = $loader->withContext($this); |
|
302
|
744 |
|
} |
|
303
|
744 |
|
} |
|
304
|
|
|
|
|
305
|
|
|
final public function __destruct() |
|
306
|
|
|
{ |
|
307
|
6286 |
|
unset($this->parent, $this->inherit, $this->subclasses, $this->load, $this->join); |
|
308
|
6286 |
|
} |
|
309
|
6286 |
|
|
|
310
|
328 |
|
protected function loadChild(AbstractNode $node, bool $includeRole = false): void |
|
311
|
328 |
|
{ |
|
312
|
|
|
foreach ($this->load as $relation => $loader) { |
|
313
|
|
|
$loader->loadData($node->getNode($relation), $includeRole); |
|
314
|
|
|
} |
|
315
|
6286 |
|
$this->loadHierarchy($node, $includeRole); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* @deprecated |
|
320
|
|
|
* |
|
321
|
|
|
* @codeCoverageIgnore |
|
322
|
|
|
*/ |
|
323
|
6494 |
|
#[Deprecated('2.3', '$this->loadHierarchy(%parameter0%, %parameter1%)')] |
|
324
|
|
|
protected function loadIerarchy(AbstractNode $node, bool $includeRole = false): void |
|
325
|
6494 |
|
{ |
|
326
|
|
|
$this->loadHierarchy($node, $includeRole); |
|
327
|
6494 |
|
} |
|
328
|
744 |
|
|
|
329
|
|
|
protected function loadHierarchy(AbstractNode $node, bool $includeRole = false): void |
|
330
|
|
|
{ |
|
331
|
6494 |
|
if ($this->inherit === null && !$this->loadSubclasses) { |
|
332
|
552 |
|
return; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
6494 |
|
// Merge parent nodes |
|
336
|
3562 |
|
if ($this->inherit !== null) { |
|
337
|
1378 |
|
$inheritNode = $node->getParentMergeNode(); |
|
338
|
24 |
|
$this->inherit->loadData($inheritNode, $includeRole); |
|
339
|
1354 |
|
} |
|
340
|
|
|
|
|
341
|
|
|
// Merge subclass nodes |
|
342
|
|
|
if ($this->loadSubclasses) { |
|
343
|
6470 |
|
$subclassNodes = $node->getSubclassMergeNodes(); |
|
344
|
6422 |
|
foreach ($this->subclasses as $i => $loader) { |
|
345
|
328 |
|
$inheritNode = $subclassNodes[$i]; |
|
346
|
|
|
$loader->loadData($inheritNode, $includeRole); |
|
347
|
|
|
} |
|
348
|
|
|
} |
|
349
|
6470 |
|
|
|
350
|
|
|
$node->mergeInheritanceNodes($includeRole); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* Create input node for the loader. |
|
355
|
|
|
*/ |
|
356
|
|
|
abstract protected function initNode(): AbstractNode; |
|
357
|
|
|
|
|
358
|
|
|
protected function configureQuery(SelectQuery $query): SelectQuery |
|
359
|
6834 |
|
{ |
|
360
|
|
|
$query = $this->applyScope($query); |
|
361
|
6834 |
|
|
|
362
|
|
|
if ($this->inherit !== null) { |
|
363
|
|
|
$query = $this->inherit->configureQuery($query); |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
foreach ($this->join as $loader) { |
|
367
|
6834 |
|
$query = $loader->configureQuery($query); |
|
368
|
|
|
} |
|
369
|
6834 |
|
|
|
370
|
6834 |
|
foreach ($this->load as $loader) { |
|
371
|
6834 |
|
if ($loader instanceof JoinableInterface && $loader->isJoined()) { |
|
372
|
744 |
|
$query = $loader->isHierarchical() |
|
373
|
|
|
? $loader->configureSubQuery($query) |
|
|
|
|
|
|
374
|
6834 |
|
: $loader->configureQuery($query); |
|
375
|
6834 |
|
} |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
6834 |
|
if ($this->loadSubclasses) { |
|
379
|
|
|
foreach ($this->subclasses as $loader) { |
|
380
|
6834 |
|
$query = $loader->configureQuery($query); |
|
381
|
6834 |
|
} |
|
382
|
6834 |
|
} |
|
383
|
6834 |
|
|
|
384
|
|
|
return $query; |
|
385
|
|
|
} |
|
386
|
6834 |
|
|
|
387
|
|
|
abstract protected function applyScope(SelectQuery $query): SelectQuery; |
|
388
|
6834 |
|
|
|
389
|
616 |
|
/** |
|
390
|
616 |
|
* Define schema option associated with the entity. |
|
391
|
616 |
|
* |
|
392
|
|
|
* @return mixed |
|
393
|
|
|
*/ |
|
394
|
|
|
protected function define(int $property) |
|
395
|
|
|
{ |
|
396
|
6834 |
|
return $this->ormSchema->define($this->target, $property); |
|
397
|
|
|
} |
|
398
|
6834 |
|
|
|
399
|
6834 |
|
/** |
|
400
|
5278 |
|
* Returns list of relations to be automatically joined with parent object. |
|
401
|
840 |
|
*/ |
|
402
|
|
|
protected function getEagerLoaders(?string $role = null): \Generator |
|
403
|
|
|
{ |
|
404
|
|
|
$role ??= $this->target; |
|
405
|
|
|
$parentLoader = $this->generateParentLoader($role); |
|
406
|
|
|
if ($parentLoader !== null) { |
|
407
|
|
|
yield $parentLoader; |
|
408
|
|
|
} |
|
409
|
|
|
yield from $this->generateSublassLoaders(); |
|
410
|
|
|
yield from $this->generateEagerRelationLoaders($role); |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
protected function generateParentLoader(string $role): ?LoaderInterface |
|
414
|
|
|
{ |
|
415
|
|
|
$parent = $this->ormSchema->define($role, SchemaInterface::PARENT); |
|
416
|
|
|
return $parent === null |
|
417
|
|
|
? null |
|
418
|
|
|
: $this->factory->loader($this->ormSchema, $this->sourceProvider, $role, FactoryInterface::PARENT_LOADER); |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
protected function generateSublassLoaders(): iterable |
|
422
|
|
|
{ |
|
423
|
|
|
if ($this->children !== []) { |
|
424
|
|
|
foreach ($this->children as $subRole => $children) { |
|
425
|
|
|
yield $this->factory |
|
426
|
|
|
->loader($this->ormSchema, $this->sourceProvider, $subRole, FactoryInterface::CHILD_LOADER); |
|
427
|
|
|
} |
|
428
|
|
|
} |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
protected function generateEagerRelationLoaders(string $target): \Generator |
|
432
|
|
|
{ |
|
433
|
|
|
$relations = $this->ormSchema->define($target, SchemaInterface::RELATIONS) ?? []; |
|
434
|
|
|
foreach ($relations as $relation => $schema) { |
|
435
|
|
|
if (($schema[Relation::LOAD] ?? null) === Relation::LOAD_EAGER) { |
|
436
|
|
|
yield $relation; |
|
437
|
|
|
} |
|
438
|
|
|
} |
|
439
|
|
|
} |
|
440
|
|
|
} |
|
441
|
|
|
|