Skip to content

Commit 83340c3

Browse files
committed
bug #2094 [Autocomplete] Reset form state on each request for applications that reuse the Symfony application between requests (dotdevio)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Autocomplete] Reset form state on each request for applications that reuse the Symfony application between requests | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Issues | | License | MIT Hi there, in ux-autocomplete there is an WrappedEntityTypeAutocompleter::getForm which on first call saves form instance in WrappedEntityTypeAutocompleter $form property, it has method setOptions which is called on each request in EntityAutocompleteController, in PHP-FPM everything will work as expected where container is created on each request, but with Swoole or similar where container is not created on each request rather only first request on first EntityAutocompleteController request everything is also fine, but on second request WrappedEntityTypeAutocompleter will have an instance of form from previous request which is fine but the call of setOptions in controller will throw exception. So in Swoole on second and next requests $this->form already contains an instance of form from previous request because DI Container is not created on each request as in PHP-FPM (Container is created only once) and exception is thrown. So, Symfony have an kernel.reset / kernel.terminate event which probably can be used here to reset $this->form in WrappedEntityTypeAutocompleter because they are fired after request is completed. Commits ------- e5e9b90 [Autocomplete] Reset form state on each request for applications that reuse the Symfony application between requests
2 parents cedf9c8 + e5e9b90 commit 83340c3

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/Autocomplete/src/DependencyInjection/AutocompleteFormTypePass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ private function processEntityAutocompleteFieldTag(ContainerBuilder $container)
4747
$wrappedDefinition = (new ChildDefinition('ux.autocomplete.wrapped_entity_type_autocompleter'))
4848
// the "formType" string
4949
->replaceArgument(0, $serviceDefinition->getClass())
50-
->addTag(self::ENTITY_AUTOCOMPLETER_TAG, ['alias' => $alias]);
50+
->addTag(self::ENTITY_AUTOCOMPLETER_TAG, ['alias' => $alias])
51+
->addTag('kernel.reset', ['method' => 'reset']);
5152
$container->setDefinition('ux.autocomplete.wrapped_entity_type_autocompleter.'.$alias, $wrappedDefinition);
5253
}
5354
}

src/Autocomplete/src/Form/WrappedEntityTypeAutocompleter.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Form\FormInterface;
2020
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
2121
use Symfony\Component\PropertyAccess\PropertyPathInterface;
22+
use Symfony\Contracts\Service\ResetInterface;
2223
use Symfony\UX\Autocomplete\Doctrine\EntityMetadata;
2324
use Symfony\UX\Autocomplete\Doctrine\EntityMetadataFactory;
2425
use Symfony\UX\Autocomplete\Doctrine\EntitySearchUtil;
@@ -29,7 +30,7 @@
2930
*
3031
* @internal
3132
*/
32-
final class WrappedEntityTypeAutocompleter implements OptionsAwareEntityAutocompleterInterface
33+
final class WrappedEntityTypeAutocompleter implements OptionsAwareEntityAutocompleterInterface, ResetInterface
3334
{
3435
private ?FormInterface $form = null;
3536
private ?EntityMetadata $entityMetadata = null;
@@ -188,4 +189,10 @@ public function setOptions(array $options): void
188189

189190
$this->options = $options;
190191
}
192+
193+
public function reset(): void
194+
{
195+
unset($this->form);
196+
$this->form = null;
197+
}
191198
}

0 commit comments

Comments
 (0)