diff --git a/CHANGELOG.md b/CHANGELOG.md index c7f3121163..cdef5b5f91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,22 +18,23 @@ a release. --- ## [Unreleased] +## Added +- SoftDeleteable: `Gedmo\SoftDeleteable\Mapping\Event::createPreSoftDeleteEventArgs()` and `Gedmo\SoftDeleteable\Mapping\Event::createPostSoftDeleteEventArgs()` methods. + ### Changed - Dropped support for PHP < 7.4 - Dropped support for Symfony < 5.4 ### 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. +- Calling `Gedmo\Uploadable\Event\UploadableBaseEventArgs::getEntityManager()` and `getEntity()`. Call `getObjectManager()` and `getObject()` instead. +- `Gedmo\Mapping\Event\AdapterInterface::createLifecycleEventArgsInstance()` method. ## [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 -### Deprecated -- SoftDeleteable: function `createLifecycleEventArgsInstance` is deprecated. Use `createPreSoftDeleteEventArgs` and `createPostSoftDeleteEventArgs` instead. - ## [3.12.0] - 2023-07-08 ### Added - Tree: `setSibling()` and `getSibling()` methods in the `Node` interface through the BC `@method` annotation diff --git a/src/Mapping/Event/Adapter/ODM.php b/src/Mapping/Event/Adapter/ODM.php index 25399cc372..f90ee5417b 100644 --- a/src/Mapping/Event/Adapter/ODM.php +++ b/src/Mapping/Event/Adapter/ODM.php @@ -13,11 +13,8 @@ use Doctrine\ODM\MongoDB\DocumentManager; use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs; use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; -use Doctrine\Persistence\ObjectManager; use Gedmo\Exception\RuntimeException; use Gedmo\Mapping\Event\AdapterInterface; -use Gedmo\SoftDeleteable\Event\ODM\PostSoftDeleteEventArgs; -use Gedmo\SoftDeleteable\Event\ODM\PreSoftDeleteEventArgs; /** * Doctrine event adapter for ODM specific @@ -155,36 +152,13 @@ public function clearObjectChangeSet($uow, $object) /** * Creates a ODM specific LifecycleEventArgs. * - * @param DocumentManager $manager + * @param object $document + * @param DocumentManager $documentManager * - * @deprecated Use createPreSoftDeleteEventArgs() or createPostSoftDeleteEventArgs() instead + * @return LifecycleEventArgs */ - public function createLifecycleEventArgsInstance(object $object, ObjectManager $manager): LifecycleEventArgs + public function createLifecycleEventArgsInstance($document, $documentManager) { - return new LifecycleEventArgs($object, $manager); - } - - /** - * Creates a ODM specific PreSoftDeleteEventArgs. - * - * @param DocumentManager $manager - * - * @return PreSoftDeleteEventArgs - */ - public function createPreSoftDeleteEventArgs(object $object, ObjectManager $manager): LifecycleEventArgs - { - return new PreSoftDeleteEventArgs($object, $manager); - } - - /** - * Creates a ODM specific PostSoftDeleteEventArgs. - * - * @param DocumentManager $manager - * - * @return PostSoftDeleteEventArgs - */ - public function createPostSoftDeleteEventArgs(object $object, ObjectManager $manager): LifecycleEventArgs - { - return new PostSoftDeleteEventArgs($object, $manager); + return new LifecycleEventArgs($document, $documentManager); } } diff --git a/src/Mapping/Event/Adapter/ORM.php b/src/Mapping/Event/Adapter/ORM.php index 41e348a2d5..aa090bfffe 100644 --- a/src/Mapping/Event/Adapter/ORM.php +++ b/src/Mapping/Event/Adapter/ORM.php @@ -11,13 +11,10 @@ use Doctrine\Common\EventArgs; use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\Event\LifecycleEventArgs; use Doctrine\ORM\Mapping\ClassMetadata; -use Doctrine\Persistence\Event\LifecycleEventArgs; -use Doctrine\Persistence\ObjectManager; use Gedmo\Exception\RuntimeException; use Gedmo\Mapping\Event\AdapterInterface; -use Gedmo\SoftDeleteable\Event\ORM\PostSoftDeleteEventArgs; -use Gedmo\SoftDeleteable\Event\ORM\PreSoftDeleteEventArgs; /** * Doctrine event adapter for ORM specific @@ -183,38 +180,15 @@ public function clearObjectChangeSet($uow, $object) } /** - * Creates a ORM specific LifecycleEventArgs. + * Creates an ORM specific LifecycleEventArgs. * - * @param EntityManagerInterface $manager + * @param object $object + * @param EntityManagerInterface $entityManager * - * @deprecated Use createPreSoftDeleteEventArgs() or createPostSoftDeleteEventArgs() instead + * @return LifecycleEventArgs */ - public function createLifecycleEventArgsInstance(object $object, ObjectManager $manager): LifecycleEventArgs + public function createLifecycleEventArgsInstance($object, $entityManager) { - return new LifecycleEventArgs($object, $manager); - } - - /** - * Creates a ORM specific PreSoftDeleteEventArgs. - * - * @param EntityManagerInterface $manager - * - * @return PreSoftDeleteEventArgs - */ - public function createPreSoftDeleteEventArgs(object $object, ObjectManager $manager): LifecycleEventArgs - { - return new PreSoftDeleteEventArgs($object, $manager); - } - - /** - * Creates a ORM specific PostSoftDeleteEventArgs. - * - * @param EntityManagerInterface $manager - * - * @return PostSoftDeleteEventArgs - */ - public function createPostSoftDeleteEventArgs(object $object, ObjectManager $manager): LifecycleEventArgs - { - return new PostSoftDeleteEventArgs($object, $manager); + return new LifecycleEventArgs($object, $entityManager); } } diff --git a/src/Mapping/Event/AdapterInterface.php b/src/Mapping/Event/AdapterInterface.php index 8a024e82eb..339a54f3ea 100644 --- a/src/Mapping/Event/AdapterInterface.php +++ b/src/Mapping/Event/AdapterInterface.php @@ -21,9 +21,7 @@ * * @author Gediminas Morkevicius * - * @method LifecycleEventArgs createLifecycleEventArgsInstance(object $object, ObjectManager $manager) @deprecated Use createPreSoftDeleteEventArgs() or createPostSoftDeleteEventArgs() instead - * @method LifecycleEventArgs createPreSoftDeleteEventArgs(object $object, ObjectManager $manager) - * @method LifecycleEventArgs createPostSoftDeleteEventArgs(object $object, ObjectManager $manager) + * @method LifecycleEventArgs createLifecycleEventArgsInstance(object $object, ObjectManager $manager) @deprecated * @method object getObject() */ interface AdapterInterface diff --git a/src/SoftDeleteable/Event/ORM/PostSoftDeleteEventArgs.php b/src/SoftDeleteable/Event/ORM/PostSoftDeleteEventArgs.php index 74f69e2f3a..7eb7940692 100644 --- a/src/SoftDeleteable/Event/ORM/PostSoftDeleteEventArgs.php +++ b/src/SoftDeleteable/Event/ORM/PostSoftDeleteEventArgs.php @@ -11,8 +11,17 @@ namespace Gedmo\SoftDeleteable\Event\ORM; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Event\LifecycleEventArgs; +use Doctrine\Persistence\Event\LifecycleEventArgs as PersistenceLifecycleEventArgs; -final class PostSoftDeleteEventArgs extends LifecycleEventArgs -{ +if (!class_exists(LifecycleEventArgs::class)) { + /** @template-extends PersistenceLifecycleEventArgs */ + final class PostSoftDeleteEventArgs extends PersistenceLifecycleEventArgs + { + } +} else { + final class PostSoftDeleteEventArgs extends LifecycleEventArgs + { + } } diff --git a/src/SoftDeleteable/Event/ORM/PreSoftDeleteEventArgs.php b/src/SoftDeleteable/Event/ORM/PreSoftDeleteEventArgs.php index e769c72f49..be74f9aafc 100644 --- a/src/SoftDeleteable/Event/ORM/PreSoftDeleteEventArgs.php +++ b/src/SoftDeleteable/Event/ORM/PreSoftDeleteEventArgs.php @@ -11,8 +11,17 @@ namespace Gedmo\SoftDeleteable\Event\ORM; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Event\LifecycleEventArgs; +use Doctrine\Persistence\Event\LifecycleEventArgs as PersistenceLifecycleEventArgs; -final class PreSoftDeleteEventArgs extends LifecycleEventArgs -{ +if (!class_exists(LifecycleEventArgs::class)) { + /** @template-extends PersistenceLifecycleEventArgs */ + final class PreSoftDeleteEventArgs extends PersistenceLifecycleEventArgs + { + } +} else { + final class PreSoftDeleteEventArgs extends LifecycleEventArgs + { + } } diff --git a/src/SoftDeleteable/Mapping/Event/Adapter/ODM.php b/src/SoftDeleteable/Mapping/Event/Adapter/ODM.php index bffbbfd8d2..07bcc32315 100644 --- a/src/SoftDeleteable/Mapping/Event/Adapter/ODM.php +++ b/src/SoftDeleteable/Mapping/Event/Adapter/ODM.php @@ -9,8 +9,12 @@ namespace Gedmo\SoftDeleteable\Mapping\Event\Adapter; +use Doctrine\ODM\MongoDB\DocumentManager; use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; +use Doctrine\Persistence\ObjectManager; use Gedmo\Mapping\Event\Adapter\ODM as BaseAdapterODM; +use Gedmo\SoftDeleteable\Event\ODM\PostSoftDeleteEventArgs; +use Gedmo\SoftDeleteable\Event\ODM\PreSoftDeleteEventArgs; use Gedmo\SoftDeleteable\Mapping\Event\SoftDeleteableAdapter; /** @@ -40,4 +44,20 @@ public function getDateValue($meta, $field) return $datetime; } + + /** + * @param DocumentManager $manager + */ + public function createPreSoftDeleteEventArgs(object $object, ObjectManager $manager): PreSoftDeleteEventArgs + { + return new PreSoftDeleteEventArgs($object, $manager); + } + + /** + * @param DocumentManager $manager + */ + public function createPostSoftDeleteEventArgs(object $object, ObjectManager $manager): PostSoftDeleteEventArgs + { + return new PostSoftDeleteEventArgs($object, $manager); + } } diff --git a/src/SoftDeleteable/Mapping/Event/Adapter/ORM.php b/src/SoftDeleteable/Mapping/Event/Adapter/ORM.php index 6c28005890..bab3b43f76 100644 --- a/src/SoftDeleteable/Mapping/Event/Adapter/ORM.php +++ b/src/SoftDeleteable/Mapping/Event/Adapter/ORM.php @@ -11,8 +11,12 @@ use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadata; +use Doctrine\Persistence\ObjectManager; use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM; +use Gedmo\SoftDeleteable\Event\ORM\PostSoftDeleteEventArgs; +use Gedmo\SoftDeleteable\Event\ORM\PreSoftDeleteEventArgs; use Gedmo\SoftDeleteable\Mapping\Event\SoftDeleteableAdapter; /** @@ -35,6 +39,22 @@ public function getDateValue($meta, $field) return $converter->convertToPHPValue($this->getRawDateValue($mapping), $platform); } + /** + * @param EntityManagerInterface $manager + */ + public function createPreSoftDeleteEventArgs(object $object, ObjectManager $manager): PreSoftDeleteEventArgs + { + return new PreSoftDeleteEventArgs($object, $manager); + } + + /** + * @param EntityManagerInterface $manager + */ + public function createPostSoftDeleteEventArgs(object $object, ObjectManager $manager): PostSoftDeleteEventArgs + { + return new PostSoftDeleteEventArgs($object, $manager); + } + /** * Generates current timestamp for the specified mapping * diff --git a/src/SoftDeleteable/Mapping/Event/SoftDeleteableAdapter.php b/src/SoftDeleteable/Mapping/Event/SoftDeleteableAdapter.php index a855c58c79..ae433e42ae 100644 --- a/src/SoftDeleteable/Mapping/Event/SoftDeleteableAdapter.php +++ b/src/SoftDeleteable/Mapping/Event/SoftDeleteableAdapter.php @@ -9,13 +9,18 @@ namespace Gedmo\SoftDeleteable\Mapping\Event; +use Doctrine\Persistence\Event\LifecycleEventArgs; use Doctrine\Persistence\Mapping\ClassMetadata; +use Doctrine\Persistence\ObjectManager; use Gedmo\Mapping\Event\AdapterInterface; /** * Doctrine event adapter for the SoftDeleteable extension. * * @author Gediminas Morkevicius + * + * @method LifecycleEventArgs createPreSoftDeleteEventArgs(object $object, ObjectManager $manager) + * @method LifecycleEventArgs createPostSoftDeleteEventArgs(object $object, ObjectManager $manager) */ interface SoftDeleteableAdapter extends AdapterInterface { diff --git a/src/SoftDeleteable/SoftDeleteableListener.php b/src/SoftDeleteable/SoftDeleteableListener.php index df973aaabf..39f2a38213 100644 --- a/src/SoftDeleteable/SoftDeleteableListener.php +++ b/src/SoftDeleteable/SoftDeleteableListener.php @@ -81,9 +81,14 @@ public function onFlush(EventArgs $args) continue; // want to hard delete } + // @todo: in the next major remove check and call createPreSoftDeleteEventArgs + $preSoftDeleteEventArgs = method_exists($ea, 'createPreSoftDeleteEventArgs') + ? $ea->createPreSoftDeleteEventArgs($object, $om) + : $ea->createLifecycleEventArgsInstance($object, $om); + $evm->dispatchEvent( self::PRE_SOFT_DELETE, - $ea->createPreSoftDeleteEventArgs($object, $om) + $preSoftDeleteEventArgs ); $reflProp->setValue($object, $date); @@ -98,9 +103,14 @@ public function onFlush(EventArgs $args) ]); } + // @todo: in the next major remove check and call createPostSoftDeleteEventArgs + $postSoftDeleteEventArgs = method_exists($ea, 'createPostSoftDeleteEventArgs') + ? $ea->createPostSoftDeleteEventArgs($object, $om) + : $ea->createLifecycleEventArgsInstance($object, $om); + $evm->dispatchEvent( self::POST_SOFT_DELETE, - $ea->createPostSoftDeleteEventArgs($object, $om) + $postSoftDeleteEventArgs ); } }