forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Improve EntityValueResolver #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jderusse
merged 1 commit into
jderusse:doctrine-resolver
from
nicolas-grekas:doctrine-resolver
Jul 12, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,24 +31,11 @@ | |
*/ | ||
final class EntityValueResolver implements ArgumentValueResolverInterface | ||
{ | ||
private array $defaultOptions = [ | ||
'object_manager' => null, | ||
'expr' => null, | ||
'mapping' => [], | ||
'exclude' => [], | ||
'strip_null' => false, | ||
'id' => null, | ||
'evict_cache' => false, | ||
'auto_mapping' => true, | ||
'attribute_only' => false, | ||
]; | ||
|
||
public function __construct( | ||
private ManagerRegistry $registry, | ||
private ?ExpressionLanguage $language = null, | ||
array $defaultOptions = [], | ||
private ?ExpressionLanguage $expressionLanguage = null, | ||
private MapEntity $defaults = new MapEntity(), | ||
) { | ||
$this->defaultOptions = array_merge($this->defaultOptions, $defaultOptions); | ||
} | ||
|
||
/** | ||
|
@@ -61,20 +48,16 @@ public function supports(Request $request, ArgumentMetadata $argument): bool | |
} | ||
|
||
$options = $this->getOptions($argument); | ||
if (null === $options['class']) { | ||
return false; | ||
} | ||
|
||
if ($options['attribute_only'] && !$options['has_attribute']) { | ||
if (!$options->class || $options->disabled) { | ||
return false; | ||
} | ||
|
||
// Doctrine Entity? | ||
if (null === $objectManager = $this->getManager($options['object_manager'], $options['class'])) { | ||
if (!$objectManager = $this->getManager($options->objectManager, $options->class)) { | ||
return false; | ||
} | ||
|
||
return !$objectManager->getMetadataFactory()->isTransient($options['class']); | ||
return !$objectManager->getMetadataFactory()->isTransient($options->class); | ||
} | ||
|
||
/** | ||
|
@@ -83,20 +66,18 @@ public function supports(Request $request, ArgumentMetadata $argument): bool | |
public function resolve(Request $request, ArgumentMetadata $argument): iterable | ||
{ | ||
$options = $this->getOptions($argument); | ||
|
||
$name = $argument->getName(); | ||
$class = $options['class']; | ||
$class = $options->class; | ||
|
||
$errorMessage = null; | ||
if (null !== $options['expr']) { | ||
if (null === $object = $this->findViaExpression($class, $request, $options['expr'], $options)) { | ||
$errorMessage = sprintf('The expression "%s" returned null', $options['expr']); | ||
if (null !== $options->expr) { | ||
if (null === $object = $this->findViaExpression($class, $request, $options->expr, $options)) { | ||
$errorMessage = sprintf('The expression "%s" returned null', $options->expr); | ||
} | ||
// find by identifier? | ||
} elseif (false === $object = $this->find($class, $request, $options, $name)) { | ||
// find by criteria | ||
$object = $this->findOneBy($class, $request, $options); | ||
if (false === $object) { | ||
if (false === $object = $this->findOneBy($class, $request, $options)) { | ||
if (!$argument->isNullable()) { | ||
throw new \LogicException(sprintf('Unable to guess how to get a Doctrine instance from the request information for parameter "%s".', $name)); | ||
} | ||
|
@@ -134,9 +115,9 @@ private function getManager(?string $name, string $class): ?ObjectManager | |
} | ||
} | ||
|
||
private function find(string $class, Request $request, array $options, string $name): false|object|null | ||
private function find(string $class, Request $request, MapEntity $options, string $name): false|object|null | ||
{ | ||
if ($options['mapping'] || $options['exclude']) { | ||
if ($options->mapping || $options->exclude) { | ||
return false; | ||
} | ||
|
||
|
@@ -145,8 +126,8 @@ private function find(string $class, Request $request, array $options, string $n | |
return false; | ||
} | ||
|
||
$objectManager = $this->getManager($options['object_manager'], $class); | ||
if ($options['evict_cache'] && $objectManager instanceof EntityManagerInterface) { | ||
$objectManager = $this->getManager($options->objectManager, $class); | ||
if ($options->evictCache && $objectManager instanceof EntityManagerInterface) { | ||
$cacheProvider = $objectManager->getCache(); | ||
if ($cacheProvider && $cacheProvider->containsEntity($class, $id)) { | ||
$cacheProvider->evictEntity($class, $id); | ||
|
@@ -160,11 +141,11 @@ private function find(string $class, Request $request, array $options, string $n | |
} | ||
} | ||
|
||
private function getIdentifier(Request $request, array $options, string $name): mixed | ||
private function getIdentifier(Request $request, MapEntity $options, string $name): mixed | ||
{ | ||
if (\is_array($options['id'])) { | ||
if (\is_array($options->id)) { | ||
$id = []; | ||
foreach ($options['id'] as $field) { | ||
foreach ($options->id as $field) { | ||
// Convert "%s_uuid" to "foobar_uuid" | ||
if (str_contains($field, '%s')) { | ||
$field = sprintf($field, $name); | ||
|
@@ -176,59 +157,55 @@ private function getIdentifier(Request $request, array $options, string $name): | |
return $id; | ||
} | ||
|
||
if (null !== $options['id']) { | ||
$name = $options['id']; | ||
if (null !== $options->id) { | ||
$name = $options->id; | ||
} | ||
|
||
if ($request->attributes->has($name)) { | ||
return $request->attributes->get($name); | ||
} | ||
|
||
if (!$options['id'] && $request->attributes->has('id')) { | ||
if (!$options->id && $request->attributes->has('id')) { | ||
return $request->attributes->get('id'); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function findOneBy(string $class, Request $request, array $options): false|object|null | ||
private function findOneBy(string $class, Request $request, MapEntity $options): false|object|null | ||
{ | ||
if (!$options['mapping']) { | ||
if (!$options['auto_mapping']) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for option There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch |
||
return false; | ||
} | ||
|
||
if (null === $mapping = $options->mapping) { | ||
$keys = $request->attributes->keys(); | ||
$options['mapping'] = $keys ? array_combine($keys, $keys) : []; | ||
$mapping = $keys ? array_combine($keys, $keys) : []; | ||
} | ||
|
||
foreach ($options['exclude'] as $exclude) { | ||
unset($options['mapping'][$exclude]); | ||
foreach ($options->exclude as $exclude) { | ||
unset($mapping[$exclude]); | ||
} | ||
|
||
if (!$options['mapping']) { | ||
if (!$mapping) { | ||
return false; | ||
} | ||
|
||
// if a specific id has been defined in the options and there is no corresponding attribute | ||
// return false in order to avoid a fallback to the id which might be of another object | ||
if ($options['id'] && null === $request->attributes->get($options['id'])) { | ||
if (\is_string($options->id) && null === $request->attributes->get($options->id)) { | ||
jderusse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return false; | ||
} | ||
|
||
$criteria = []; | ||
$objectManager = $this->getManager($options['object_manager'], $class); | ||
$objectManager = $this->getManager($options->objectManager, $class); | ||
$metadata = $objectManager->getClassMetadata($class); | ||
|
||
foreach ($options['mapping'] as $attribute => $field) { | ||
foreach ($mapping as $attribute => $field) { | ||
if (!$metadata->hasField($field) && (!$metadata->hasAssociation($field) || !$metadata->isSingleValuedAssociation($field))) { | ||
continue; | ||
} | ||
|
||
$criteria[$field] = $request->attributes->get($attribute); | ||
} | ||
|
||
if ($options['strip_null']) { | ||
if ($options->stripNull) { | ||
$criteria = array_filter($criteria, static fn ($value) => null !== $value); | ||
} | ||
|
||
|
@@ -243,51 +220,27 @@ private function findOneBy(string $class, Request $request, array $options): fal | |
} | ||
} | ||
|
||
private function findViaExpression(string $class, Request $request, string $expression, array $options): ?object | ||
private function findViaExpression(string $class, Request $request, string $expression, MapEntity $options): ?object | ||
{ | ||
if (null === $this->language) { | ||
if (!$this->expressionLanguage) { | ||
throw new \LogicException(sprintf('You cannot use the "%s" if the ExpressionLanguage component is not available. Try running "composer require symfony/expression-language".', __CLASS__)); | ||
} | ||
|
||
$repository = $this->getManager($options['object_manager'], $class)->getRepository($class); | ||
$repository = $this->getManager($options->objectManager, $class)->getRepository($class); | ||
$variables = array_merge($request->attributes->all(), ['repository' => $repository]); | ||
|
||
try { | ||
return $this->language->evaluate($expression, $variables); | ||
return $this->expressionLanguage->evaluate($expression, $variables); | ||
} catch (NoResultException|ConversionException) { | ||
return null; | ||
} | ||
} | ||
|
||
private function getOptions(ArgumentMetadata $argument): array | ||
private function getOptions(ArgumentMetadata $argument): MapEntity | ||
{ | ||
/** @var ?MapEntity $configuration */ | ||
$configuration = $argument->getAttributes(MapEntity::class, ArgumentMetadata::IS_INSTANCEOF)[0] ?? null; | ||
|
||
$argumentClass = $argument->getType(); | ||
if ($argumentClass && !class_exists($argumentClass)) { | ||
$argumentClass = null; | ||
} | ||
|
||
if (null === $configuration) { | ||
return array_merge($this->defaultOptions, [ | ||
'class' => $argumentClass, | ||
'has_attribute' => false, | ||
]); | ||
} | ||
/** @var MapEntity $options */ | ||
$options = $argument->getAttributes(MapEntity::class, ArgumentMetadata::IS_INSTANCEOF)[0] ?? $this->defaults; | ||
|
||
return [ | ||
'class' => $configuration->class ?? $argumentClass, | ||
'object_manager' => $configuration->objectManager ?? $this->defaultOptions['object_manager'], | ||
'expr' => $configuration->expr ?? $this->defaultOptions['expr'], | ||
'mapping' => $configuration->mapping ?? $this->defaultOptions['mapping'], | ||
'exclude' => $configuration->exclude ?? $this->defaultOptions['exclude'], | ||
'strip_null' => $configuration->stripNull ?? $this->defaultOptions['strip_null'], | ||
'id' => $configuration->id ?? $this->defaultOptions['id'], | ||
'evict_cache' => $configuration->evictCache ?? $this->defaultOptions['evict_cache'], | ||
'has_attribute' => true, | ||
'auto_mapping' => $this->defaultOptions['auto_mapping'], | ||
'attribute_only' => $this->defaultOptions['attribute_only'], | ||
]; | ||
return $options->withDefaults($this->defaults, $argument->getType()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
6.2 | ||
--- | ||
|
||
* Add `#[MapEntity]` with its corresponding `EntityArgumentResolver` | ||
|
||
6.0 | ||
--- | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.