Skip to content
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

Add PHP 8.0 Attribute mapping support #2344

Merged
merged 7 commits into from
Aug 11, 2021

Conversation

IonBazan
Copy link
Member

@IonBazan IonBazan commented Jul 23, 2021

Hi guys, this is my first contribution here 👾

Q A
Type feature
BC Break minor, due to @NamedArgumentConstructor - see UPGRADE-2.3.md
Fixed issues #2337
Conflicts with #2243 (some changes are duplicated there but we need ti check for conflicts or differences)

Summary

This PR adds AttributeDriver to allow mapping documents using PHP 8 Attributes to align with ORM functionality.
The driver is actually same as the AnnotationDriver and using custom Reader.

I noticed some annotations are not documented in the Annotations Reference page:

Progress

  • Add the Driver
  • Annotate all Annotations/Attributes with @NamedArgumentConstructor and add constructors
  • Test the mapping against existing drivers
  • Mention the new driver in the documentation
  • Review Annotations that are not present in the Annotation Reference

Notes

@IonBazan IonBazan changed the title Add AttributeDriver Add PHP 8.0 Attribute mapping support Jul 23, 2021
@alcaeus
Copy link
Member

alcaeus commented Jul 23, 2021

Thank you for the work done so far! @beberlei could I ask you to help out with reviewing this since you made the same change in ORM?

I'm a little strapped for time right now, but we'll try to give feedback as quick as possible. We definitely want this feature in ODM, so thank you again for getting this started!

@alcaeus alcaeus requested a review from malarzm July 23, 2021 07:54
Copy link
Member

@beberlei beberlei left a comment

Choose a reason for hiding this comment

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

I see this extends the AnnoationDriver with no extra changes. This will not work or you must require PHP 8.1, because nested attributes are not supported in 8.0.

I am not super familiar with the ODM, one example i saw #[Indexes] nesting index attributes.

What we did in ORM is copy adapted the Attribute driver and flattened the attribute hierachy

@IonBazan
Copy link
Member Author

@beberlei You are right but the #[Indexes] and #[Document(indexes: ...)] are the only places we use nested annotations and both are deprecated. That's why I'd suggest to keep it simple and keep the attribute hierarchy flat in the future. I saw the ORM implementation where mapping relies on nested annotations but since we are dropping existing nested annotations soon, I don't think it's worth implementing that in AttributeDriver.

@beberlei
Copy link
Member

That makes sense then, i never used the ODM before and am clueless about use of nested annotations there, just wanted to make sure that its adressed

@IonBazan IonBazan requested review from franmomu and beberlei July 29, 2021 16:05
@IonBazan IonBazan force-pushed the attribute-driver branch 3 times, most recently from cc69843 to 0c9b20a Compare July 29, 2021 16:21
@IonBazan
Copy link
Member Author

OK I revisited all annotations and compared with changes from #2243 - there were a few places where I missed the default values or properties that shouldn't be set in constructor.

The only problematic one is Indexes which I can't make work with NamedArgumentConstructor because $value can be an array or a single item and can be constructed either by passing an array of indexes or a list of arguments like:
@Indexes(@Index, @Index, @Index so I left it intact as it's deprecated anyway.

@alcaeus alcaeus self-requested a review August 4, 2021 20:59
@alcaeus
Copy link
Member

alcaeus commented Aug 5, 2021

I think we're fine not supporting #[Indexes] until initialisers are done. In the meantime, people have other ways to map indexes using attributes. Let's just make sure to call this out in our docs so that people know they can't just change annotation syntax into an attribute syntax without making some minor tweaks.

Copy link
Member

@alcaeus alcaeus left a comment

Choose a reason for hiding this comment

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

Gave this a review, but I'd still appreciate someone else taking a look. Great work @IonBazan!

@malarzm
Copy link
Member

malarzm commented Aug 5, 2021

Just wanted to let you know I'll get to this PR somewhen next week. I'm a bit swamped at work before going on vacation ;)

/**
* The AnnotationDriver reads the mapping metadata from docblock annotations.
*/
class AttributeDriver extends AnnotationDriver
Copy link
Contributor

Choose a reason for hiding this comment

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

I was trying the branch and I had to add the constructor like in https://github.com/doctrine/orm/blob/106ed8009a0c581a1566775cbd4d2c4cc93976b1/lib/Doctrine/ORM/Mapping/Driver/AttributeDriver.php#L33-L39

Looks like the create method was introduced for testing purposes in 5d783c3

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've added the constructor but it seems that it doesn't make much sense here. I think we should use the one from parent class.

Copy link
Contributor

Choose a reason for hiding this comment

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

The thing here is that using the bundle it creates the service definition for the driver here I think:

https://github.com/symfony/symfony/blob/83bdd2eacf593ec8ab4511afccba75bcaf92ef66/src/Symfony/Bridge/Doctrine/DependencyInjection/AbstractDoctrineExtension.php#L210-L212

And it only receives the array of paths.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for clarification - in that case, the current constructor should work with that. I will try adding the integration to MongoDBBundle just to make sure it works.

Copy link
Member

Choose a reason for hiding this comment

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

I will try adding the integration to MongoDBBundle just to make sure it works.

For prior art, you can check out the same change in DoctrineBundle: doctrine/DoctrineBundle#1322. Changes should be pretty much the same, since the bundles share the same base architecture.

Copy link

@Ahummeling Ahummeling left a comment

Choose a reason for hiding this comment

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

So I feel like I've already gone into too much technical detail already, feel free to ignore / resolve my comments at will. This is my first code review I've done outside of my own company. If my comments seem strange or out of place, please let me know, so I can work on it.

@IonBazan IonBazan force-pushed the attribute-driver branch 3 times, most recently from d380185 to 38c948f Compare August 6, 2021 02:52
Copy link
Member

@alcaeus alcaeus left a comment

Choose a reason for hiding this comment

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

Changes LGTM, only a question about the handling of the @Indexes annotation which seems to have changed slightly.

lib/Doctrine/ODM/MongoDB/Types/DateImmutableType.php Outdated Show resolved Hide resolved
@alcaeus
Copy link
Member

alcaeus commented Aug 10, 2021

@IonBazan the test failure on a sharded cluster seems unrelated - this might be a case of us not using the correct write concern for those tests on a sharded cluster. I'll investigate separately but have kicked the build to see if it passes now.

Copy link
Member

@alcaeus alcaeus left a comment

Choose a reason for hiding this comment

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

LGTM. Great work @IonBazan, this is very much appreciated! 💚

@alcaeus alcaeus removed the request for review from beberlei August 10, 2021 07:43
@alcaeus
Copy link
Member

alcaeus commented Aug 10, 2021

@franmomu @malarzm happy to merge this if one of you gives it another seal of approval :)

Copy link
Contributor

@franmomu franmomu left a comment

Choose a reason for hiding this comment

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

Awesome work! thank you! 🚀

@malarzm
Copy link
Member

malarzm commented Aug 10, 2021

@IonBazan one last thing: could you please rebase your branch atop current 2.3.x? I'd like to al least get rid of merge commits from the history, also there were commits for CI that are no longer needed. Ideally the commit history should make sense on its own, but probably it's too late now so we may squash everything together :)

@IonBazan
Copy link
Member Author

@malarzm I managed to squash and edit some of the commits to get rid the ones that have been reverted so the history makes more sense now. I kept co-author info where @franmomu helped. Let me know if it needs further squashing.

@alcaeus alcaeus self-assigned this Aug 11, 2021
@alcaeus alcaeus merged commit 34a3cf8 into doctrine:2.3.x Aug 11, 2021
@IonBazan IonBazan deleted the attribute-driver branch August 11, 2021 06:32
@alcaeus
Copy link
Member

alcaeus commented Aug 11, 2021

Thanks @IonBazan! Great work here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PHP 8 attributes
6 participants