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

Use custom lifecycle event classes for the soft-deleteable lifecycle events #2725

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ a release.
- Dropped support for PHP < 7.4
- Dropped support for Symfony < 5.4
- Dropped support for doctrine/dbal < 3.2
- ⚠️ [B/C Break] The `Gedmo\SoftDeleteable\SoftDeletableListener` dispatches events using the new `Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs`
and `Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs` classes, which directly extend from `Doctrine\Persistence\Event\LifecycleEventArgs`; previously,
each object manager's own lifecycle event args class was used.

### 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.
- The `createLifecycleEventArgsInstance()` method on `Gedmo\Mapping\Event\AdapterInterface` implementations is deprecated, use your own subclass of `Doctrine\Persistence\Event\LifecycleEventArgs` as needed.

## [3.13.0] - 2023-09-06
### Fixed
Expand Down
7 changes: 7 additions & 0 deletions src/Mapping/Event/Adapter/ODM.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,16 @@
* @param DocumentManager $documentManager
*
* @return LifecycleEventArgs
*
* @deprecated to be removed in 4.0, use custom lifecycle event classes instead
*/
public function createLifecycleEventArgsInstance($document, $documentManager)
{
@trigger_error(sprintf(
'Using "%s()" method is deprecated since gedmo/doctrine-extensions 3.14 and will be removed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

Check warning on line 167 in src/Mapping/Event/Adapter/ODM.php

View check run for this annotation

Codecov / codecov/patch

src/Mapping/Event/Adapter/ODM.php#L164-L167

Added lines #L164 - L167 were not covered by tests

return new LifecycleEventArgs($document, $documentManager);
}
}
11 changes: 11 additions & 0 deletions src/Mapping/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,20 @@
* @param EntityManagerInterface $entityManager
*
* @return LifecycleEventArgs
*
* @deprecated Use custom lifecycle event classes instead
*/
public function createLifecycleEventArgsInstance($document, $entityManager)
{
@trigger_error(sprintf(
'Using "%s()" method is deprecated since gedmo/doctrine-extensions 3.14 and will be removed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

Check warning on line 197 in src/Mapping/Event/Adapter/ORM.php

View check run for this annotation

Codecov / codecov/patch

src/Mapping/Event/Adapter/ORM.php#L194-L197

Added lines #L194 - L197 were not covered by tests

if (!class_exists(LifecycleEventArgs::class)) {
throw new \RuntimeException(sprintf('Cannot call %s() when using doctrine/orm >=3.0, use a custom lifecycle event class instead.', __METHOD__));

Check warning on line 200 in src/Mapping/Event/Adapter/ORM.php

View check run for this annotation

Codecov / codecov/patch

src/Mapping/Event/Adapter/ORM.php#L199-L200

Added lines #L199 - L200 were not covered by tests
}

return new LifecycleEventArgs($document, $entityManager);
}
}
22 changes: 22 additions & 0 deletions src/SoftDeleteable/Event/PostSoftDeleteEventArgs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gedmo\SoftDeleteable\Event;

use Doctrine\Persistence\Event\LifecycleEventArgs;
use Doctrine\Persistence\ObjectManager;

/**
* Post soft-delete lifecycle event
*
* @extends LifecycleEventArgs<ObjectManager>
*/
final class PostSoftDeleteEventArgs extends LifecycleEventArgs
{
}
22 changes: 22 additions & 0 deletions src/SoftDeleteable/Event/PreSoftDeleteEventArgs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gedmo\SoftDeleteable\Event;

use Doctrine\Persistence\Event\LifecycleEventArgs;
use Doctrine\Persistence\ObjectManager;

/**
* Pre soft-delete lifecycle event
*
* @extends LifecycleEventArgs<ObjectManager>
*/
final class PreSoftDeleteEventArgs extends LifecycleEventArgs
{
}
6 changes: 4 additions & 2 deletions src/SoftDeleteable/SoftDeleteableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectManager;
use Gedmo\Mapping\MappedEventSubscriber;
use Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs;
use Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs;

/**
* SoftDeleteable listener
Expand Down Expand Up @@ -83,7 +85,7 @@ public function onFlush(EventArgs $args)

$evm->dispatchEvent(
self::PRE_SOFT_DELETE,
$ea->createLifecycleEventArgsInstance($object, $om)
new PreSoftDeleteEventArgs($object, $om)
);

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

$evm->dispatchEvent(
self::POST_SOFT_DELETE,
$ea->createLifecycleEventArgsInstance($object, $om)
new PostSoftDeleteEventArgs($object, $om)
);
}
}
Expand Down