From f3373e5a55af5faff0df361c41606f10acad9531 Mon Sep 17 00:00:00 2001 From: Tomas Date: Thu, 3 Oct 2024 13:01:35 +0300 Subject: [PATCH] Support Doctrine ORM v3 --- .github/workflows/ci.yml | 8 ++++---- composer.json | 2 +- phpstan-baseline.neon | 15 +++++++++++++++ src/Mapping/Driver/AnnotationDriver.php | 14 ++++++++++++-- src/Mapping/Driver/AttributeDriver.php | 14 ++++++++++++-- 5 files changed, 44 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 75654188..aaf8f0b7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -63,7 +63,7 @@ jobs: matrix: dependencies: ['highest'] php: [ '7.4', '8.0', '8.1', '8.2' ] - sf_version: [ '4.4.*', '5.4.*', '6.4.*', '7.0.*' ] + sf_version: [ '4.4.*', '5.4.*', '6.4.*', '7.1.*' ] # include: # - php: '7.4' # sf_version: '4.4.*' @@ -79,11 +79,11 @@ jobs: - php: '8.0' sf_version: '6.4.*' - php: '7.4' - sf_version: '7.0.*' + sf_version: '7.1.*' - php: '8.0' - sf_version: '7.0.*' + sf_version: '7.1.*' - php: '8.1' - sf_version: '7.0.*' + sf_version: '7.1.*' steps: - name: "Checkout code" uses: actions/checkout@v4 diff --git a/composer.json b/composer.json index 830303a9..d5bc1814 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "require-dev": { "doctrine/annotations": "^1.11.1 || ^2.0", "doctrine/doctrine-bundle": "^2.3", - "doctrine/orm": "^2.8", + "doctrine/orm": "^2.8 || ^3.0", "fakerphp/faker": "^1.20", "friendsofphp/php-cs-fixer": "^3.0", "geocoder-php/algolia-places-provider": "^0.4", diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 864b6503..4d7a09d4 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -125,6 +125,11 @@ parameters: count: 1 path: src/DependencyInjection/Configuration.php + - + message: "#^Call to an undefined method Doctrine\\\\ORM\\\\Event\\\\OnFlushEventArgs\\:\\:getEntityManager\\(\\)\\.$#" + count: 1 + path: src/Doctrine/ORM/GeocoderListener.php + - message: "#^Cannot call method getLatitude\\(\\) on Geocoder\\\\Model\\\\Coordinates\\|null\\.$#" count: 1 @@ -135,6 +140,16 @@ parameters: count: 1 path: src/Doctrine/ORM/GeocoderListener.php + - + message: "#^Method Bazinga\\\\GeocoderBundle\\\\Mapping\\\\Driver\\\\AnnotationDriver\\:\\:getReflection\\(\\) return type with generic class ReflectionClass does not specify its types\\: T$#" + count: 1 + path: src/Mapping/Driver/AnnotationDriver.php + + - + message: "#^Method Bazinga\\\\GeocoderBundle\\\\Mapping\\\\Driver\\\\AttributeDriver\\:\\:getReflection\\(\\) return type with generic class ReflectionClass does not specify its types\\: T$#" + count: 1 + path: src/Mapping/Driver/AttributeDriver.php + - message: "#^Parameter \\#1 \\$text of method Geocoder\\\\Query\\\\GeocodeQuery\\:\\:withText\\(\\) expects string, string\\|null given\\.$#" count: 1 diff --git a/src/Mapping/Driver/AnnotationDriver.php b/src/Mapping/Driver/AnnotationDriver.php index e2ae8498..71089902 100644 --- a/src/Mapping/Driver/AnnotationDriver.php +++ b/src/Mapping/Driver/AnnotationDriver.php @@ -17,6 +17,7 @@ use Bazinga\GeocoderBundle\Mapping\Exception; use Doctrine\Common\Annotations\Reader; use Doctrine\Common\Util\ClassUtils; +use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver; /** * @author Markus Bachmann @@ -32,14 +33,14 @@ public function __construct(Reader $reader) public function isGeocodeable($object): bool { - $reflection = ClassUtils::newReflectionObject($object); + $reflection = self::getReflection($object); return (bool) $this->reader->getClassAnnotation($reflection, Annotations\Geocodeable::class); } public function loadMetadataFromObject($object) { - $reflection = ClassUtils::newReflectionObject($object); + $reflection = self::getReflection($object); if (!$annotation = $this->reader->getClassAnnotation($reflection, Annotations\Geocodeable::class)) { throw new Exception\MappingException(sprintf('The class %s is not geocodeable', get_class($object))); @@ -74,4 +75,13 @@ public function loadMetadataFromObject($object) return $metadata; } + + private static function getReflection(object $object): \ReflectionClass + { + if (class_exists(ClassUtils::class)) { + return ClassUtils::newReflectionObject($object); + } + + return new \ReflectionClass(DefaultProxyClassNameResolver::getClass($object)); + } } diff --git a/src/Mapping/Driver/AttributeDriver.php b/src/Mapping/Driver/AttributeDriver.php index 2b1cbaaf..703b0efb 100644 --- a/src/Mapping/Driver/AttributeDriver.php +++ b/src/Mapping/Driver/AttributeDriver.php @@ -16,6 +16,7 @@ use Bazinga\GeocoderBundle\Mapping\ClassMetadata; use Bazinga\GeocoderBundle\Mapping\Exception\MappingException; use Doctrine\Common\Util\ClassUtils; +use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver; /** * @author Pierre du Plessis @@ -28,7 +29,7 @@ public function isGeocodeable($object): bool return false; } - $reflection = ClassUtils::newReflectionObject($object); + $reflection = self::getReflection($object); return count($reflection->getAttributes(Annotations\Geocodeable::class)) > 0; } @@ -42,7 +43,7 @@ public function loadMetadataFromObject($object): ClassMetadata throw new MappingException(sprintf('The class %s is not geocodeable', get_class($object))); } - $reflection = ClassUtils::newReflectionObject($object); + $reflection = self::getReflection($object); $attributes = $reflection->getAttributes(Annotations\Geocodeable::class); @@ -79,4 +80,13 @@ public function loadMetadataFromObject($object): ClassMetadata return $metadata; } + + private static function getReflection(object $object): \ReflectionClass + { + if (class_exists(ClassUtils::class)) { + return ClassUtils::newReflectionObject($object); + } + + return new \ReflectionClass(DefaultProxyClassNameResolver::getClass($object)); + } }