Skip to content

Commit 1728df1

Browse files
committed
Use custom lifecycle event classes for the soft-deleteable lifecycle events
1 parent b2c2f14 commit 1728df1

File tree

6 files changed

+71
-3
lines changed

6 files changed

+71
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ a release.
2121
### Changed
2222
- Dropped support for PHP < 7.4
2323
- Dropped support for Symfony < 5.4
24+
- ⚠️ [B/C Break] The `Gedmo\SoftDeleteable\SoftDeletableListener` dispatches events using the new `Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs`
25+
and `Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs` classes, which directly extend from `Doctrine\Persistence\Event\LifecycleEventArgs`; previously,
26+
each object manager's own lifecycle event args class was used.
2427

2528
### Deprecated
2629
- 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()`)
27-
- Calling `Gedmo\Uploadable\Event\UploadableBaseEventArgs::getEntityManager()` and `getEntity()`. Call `getObjectManager()` and `getObject()` instead.
30+
- Calling `Gedmo\Uploadable\Event\UploadableBaseEventArgs::getEntityManager()` and `getEntity()`. Call `getObjectManager()` and `getObject()` instead.
31+
- The `createLifecycleEventArgsInstance()` method on `Gedmo\Mapping\Event\AdapterInterface` implementations is deprecated, use your own subclass of `Doctrine\Persistence\Event\LifecycleEventArgs` as needed.
2832

2933
## [3.13.0] - 2023-09-06
3034
### Fixed

src/Mapping/Event/Adapter/ODM.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,16 @@ public function clearObjectChangeSet($uow, $object)
156156
* @param DocumentManager $documentManager
157157
*
158158
* @return LifecycleEventArgs
159+
*
160+
* @deprecated to be removed in 4.0, use custom lifecycle event classes instead
159161
*/
160162
public function createLifecycleEventArgsInstance($document, $documentManager)
161163
{
164+
@trigger_error(sprintf(
165+
'Using "%s()" method is deprecated since gedmo/doctrine-extensions 3.14 and will be removed in version 4.0.',
166+
__METHOD__
167+
), E_USER_DEPRECATED);
168+
162169
return new LifecycleEventArgs($document, $documentManager);
163170
}
164171
}

src/Mapping/Event/Adapter/ORM.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,20 @@ public function clearObjectChangeSet($uow, $object)
186186
* @param EntityManagerInterface $entityManager
187187
*
188188
* @return LifecycleEventArgs
189+
*
190+
* @deprecated Use custom lifecycle event classes instead
189191
*/
190192
public function createLifecycleEventArgsInstance($document, $entityManager)
191193
{
194+
@trigger_error(sprintf(
195+
'Using "%s()" method is deprecated since gedmo/doctrine-extensions 3.14 and will be removed in version 4.0.',
196+
__METHOD__
197+
), E_USER_DEPRECATED);
198+
199+
if (!class_exists(LifecycleEventArgs::class)) {
200+
throw new \RuntimeException(sprintf('Cannot call %s() when using doctrine/orm >=3.0, use a custom lifecycle event class instead.', __METHOD__));
201+
}
202+
192203
return new LifecycleEventArgs($document, $entityManager);
193204
}
194205
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Doctrine Behavioral Extensions package.
5+
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Gedmo\SoftDeleteable\Event;
11+
12+
use Doctrine\Persistence\Event\LifecycleEventArgs;
13+
use Doctrine\Persistence\ObjectManager;
14+
15+
/**
16+
* Post soft-delete lifecycle event
17+
*
18+
* @extends LifecycleEventArgs<ObjectManager>
19+
*/
20+
final class PostSoftDeleteEventArgs extends LifecycleEventArgs
21+
{
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Doctrine Behavioral Extensions package.
5+
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Gedmo\SoftDeleteable\Event;
11+
12+
use Doctrine\Persistence\Event\LifecycleEventArgs;
13+
use Doctrine\Persistence\ObjectManager;
14+
15+
/**
16+
* Pre soft-delete lifecycle event
17+
*
18+
* @extends LifecycleEventArgs<ObjectManager>
19+
*/
20+
final class PreSoftDeleteEventArgs extends LifecycleEventArgs
21+
{
22+
}

src/SoftDeleteable/SoftDeleteableListener.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Doctrine\Persistence\Mapping\ClassMetadata;
1818
use Doctrine\Persistence\ObjectManager;
1919
use Gedmo\Mapping\MappedEventSubscriber;
20+
use Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs;
21+
use Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs;
2022

2123
/**
2224
* SoftDeleteable listener
@@ -83,7 +85,7 @@ public function onFlush(EventArgs $args)
8385

8486
$evm->dispatchEvent(
8587
self::PRE_SOFT_DELETE,
86-
$ea->createLifecycleEventArgsInstance($object, $om)
88+
new PreSoftDeleteEventArgs($object, $om)
8789
);
8890

8991
$reflProp->setValue($object, $date);
@@ -100,7 +102,7 @@ public function onFlush(EventArgs $args)
100102

101103
$evm->dispatchEvent(
102104
self::POST_SOFT_DELETE,
103-
$ea->createLifecycleEventArgsInstance($object, $om)
105+
new PostSoftDeleteEventArgs($object, $om)
104106
);
105107
}
106108
}

0 commit comments

Comments
 (0)