From 7a7612a28c833a2e067cf53a0997c61dd091093b Mon Sep 17 00:00:00 2001 From: DubbleClick Date: Sun, 29 Oct 2023 13:52:02 +0100 Subject: [PATCH] fix EventArgs::getEntityManger deprecation (#2639) * use EventArgs::getObjectManager (fix DoctrineBundle 2.11 deprecation) * add deprecation messages to test * fix deprecation error type * add changelog note * clear up deprecation messages and add instructions for the next major release * re-add type to depreaction message --- CHANGELOG.md | 6 +++- src/Mapping/Event/Adapter/ORM.php | 30 ++++++++++++++++++ .../Event/UploadableBaseEventArgs.php | 31 +++++++++++++++++++ .../Gedmo/Mapping/MappingEventAdapterTest.php | 4 +-- 4 files changed, 68 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9110f7fefd..7756160801 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,11 @@ a release. ## [Unreleased] -## [3.13.0] +### Deprecated +- Calling `Gedmo\Mapping\Event\Adapter\ORM::getObjectManager()` and `getObject()` on EventArgs that do not implement `getObjectManager()` and `getObject()` (such as old EventArgs implementing `getEntityManager()` and `getEntity()`) +- Calling `Gedmo\Uploadable\Event\UploadableBaseEventArgs::getEntityManager()` and `getEntity()`. Call `getObjectManager()` and `getObject()` instead. + +## [3.13.0] - 2023-09-06 ### Fixed - References: fixed condition in `XML` Driver that did not allow to retrieve from the entity definition the `mappedBy` and `inversedBy` fields. - Fix bug collecting metadata for inherited mapped classes diff --git a/src/Mapping/Event/Adapter/ORM.php b/src/Mapping/Event/Adapter/ORM.php index 84d4c84fcb..7b707c009f 100644 --- a/src/Mapping/Event/Adapter/ORM.php +++ b/src/Mapping/Event/Adapter/ORM.php @@ -95,6 +95,21 @@ public function getObjectManager() throw new \LogicException(sprintf('Event args must be set before calling "%s()".', __METHOD__)); } + // todo: for the next major release, uncomment the next line: + // return $this->args->getObjectManager(); + // and remove anything past this + if (\method_exists($this->args, 'getObjectManager')) { + return $this->args->getObjectManager(); + } + + @trigger_error(sprintf( + 'Calling "%s()" on event args of class "%s" that does not implement "getObjectManager()" is deprecated since gedmo/doctrine-extensions 3.x' + .' and will throw a "%s" error in version 4.0.', + __METHOD__, + get_class($this->args), + \Error::class + ), E_USER_DEPRECATED); + return $this->args->getEntityManager(); } @@ -104,6 +119,21 @@ public function getObject(): object throw new \LogicException(sprintf('Event args must be set before calling "%s()".', __METHOD__)); } + // todo: for the next major release, uncomment the next line: + // return $this->args->getObject(); + // and remove anything past this + if (\method_exists($this->args, 'getObject')) { + return $this->args->getObject(); + } + + @trigger_error(sprintf( + 'Calling "%s()" on event args of class "%s" that does not imeplement "getObject()" is deprecated since gedmo/doctrine-extensions 3.x' + .' and will throw a "%s" error in version 4.0.', + __METHOD__, + get_class($this->args), + \Error::class + ), E_USER_DEPRECATED); + return $this->args->getEntity(); } diff --git a/src/Uploadable/Event/UploadableBaseEventArgs.php b/src/Uploadable/Event/UploadableBaseEventArgs.php index b4f9e1b10f..cd16da3761 100644 --- a/src/Uploadable/Event/UploadableBaseEventArgs.php +++ b/src/Uploadable/Event/UploadableBaseEventArgs.php @@ -11,6 +11,7 @@ use Doctrine\Common\EventArgs; use Doctrine\ORM\EntityManagerInterface; +use Doctrine\Persistence\ObjectManager; use Gedmo\Uploadable\FileInfo\FileInfoInterface; use Gedmo\Uploadable\UploadableListener; @@ -100,6 +101,21 @@ public function getListener() * @return EntityManagerInterface */ public function getEntityManager() + { + @trigger_error(sprintf( + '"%s()" is deprecated since gedmo/doctrine-extensions 3.x and will be removed in version 4.0.', + __METHOD__ + ), E_USER_DEPRECATED); + + return $this->em; + } + + /** + * Retrieve associated EntityManager + * + * @return ObjectManager + */ + public function getObjectManager() { return $this->em; } @@ -110,6 +126,21 @@ public function getEntityManager() * @return object */ public function getEntity() + { + @trigger_error(sprintf( + '"%s()" is deprecated since gedmo/doctrine-extensions 3.x and will be removed in version 4.0.', + __METHOD__ + ), E_USER_DEPRECATED); + + return $this->entity; + } + + /** + * Retrieve associated Object + * + * @return object + */ + public function getObject() { return $this->entity; } diff --git a/tests/Gedmo/Mapping/MappingEventAdapterTest.php b/tests/Gedmo/Mapping/MappingEventAdapterTest.php index 2ee3665eae..3ea084d924 100644 --- a/tests/Gedmo/Mapping/MappingEventAdapterTest.php +++ b/tests/Gedmo/Mapping/MappingEventAdapterTest.php @@ -53,10 +53,10 @@ public function testAdapterBehavior(): void ->disableOriginalConstructor() ->getMock(); $eventArgsMock->expects(static::once()) - ->method('getEntityManager'); + ->method('getObjectManager'); $eventArgsMock->expects(static::once()) - ->method('getEntity') + ->method('getObject') ->willReturn(new \stdClass()); $eventAdapter = new EventAdapterORM();