From 0c9ba07beca76802adba7e1392a2c795d160c80d Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sat, 23 Jul 2022 15:52:02 +0200 Subject: [PATCH] Improve performance of the configurator applied to all fields --- .../Configurator/CommonPreConfigurator.php | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/Field/Configurator/CommonPreConfigurator.php b/src/Field/Configurator/CommonPreConfigurator.php index 250a397a73..4e4e05b16d 100644 --- a/src/Field/Configurator/CommonPreConfigurator.php +++ b/src/Field/Configurator/CommonPreConfigurator.php @@ -10,6 +10,7 @@ use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto; use EasyCorp\Bundle\EasyAdminBundle\Field\AvatarField; use EasyCorp\Bundle\EasyAdminBundle\Field\FormField; +use Symfony\Component\PropertyAccess\Exception\AccessException; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; use function Symfony\Component\String\u; use function Symfony\Component\Translation\t; @@ -39,8 +40,14 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c // if a field already has set a value, someone has written something to // it (as a virtual field or overwrite); don't modify the value in that case - if (null === $field->getValue()) { - $value = $this->buildValueOption($field, $entityDto); + $isReadable = true; + if (null === $value = $field->getValue()) { + try { + $value = null === $entityDto->getInstance() ? null : $this->propertyAccessor->getValue($entityDto->getInstance(), $field->getProperty()); + } catch (AccessException) { + $isReadable = false; + } + $field->setValue($value); $field->setFormattedValue($value); } @@ -57,7 +64,7 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c $isVirtual = $this->buildVirtualOption($field, $entityDto); $field->setVirtual($isVirtual); - $templatePath = $this->buildTemplatePathOption($context, $field, $entityDto); + $templatePath = $this->buildTemplatePathOption($context, $field, $entityDto, $isReadable); $field->setTemplatePath($templatePath); $doctrineMetadata = $entityDto->hasProperty($field->getProperty()) ? $entityDto->getPropertyMetadata($field->getProperty())->all() : []; @@ -80,18 +87,6 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c $field->setFormTypeOptionIfNotSet('label', $field->getLabel()); } - private function buildValueOption(FieldDto $field, EntityDto $entityDto) - { - $entityInstance = $entityDto->getInstance(); - $propertyName = $field->getProperty(); - - if (null === $entityInstance || !$this->propertyAccessor->isReadable($entityInstance, $propertyName)) { - return null; - } - - return $this->propertyAccessor->getValue($entityInstance, $propertyName); - } - private function buildHelpOption(FieldDto $field, string $translationDomain): ?TranslatableInterface { $help = $field->getHelp(); @@ -161,15 +156,14 @@ private function buildVirtualOption(FieldDto $field, EntityDto $entityDto): bool return !$entityDto->hasProperty($field->getProperty()); } - private function buildTemplatePathOption(AdminContext $adminContext, FieldDto $field, EntityDto $entityDto): string + private function buildTemplatePathOption(AdminContext $adminContext, FieldDto $field, EntityDto $entityDto, bool $isReadable): string { if (null !== $templatePath = $field->getTemplatePath()) { return $templatePath; } // if field has a value set, don't display it as inaccessible (needed e.g. for virtual fields) - $isPropertyReadable = null !== $entityDto->getInstance() && $this->propertyAccessor->isReadable($entityDto->getInstance(), $field->getProperty()); - if (!$isPropertyReadable && null === $field->getValue()) { + if (!$isReadable && null === $field->getValue()) { return $adminContext->getTemplatePath('label/inaccessible'); }