1919use Symfony \Bridge \Doctrine \Attribute \MapEntity ;
2020use Symfony \Component \ExpressionLanguage \ExpressionLanguage ;
2121use Symfony \Component \HttpFoundation \Request ;
22- use Symfony \Component \HttpKernel \Controller \ArgumentValueResolverInterface ;
22+ use Symfony \Component \HttpKernel \Controller \ValueResolverInterface ;
2323use Symfony \Component \HttpKernel \ControllerMetadata \ArgumentMetadata ;
2424use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
2525
2929 * @author Fabien Potencier <fabien@symfony.com>
3030 * @author Jérémy Derussé <jeremy@derusse.com>
3131 */
32- final class EntityValueResolver implements ArgumentValueResolverInterface
32+ final class EntityValueResolver implements ValueResolverInterface
3333{
3434 public function __construct (
3535 private ManagerRegistry $ registry ,
@@ -41,66 +41,36 @@ public function __construct(
4141 /**
4242 * {@inheritdoc}
4343 */
44- public function supports (Request $ request , ArgumentMetadata $ argument ): bool
44+ public function resolve (Request $ request , ArgumentMetadata $ argument ): array
4545 {
46- if (!$ this ->registry ->getManagerNames ()) {
47- return false ;
48- }
49-
5046 $ options = $ this ->getMapEntityAttribute ($ argument );
5147 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 [];
6249 }
63-
64- if ($ objectManager ->getMetadataFactory ()->isTransient ($ options ->class )) {
65- return false ;
50+ if (!$ manager = $ this ->getManager ($ options ->objectManager , $ options ->class )) {
51+ return [];
6652 }
6753
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 = '' ;
8155 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 );
8458 }
8559 // find by identifier?
86- } elseif (false === $ object = $ this ->find ($ objectManager , $ request , $ options , $ name )) {
60+ } elseif (false === $ object = $ this ->find ($ manager , $ request , $ options , $ argument -> getName () )) {
8761 // 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 ) {
9368 $ object = null ;
9469 }
9570 }
9671
9772 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 );
10474 }
10575
10676 return [$ object ];
@@ -112,18 +82,16 @@ private function getManager(?string $name, string $class): ?ObjectManager
11282 return $ this ->registry ->getManagerForClass ($ class );
11383 }
11484
115- if (!isset ($ this ->registry ->getManagerNames ()[$ name ])) {
116- return null ;
117- }
118-
11985 try {
120- return $ this ->registry ->getManager ($ name );
86+ $ manager = $ this ->registry ->getManager ($ name );
12187 } catch (\InvalidArgumentException ) {
12288 return null ;
12389 }
90+
91+ return $ manager ->getMetadataFactory ()->isTransient ($ class ) ? null : $ manager ;
12492 }
12593
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
12795 {
12896 if ($ options ->mapping || $ options ->exclude ) {
12997 return false ;
@@ -134,15 +102,15 @@ private function find(ObjectManager $objectManager, Request $request, MapEntity
134102 return $ id ;
135103 }
136104
137- if ($ options ->evictCache && $ objectManager instanceof EntityManagerInterface) {
138- $ cacheProvider = $ objectManager ->getCache ();
105+ if ($ options ->evictCache && $ manager instanceof EntityManagerInterface) {
106+ $ cacheProvider = $ manager ->getCache ();
139107 if ($ cacheProvider && $ cacheProvider ->containsEntity ($ options ->class , $ id )) {
140108 $ cacheProvider ->evictEntity ($ options ->class , $ id );
141109 }
142110 }
143111
144112 try {
145- return $ objectManager ->getRepository ($ options ->class )->find ($ id );
113+ return $ manager ->getRepository ($ options ->class )->find ($ id );
146114 } catch (NoResultException |ConversionException ) {
147115 return null ;
148116 }
@@ -179,20 +147,7 @@ private function getIdentifier(Request $request, MapEntity $options, string $nam
179147 return false ;
180148 }
181149
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
196151 {
197152 if (null === $ mapping = $ options ->mapping ) {
198153 $ mapping = $ request ->attributes ->keys ();
@@ -217,7 +172,7 @@ private function getCriteria(Request $request, MapEntity $options, ObjectManager
217172 }
218173
219174 $ criteria = [];
220- $ metadata = $ objectManager ->getClassMetadata ($ options ->class );
175+ $ metadata = $ manager ->getClassMetadata ($ options ->class );
221176
222177 foreach ($ mapping as $ attribute => $ field ) {
223178 if (!$ metadata ->hasField ($ field ) && (!$ metadata ->hasAssociation ($ field ) || !$ metadata ->isSingleValuedAssociation ($ field ))) {
@@ -234,13 +189,13 @@ private function getCriteria(Request $request, MapEntity $options, ObjectManager
234189 return $ criteria ;
235190 }
236191
237- private function findViaExpression (ObjectManager $ objectManager , Request $ request , MapEntity $ options ): ?object
192+ private function findViaExpression (ObjectManager $ manager , Request $ request , MapEntity $ options ): ?object
238193 {
239194 if (!$ this ->expressionLanguage ) {
240195 throw new \LogicException (sprintf ('You cannot use the "%s" if the ExpressionLanguage component is not available. Try running "composer require symfony/expression-language". ' , __CLASS__ ));
241196 }
242197
243- $ repository = $ objectManager ->getRepository ($ options ->class );
198+ $ repository = $ manager ->getRepository ($ options ->class );
244199 $ variables = array_merge ($ request ->attributes ->all (), ['repository ' => $ repository ]);
245200
246201 try {
0 commit comments