Skip to content

[Translation] Extract constraint class names instead of relying on service declaration #49785

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
wants to merge 2 commits into from
Closed

[Translation] Extract constraint class names instead of relying on service declaration #49785

wants to merge 2 commits into from

Conversation

nfragnet
Copy link

@nfragnet nfragnet commented Mar 23, 2023

Q A
Branch? 6.2
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #49397
License MIT
Doc PR

This is an alternative solution to this one involving a fix translator side.
See it as a discussion support first, more than a definitive proposal.

Instead of relying on service declaration to inject the constraint names to the ConstraintVisitor, we extract them from files and return all class names of classes extending Constraint.

The private method looks like the one we can find in the DebugCommand and I'm not really sure it has its place directly in the TranslatorPass but this is a first draft and I hope someone can lead me in the right direction to move stuff at the right place.

Contrary to my first PR this solution excludes userland constraints. It also exclude constraints from other places than Symfony/Component/Validator/Constraints directory (like UniqueEntity for example).

To include them all, we would probably need to:

  • add the service declaration of external constraint like UniqueEntity
  • keep the old way to get all tagged services
  • merge the result with the one provided in this PR

Userland constraint would be included at the condition of being declared as services.

Copy link
Member

@welcoMattic welcoMattic left a comment

Choose a reason for hiding this comment

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

This approach is better than the first one, thank you @nfragnet

Co-authored-by: Mathieu Santostefano <msantostefano@protonmail.com>
@nfragnet
Copy link
Author

Regarding the tests: any idea how to get rid of deprecation notices making tests fail?
I'm not even using the class explicitly, just reading the folder.


Regarding the private method: should it belong elsewhere or do we keep it as it is?


Following the discussion here, question asked by @MatTheCat

But would it be possible to still consider it in TranslatorPass to take userland validators into account?

We can either:

  1. ignore them (for now) -> PR in current state
  2. include them at the condition they are declared & tagged
    That would mean rollback the removed part and merge both arrays (class names of tagged services + class names of Constraints folder)
  3. add a config parameter for user to provide userland constraint folders, inject in translator pass -> overkill & ugly IMO + won't fit in 6.2
  4. ...

Any other suggestion is welcome.

@welcoMattic welcoMattic changed the title Extract constraint class names instead of relying on service declaration [Translation] Extract constraint class names instead of relying on service declaration Mar 24, 2023
@nicolas-grekas
Copy link
Member

I have another proposal in #49850. It would need some adjustments to work for 5.4 + some tests. Would you be up to have a look and borrow from there?

@nicolas-grekas
Copy link
Member

Closing in favor of #49850, thanks for raising this!

nicolas-grekas added a commit that referenced this pull request Mar 30, 2023
…ts (nicolas-grekas)

This PR was merged into the 6.2 branch.

Discussion
----------

[FrameworkBundle] Fix auto-discovering validator constraints

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | #49397
| License       | MIT
| Doc PR        | -

Alternative to #49785

Note that this kind of container-assisted auto-discovery of non-service classes looks like an interesting pattern for many other subsystems. /cc `@symfony`/mergers `@mtarld`

Commits
-------

f60218c [FrameworkBundle] Fix auto-discovering validator constraints
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.

4 participants