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

Decouple AttributeDriver from AnnotationDriver #2502

Merged
merged 2 commits into from
Dec 31, 2023

Conversation

franmomu
Copy link
Contributor

@franmomu franmomu commented Feb 5, 2023

Q A
Type improvement
BC Break yes
Fixed issues

Summary

This is a start so we can discuss what to do to decouple AttributeDriver from annotations, there are some BC breaks involved, so we need to see if this should be done in 2.5 or wait for 3.0.

The first one is that AttributeDriver no longer extends AnnotationDriver, I think this one is fine to do it in 2.5 (this is what this PR does at the moment).

But if we want to totally remove the dependency from AttributeDriver we should probably modify the constructor to remove the ?Reader $reader = null parameter, that would mean that the $reader property would always be of type AttributeReader (which would be also modified to not implement the Reader interface).

So, should we go all the way in? or postpone it for 3.0?

UPGRADE-2.5.md Outdated Show resolved Hide resolved
@IonBazan IonBazan changed the base branch from 2.5.x to 2.6.x March 3, 2023 03:33
@IonBazan IonBazan added this to the 2.6.0 milestone Mar 3, 2023
@alcaeus alcaeus modified the milestones: 2.6.0, 2.7.0 Nov 2, 2023
@franmomu franmomu changed the base branch from 2.6.x to 2.7.x December 5, 2023 22:28
@franmomu franmomu marked this pull request as ready for review December 5, 2023 23:03
@franmomu franmomu force-pushed the decouple_attribute_reader branch 5 times, most recently from 642e80c to 94e8a96 Compare December 8, 2023 11:02
@franmomu franmomu mentioned this pull request Dec 8, 2023
@franmomu
Copy link
Contributor Author

franmomu commented Dec 8, 2023

I've created #2596 on top of this PR to test the attribute driver

);
}

$this->reader = $reader ?? new AttributeReader();
Copy link
Member

Choose a reason for hiding this comment

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

Shall we have a type check on $reader being an AttributeReader?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

what do you mean? the constructor is receiving a ?Reader (which is deprecated), if no argument is passed it will be an AttributeReader.

Copy link
Member

Choose a reason for hiding this comment

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

I was thinking about throwing an exception in case somebody would pass a AnnotationReader by accident :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ahm, the thing is that passing any Reader (including AnnotationReader) would work, I haven't changed that to maintain BC

Copy link
Member

Choose a reason for hiding this comment

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

But wouldn't the driver do nothing with anything else than an AttributeReader? Or am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added these methods: https://github.com/doctrine/mongodb-odm/pull/2502/files#diff-ac3d9c518c39d96ef76766a33e63f5115a72783ae78bc99bb4b70405a04e718eR389-R416:

    /** @return object[] */
    private function getClassAttributes(ReflectionClass $class): array
    {
        if ($this->reader instanceof AttributeReader) {
            return $this->reader->getClassAttributes($class);
        }

        return $this->reader->getClassAnnotations($class);
    }

    /** @return object[] */
    private function getMethodAttributes(ReflectionMethod $method): array
    {
        if ($this->reader instanceof AttributeReader) {
            return $this->reader->getMethodAttributes($method);
        }

        return $this->reader->getMethodAnnotations($method);
    }

    /** @return object[] */
    private function getPropertyAttributes(ReflectionProperty $property): array
    {
        if ($this->reader instanceof AttributeReader) {
            return $this->reader->getPropertyAttributes($property);
        }

        return $this->reader->getPropertyAnnotations($property);
    }

to maintain BC in case someone is passing a Reader object

return true;
}

public function loadMetadataForClass($className, \Doctrine\Persistence\Mapping\ClassMetadata $metadata): void
Copy link
Member

Choose a reason for hiding this comment

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

I think it's rather unfortunate to have the code duplicated :/ Is there any other option?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I know 😞, what I was thinking is after we do this, we can make doctrine/annotations optional and then we can also deprecate AnnotationDriver.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we could have a trait with shared code? Or would it be possible to have AnnotationDriver extend AttributeDriver? My biggest concern is that we're far from removing AnnotationDriver as we've never talked 3.0

Copy link
Member

Choose a reason for hiding this comment

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

Only other option I can think of is to reverse the inheritance and have AnnotationDriver extend AttributeDriver, which again would remove the code duplication.

While we've never talked about ORM 3.0, I don't think it should be a "big thing" again. Instead, it should be released at the same time as a minor release, dropping all deprecated paths from that minor release.

Copy link
Member

Choose a reason for hiding this comment

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

While we've never talked about ORM 3.0, I don't think it should be a "big thing" again. Instead, it should be released at the same time as a minor release, dropping all deprecated paths from that minor release.

I'd be fine with this approach, it works well for Symfony 👍

Copy link
Contributor Author

@franmomu franmomu Dec 18, 2023

Choose a reason for hiding this comment

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

Done, but something else came up #2502 (comment)

@@ -173,7 +176,9 @@ public static function provideClassCanBeMappedByOneAbstractDocument(): ?Generato
* @ODM\Document()
* @ODM\EmbeddedDocument
*/
new class () {
new #[ODM\Document]
#[ODM\EmbeddedDocument]
Copy link
Member

Choose a reason for hiding this comment

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

Oh boy this is ugly 🙈 Not the point of this PR obviously, just pointing out ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it is! I've aligned the attributes, not sure if there is something else we can do

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure either :D but most probably it's not worth discussing in CS repo and if we can have it at least aligned it's OK :)


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

Choose a reason for hiding this comment

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

This could also be BC break, that class exists to have compatibility with doctrine/persistence 2 and 3

We can drop support of version 2 or leave it like this (AnnotationDriver would stop extending Doctrine\Persistence\Mapping\Driver\AnnotationDriver)

Copy link
Member

Choose a reason for hiding this comment

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

We've decided in doctrine/DoctrineMongoDBBundle#824 to drop support for doctrine/persistence v2 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's deal with that in #2603

@franmomu franmomu merged commit 495b583 into doctrine:2.7.x Dec 31, 2023
16 checks passed
@franmomu franmomu deleted the decouple_attribute_reader branch December 31, 2023 12:21
@franmomu
Copy link
Contributor Author

thanks for the reviews!

@malarzm
Copy link
Member

malarzm commented Jan 1, 2024

Sorry I didn't manage to get back in time. Anyway, happy new year folks!

alcaeus added a commit that referenced this pull request Mar 6, 2024
* 2.7.x:
  Bump ramsey/composer-install from 2 to 3
  Bump actions/upload-artifact from 3 to 4 (#2601)
  Bump actions/cache from 3 to 4 (#2609)
  Transaction support (#2586)
  make doctrine/annotations dependency optional
  Decouple AttributeDriver from AnnotationDriver (#2502)
  Require doctrine/persistence 3 (#2603)
  Bump doctrine/.github from 3.1.0 to 4.0.0
  Remove BC break issue type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants