19
19
use Symfony \Bridge \Doctrine \Attribute \MapEntity ;
20
20
use Symfony \Component \ExpressionLanguage \ExpressionLanguage ;
21
21
use Symfony \Component \HttpFoundation \Request ;
22
- use Symfony \Component \HttpKernel \Controller \ArgumentValueResolverInterface ;
22
+ use Symfony \Component \HttpKernel \Controller \ValueResolverInterface ;
23
23
use Symfony \Component \HttpKernel \ControllerMetadata \ArgumentMetadata ;
24
24
use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
25
25
29
29
* @author Fabien Potencier <fabien@symfony.com>
30
30
* @author Jérémy Derussé <jeremy@derusse.com>
31
31
*/
32
- final class EntityValueResolver implements ArgumentValueResolverInterface
32
+ final class EntityValueResolver implements ValueResolverInterface
33
33
{
34
34
public function __construct (
35
35
private ManagerRegistry $ registry ,
@@ -41,66 +41,36 @@ public function __construct(
41
41
/**
42
42
* {@inheritdoc}
43
43
*/
44
- public function supports (Request $ request , ArgumentMetadata $ argument ): bool
44
+ public function resolve (Request $ request , ArgumentMetadata $ argument ): array
45
45
{
46
- if (!$ this ->registry ->getManagerNames ()) {
47
- return false ;
48
- }
49
-
50
46
$ options = $ this ->getMapEntityAttribute ($ argument );
51
47
if (!$ options ->class || $ options ->disabled ) {
52
- return false ;
53
- }
54
-
55
- if (null === $ options ->expr && !($ options ->mapping || $ options ->exclude ) && null === $ this ->getIdentifier ($ request , $ options , $ argument ->getName ())) {
56
- return false ;
57
- }
58
-
59
- // Doctrine Entity?
60
- if (!$ objectManager = $ this ->getManager ($ options ->objectManager , $ options ->class )) {
61
- return false ;
48
+ return [];
62
49
}
63
-
64
- if ($ objectManager ->getMetadataFactory ()->isTransient ($ options ->class )) {
65
- return false ;
50
+ if (!$ manager = $ this ->getManager ($ options ->objectManager , $ options ->class )) {
51
+ return [];
66
52
}
67
53
68
- return null !== $ options ->expr || $ this ->getCriteria ($ request , $ options , $ objectManager );
69
- }
70
-
71
- /**
72
- * {@inheritdoc}
73
- */
74
- public function resolve (Request $ request , ArgumentMetadata $ argument ): iterable
75
- {
76
- $ options = $ this ->getMapEntityAttribute ($ argument );
77
- $ name = $ argument ->getName ();
78
- $ objectManager = $ this ->getManager ($ options ->objectManager , $ options ->class );
79
-
80
- $ errorMessage = null ;
54
+ $ message = '' ;
81
55
if (null !== $ options ->expr ) {
82
- if (null === $ object = $ this ->findViaExpression ($ objectManager , $ request , $ options )) {
83
- $ errorMessage = sprintf ('The expression "%s" returned null ' , $ options ->expr );
56
+ if (null === $ object = $ this ->findViaExpression ($ manager , $ request , $ options )) {
57
+ $ message = sprintf (' The expression "%s" returned null. ' , $ options ->expr );
84
58
}
85
59
// find by identifier?
86
- } elseif (false === $ object = $ this ->find ($ objectManager , $ request , $ options , $ name )) {
60
+ } elseif (false === $ object = $ this ->find ($ manager , $ request , $ options , $ argument -> getName () )) {
87
61
// find by criteria
88
- if (false === $ object = $ this ->findOneBy ($ objectManager , $ request , $ options )) {
89
- if (!$ argument ->isNullable ()) {
90
- throw new \LogicException (sprintf ('Unable to guess how to get a Doctrine instance from the request information for parameter "%s". ' , $ name ));
91
- }
92
-
62
+ if (!$ criteria = $ this ->getCriteria ($ request , $ options , $ manager )) {
63
+ return [];
64
+ }
65
+ try {
66
+ $ object = $ manager ->getRepository ($ options ->class )->findOneBy ($ criteria );
67
+ } catch (NoResultException |ConversionException ) {
93
68
$ object = null ;
94
69
}
95
70
}
96
71
97
72
if (null === $ object && !$ argument ->isNullable ()) {
98
- $ message = sprintf ('"%s" object not found by the "%s" Argument Resolver. ' , $ options ->class , self ::class);
99
- if ($ errorMessage ) {
100
- $ message .= ' ' .$ errorMessage ;
101
- }
102
-
103
- throw new NotFoundHttpException ($ message );
73
+ throw new NotFoundHttpException (sprintf ('"%s" object not found by "%s". ' , $ options ->class , self ::class).$ message );
104
74
}
105
75
106
76
return [$ object ];
@@ -112,18 +82,16 @@ private function getManager(?string $name, string $class): ?ObjectManager
112
82
return $ this ->registry ->getManagerForClass ($ class );
113
83
}
114
84
115
- if (!isset ($ this ->registry ->getManagerNames ()[$ name ])) {
116
- return null ;
117
- }
118
-
119
85
try {
120
- return $ this ->registry ->getManager ($ name );
86
+ $ manager = $ this ->registry ->getManager ($ name );
121
87
} catch (\InvalidArgumentException ) {
122
88
return null ;
123
89
}
90
+
91
+ return $ manager ->getMetadataFactory ()->isTransient ($ class ) ? null : $ manager ;
124
92
}
125
93
126
- private function find (ObjectManager $ objectManager , Request $ request , MapEntity $ options , string $ name ): false |object |null
94
+ private function find (ObjectManager $ manager , Request $ request , MapEntity $ options , string $ name ): false |object |null
127
95
{
128
96
if ($ options ->mapping || $ options ->exclude ) {
129
97
return false ;
@@ -134,15 +102,15 @@ private function find(ObjectManager $objectManager, Request $request, MapEntity
134
102
return $ id ;
135
103
}
136
104
137
- if ($ options ->evictCache && $ objectManager instanceof EntityManagerInterface) {
138
- $ cacheProvider = $ objectManager ->getCache ();
105
+ if ($ options ->evictCache && $ manager instanceof EntityManagerInterface) {
106
+ $ cacheProvider = $ manager ->getCache ();
139
107
if ($ cacheProvider && $ cacheProvider ->containsEntity ($ options ->class , $ id )) {
140
108
$ cacheProvider ->evictEntity ($ options ->class , $ id );
141
109
}
142
110
}
143
111
144
112
try {
145
- return $ objectManager ->getRepository ($ options ->class )->find ($ id );
113
+ return $ manager ->getRepository ($ options ->class )->find ($ id );
146
114
} catch (NoResultException |ConversionException ) {
147
115
return null ;
148
116
}
@@ -179,20 +147,7 @@ private function getIdentifier(Request $request, MapEntity $options, string $nam
179
147
return false ;
180
148
}
181
149
182
- private function findOneBy (ObjectManager $ objectManager , Request $ request , MapEntity $ options ): false |object |null
183
- {
184
- if (!$ criteria = $ this ->getCriteria ($ request , $ options , $ objectManager )) {
185
- return false ;
186
- }
187
-
188
- try {
189
- return $ objectManager ->getRepository ($ options ->class )->findOneBy ($ criteria );
190
- } catch (NoResultException |ConversionException ) {
191
- return null ;
192
- }
193
- }
194
-
195
- private function getCriteria (Request $ request , MapEntity $ options , ObjectManager $ objectManager ): array
150
+ private function getCriteria (Request $ request , MapEntity $ options , ObjectManager $ manager ): array
196
151
{
197
152
if (null === $ mapping = $ options ->mapping ) {
198
153
$ mapping = $ request ->attributes ->keys ();
@@ -217,7 +172,7 @@ private function getCriteria(Request $request, MapEntity $options, ObjectManager
217
172
}
218
173
219
174
$ criteria = [];
220
- $ metadata = $ objectManager ->getClassMetadata ($ options ->class );
175
+ $ metadata = $ manager ->getClassMetadata ($ options ->class );
221
176
222
177
foreach ($ mapping as $ attribute => $ field ) {
223
178
if (!$ metadata ->hasField ($ field ) && (!$ metadata ->hasAssociation ($ field ) || !$ metadata ->isSingleValuedAssociation ($ field ))) {
@@ -234,13 +189,13 @@ private function getCriteria(Request $request, MapEntity $options, ObjectManager
234
189
return $ criteria ;
235
190
}
236
191
237
- private function findViaExpression (ObjectManager $ objectManager , Request $ request , MapEntity $ options ): ?object
192
+ private function findViaExpression (ObjectManager $ manager , Request $ request , MapEntity $ options ): ?object
238
193
{
239
194
if (!$ this ->expressionLanguage ) {
240
195
throw new \LogicException (sprintf ('You cannot use the "%s" if the ExpressionLanguage component is not available. Try running "composer require symfony/expression-language". ' , __CLASS__ ));
241
196
}
242
197
243
- $ repository = $ objectManager ->getRepository ($ options ->class );
198
+ $ repository = $ manager ->getRepository ($ options ->class );
244
199
$ variables = array_merge ($ request ->attributes ->all (), ['repository ' => $ repository ]);
245
200
246
201
try {
0 commit comments