1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Util; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\EntityManager; |
17
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Antoine Bluchet <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
trait ShouldEagerLoad |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Checks if an operation has a `force_eager` attribute. |
26
|
|
|
* |
27
|
|
|
* @param string $resourceClass |
28
|
|
|
* @param array $options |
29
|
|
|
* |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
private function shouldOperationForceEager(string $resourceClass, array $options): bool |
33
|
|
|
{ |
34
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
|
|
|
|
35
|
|
|
|
36
|
|
|
if (isset($options['collection_operation_name'])) { |
37
|
|
|
$forceEager = $resourceMetadata->getCollectionOperationAttribute($options['collection_operation_name'], 'force_eager', null, true); |
38
|
|
|
} elseif (isset($options['item_operation_name'])) { |
39
|
|
|
$forceEager = $resourceMetadata->getItemOperationAttribute($options['item_operation_name'], 'force_eager', null, true); |
40
|
|
|
} else { |
41
|
|
|
$forceEager = $resourceMetadata->getAttribute('force_eager'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return is_bool($forceEager) ? $forceEager : $this->forceEager; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Checkes if the class has an associationMapping with FETCH=EAGER. |
49
|
|
|
* |
50
|
|
|
* @param EntityManager $em |
51
|
|
|
* @param ClassMetadataInfo $classMetadata |
52
|
|
|
* @param array $checked array cache of tested metadata classes |
53
|
|
|
*/ |
54
|
|
|
private function hasFetchEagerAssociation(EntityManager $em, ClassMetadataInfo $classMetadata, &$checked = []) |
55
|
|
|
{ |
56
|
|
|
$checked[] = $classMetadata->name; |
57
|
|
|
|
58
|
|
|
foreach ($classMetadata->associationMappings as $mapping) { |
59
|
|
|
if (ClassMetadataInfo::FETCH_EAGER === $mapping['fetch']) { |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$related = $em->getClassMetadata($mapping['targetEntity']); |
64
|
|
|
|
65
|
|
|
if (in_array($related->name, $checked, true)) { |
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (true === $this->hasFetchEagerAssociation($em, $related, $checked)) { |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: