These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\Common\Persistence\ManagerRegistry; |
||
17 | use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
||
18 | use Doctrine\ORM\Query\Expr\Join; |
||
19 | use Doctrine\ORM\Query\Expr\OrderBy; |
||
20 | use Doctrine\ORM\QueryBuilder; |
||
21 | |||
22 | /** |
||
23 | * Utility functions for working with Doctrine ORM query. |
||
24 | * |
||
25 | * @author Teoh Han Hui <[email protected]> |
||
26 | * @author Vincent Chalamon <[email protected]> |
||
27 | */ |
||
28 | final class QueryJoinParser |
||
29 | { |
||
30 | private function __construct() |
||
31 | { |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Gets the class metadata from a given join alias. |
||
36 | * |
||
37 | * @param string $alias |
||
38 | * @param QueryBuilder $queryBuilder |
||
39 | * @param ManagerRegistry $managerRegistry |
||
40 | * |
||
41 | * @return ClassMetadata |
||
42 | */ |
||
43 | public static function getClassMetadataFromJoinAlias(string $alias, QueryBuilder $queryBuilder, ManagerRegistry $managerRegistry): ClassMetadata |
||
44 | { |
||
45 | $rootEntities = $queryBuilder->getRootEntities(); |
||
46 | $rootAliases = $queryBuilder->getRootAliases(); |
||
47 | |||
48 | $joinParts = $queryBuilder->getDQLPart('join'); |
||
49 | |||
50 | $aliasMap = []; |
||
51 | $targetAlias = $alias; |
||
52 | |||
53 | foreach ($joinParts as $rootAlias => $joins) { |
||
54 | $aliasMap[$rootAlias] = 'root'; |
||
55 | |||
56 | foreach ($joins as $join) { |
||
57 | $alias = self::getJoinAlias($join); |
||
58 | $relationship = self::getJoinRelationship($join); |
||
59 | |||
60 | $pos = strpos($relationship, '.'); |
||
61 | |||
62 | $aliasMap[$alias] = [ |
||
63 | 'parentAlias' => substr($relationship, 0, $pos), |
||
64 | 'association' => substr($relationship, $pos + 1), |
||
65 | ]; |
||
66 | } |
||
67 | } |
||
68 | |||
69 | $associationStack = []; |
||
70 | $rootAlias = null; |
||
71 | |||
72 | while (null === $rootAlias) { |
||
73 | $mapping = $aliasMap[$targetAlias]; |
||
74 | |||
75 | if ('root' === $mapping) { |
||
76 | $rootAlias = $targetAlias; |
||
77 | } else { |
||
78 | $associationStack[] = $mapping['association']; |
||
79 | $targetAlias = $mapping['parentAlias']; |
||
80 | } |
||
81 | } |
||
82 | |||
83 | $rootEntity = $rootEntities[array_search($rootAlias, $rootAliases, true)]; |
||
84 | |||
85 | $rootMetadata = $managerRegistry |
||
86 | ->getManagerForClass($rootEntity) |
||
87 | ->getClassMetadata($rootEntity); |
||
88 | |||
89 | $metadata = $rootMetadata; |
||
90 | |||
91 | while (null !== ($association = array_pop($associationStack))) { |
||
92 | $associationClass = $metadata->getAssociationTargetClass($association); |
||
93 | |||
94 | $metadata = $managerRegistry |
||
95 | ->getManagerForClass($associationClass) |
||
96 | ->getClassMetadata($associationClass); |
||
97 | } |
||
98 | |||
99 | return $metadata; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Gets the relationship from a Join expression. |
||
104 | * |
||
105 | * @param Join $join |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | View Code Duplication | public static function getJoinRelationship(Join $join): string |
|
0 ignored issues
–
show
|
|||
110 | { |
||
111 | static $relationshipProperty = null; |
||
112 | static $initialized = false; |
||
113 | |||
114 | if (!$initialized && !method_exists(Join::class, 'getJoin')) { |
||
115 | $relationshipProperty = new \ReflectionProperty(Join::class, '_join'); |
||
116 | $relationshipProperty->setAccessible(true); |
||
117 | |||
118 | $initialized = true; |
||
119 | } |
||
120 | |||
121 | return (null === $relationshipProperty) ? $join->getJoin() : $relationshipProperty->getValue($join); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Gets the alias from a Join expression. |
||
126 | * |
||
127 | * @param Join $join |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | View Code Duplication | public static function getJoinAlias(Join $join): string |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
132 | { |
||
133 | static $aliasProperty = null; |
||
134 | static $initialized = false; |
||
135 | |||
136 | if (!$initialized && !method_exists(Join::class, 'getAlias')) { |
||
137 | $aliasProperty = new \ReflectionProperty(Join::class, '_alias'); |
||
138 | $aliasProperty->setAccessible(true); |
||
139 | |||
140 | $initialized = true; |
||
141 | } |
||
142 | |||
143 | return (null === $aliasProperty) ? $join->getAlias() : $aliasProperty->getValue($join); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Gets the parts from an OrderBy expression. |
||
148 | * |
||
149 | * @param OrderBy $orderBy |
||
150 | * |
||
151 | * @return string[] |
||
152 | */ |
||
153 | View Code Duplication | public static function getOrderByParts(OrderBy $orderBy): array |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
154 | { |
||
155 | static $partsProperty = null; |
||
156 | static $initialized = false; |
||
157 | |||
158 | if (!$initialized && !method_exists(OrderBy::class, 'getParts')) { |
||
159 | $partsProperty = new \ReflectionProperty(OrderBy::class, '_parts'); |
||
160 | $partsProperty->setAccessible(true); |
||
161 | |||
162 | $initialized = true; |
||
163 | } |
||
164 | |||
165 | return (null === $partsProperty) ? $orderBy->getParts() : $partsProperty->getValue($orderBy); |
||
166 | } |
||
167 | } |
||
168 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.