Skip to content

Commit

Permalink
minor #5343 Improve performance of the configurator applied to all fi…
Browse files Browse the repository at this point in the history
…elds (javiereguiluz)

This PR was merged into the 4.x branch.

Discussion
----------

Improve performance of the configurator applied to all fields

Fixes #5211.

At first I tried to add a local cache in this class, but performance was much worse. So, I refactored code a bit to not call PropertyAccess methods repeatedly.

`@oleg`-andreyev it'd be great if you could test these changes in your application. Thanks!

Commits
-------

0c9ba07 Improve performance of the configurator applied to all fields
  • Loading branch information
javiereguiluz committed Sep 20, 2022
2 parents f8ed33d + 0c9ba07 commit 89deeae
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions src/Field/Configurator/CommonPreConfigurator.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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() : [];
Expand 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();
Expand Down Expand Up @@ -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');
}

Expand Down

0 comments on commit 89deeae

Please sign in to comment.