Skip to content

[FrameworkBundle] Autoconfigure ORM attributes only if not done by DoctrineBundle #60001

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

Closed

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Mar 18, 2025

Q A
Branch? 7.3
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix doctrine/DoctrineBundle#1868 (comment)
License MIT

An attribute cannot have multiple autoconfiguration callbacks registered. By excluding classes with the #[Entity] and other ORM attributes in both FrameworkBundle (#59987) and DoctrineBundle (doctrine/DoctrineBundle#1868), the following exception is thrown:

Symfony\Component\DependencyInjection\Exception\InvalidArgumentException: "Doctrine\ORM\Mapping\Embeddable" has already been autoconfigured and merge() does not support merging autoconfiguration for the same attribute.

});

// DoctrineBundle autoconfigures attributes since version 2.14.0
if (!array_key_exists(Entity::class, $container->getAutoconfiguredAttributes())) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is resilient if the DoctrineBundle is registered before FrameworkBundle. We can check the package version instead.

Suggested change
if (!array_key_exists(Entity::class, $container->getAutoconfiguredAttributes())) {
if (InstalledVersions::isInstalled('doctrine/doctrine-bundle') && version_compare(InstalledVersions::getVersion('doctrine/doctrine-bundle'), '2.14.0', '<')) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not resilient at all irrespective of the order, because DI extensions receive a separate container builder (which is then merged into the main one), precisely to reduce the cases of writing logic that depends on the order (which is not guaranteed at all by Flex)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the composer version check better?

@@ -21,7 +21,7 @@
"ext-xml": "*",
"symfony/cache": "^6.4|^7.0",
"symfony/config": "^7.3",
"symfony/dependency-injection": "^7.2",
"symfony/dependency-injection": "^7.3",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addExcludeTag was added in version 7.3 by #59704

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternative to #59999

@nicolas-grekas
Copy link
Member

merge() does not support merging autoconfiguration for the same attribute.

maybe we can improve this instead?
merging should be just about creating a closure that calls all autoconfiguration closures in chain, don't you think?

If we keep the current approach, we should use a feature test if possible.

@GromNaN
Copy link
Member Author

GromNaN commented Mar 20, 2025

merging should be just about creating a closure that calls all autoconfiguration closures in chain, don't you think?

We use reflection on the closure. Creating a closure programmatically with a different list of arguments would be very complex.

foreach ($container->getAutoconfiguredAttributes() as $attributeName => $callable) {
$callableReflector = new \ReflectionFunction($callable(...));
if ($callableReflector->getNumberOfParameters() <= 2) {
$this->classAttributeConfigurators[$attributeName] = $callable;

Instead, ContainerBuilder::getAutoconfiguredAttributes() could return a list of closures for each attribute. But that would be a BC break.

This issue was anticipated when the feature was introduced: #39897 (comment)

@GromNaN
Copy link
Member Author

GromNaN commented Mar 20, 2025

New feature in a new PR: #60011

@GromNaN GromNaN closed this Mar 20, 2025
@GromNaN GromNaN deleted the conditional-doctrine-exclude-attributes branch March 20, 2025 14:11
nicolas-grekas added a commit that referenced this pull request Mar 21, 2025
…nfiguration callbacks on the same class (GromNaN)

This PR was squashed before being merged into the 7.3 branch.

Discussion
----------

[DependencyInjection] Enable multiple attribute autoconfiguration callbacks on the same class

| Q             | A
| ------------- | ---
| Branch?       | 7.3
| Bug fix?      | yes
| New feature?  | yes
| Deprecations? | yes
| Issues        | Fix doctrine/DoctrineBundle#1868 (comment)
| License       | MIT

Replace #60001

By having a list of callables for each attributes, we can enable merging definitions each having an autoconfiguration for the same attribute class. This is the case with the `#[Entity]` attribute in DoctrineBundle and FrameworkBundle.

I have to deprecate `ContainerBuilder::getAutoconfiguredAttributes()` as its return type is `array<class-string, callable>`; so I added a new method `AttributeAutoconfigurationPass` that returns `array<class-string, callable[]>` in  in order to use reflection on each callable in the compiler pass.

Commits
-------

e36fe60 [DependencyInjection] Enable multiple attribute autoconfiguration callbacks on the same class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants