Skip to content

Commit a4f15df

Browse files
use dedicated event eventArgs (#2649)
* use dedicated eventArgs * fix workflow fails * fix * move functions to appropriate class * move functions to appropriate class * add changelog note * Move presoftdeletable methods * update tests to expect dedicated eventargs instances * Use Pre and Post event args when it is type-hinted --------- Co-authored-by: Fran Moreno <[email protected]>
1 parent 3b5b5cb commit a4f15df

14 files changed

+337
-80
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ a release.
1818
---
1919

2020
## [Unreleased]
21+
### Added
22+
- SoftDeleteable: `Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs` and
23+
`Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs` classes.
24+
25+
### Deprecated
26+
- Do not add type-hinted parameters `Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs` and
27+
`Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs` classes to `preSoftDelete` and `postSoftDelete` events.
28+
- The `createLifecycleEventArgsInstance()` method on `Gedmo\Mapping\Event\AdapterInterface`
29+
implementations is deprecated, use your own subclass of `Doctrine\Persistence\Event\LifecycleEventArgs` as needed.
2130

2231
## [3.14.0]
2332
### Added
@@ -31,7 +40,7 @@ a release.
3140

3241
### Deprecated
3342
- 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()`)
34-
- Calling `Gedmo\Uploadable\Event\UploadableBaseEventArgs::getEntityManager()` and `getEntity()`. Call `getObjectManager()` and `getObject()` instead.
43+
- Calling `Gedmo\Uploadable\Event\UploadableBaseEventArgs::getEntityManager()` and `getEntity()`. Call `getObjectManager()` and `getObject()` instead.
3544

3645
## [3.13.0] - 2023-09-06
3746
### Fixed

rector.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*/
1111

1212
use Rector\Config\RectorConfig;
13-
use Rector\Php71\Rector\FuncCall\CountOnNullRector;
1413
use Rector\Set\ValueObject\LevelSetList;
1514
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;
1615

@@ -33,7 +32,4 @@
3332

3433
$rectorConfig->importNames();
3534
$rectorConfig->importShortClasses(false);
36-
$rectorConfig->skip([
37-
CountOnNullRector::class,
38-
]);
3935
};

src/Mapping/Event/Adapter/ODM.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ public function clearObjectChangeSet($uow, $object)
150150
}
151151

152152
/**
153+
* @deprecated to be removed in 4.0, use custom lifecycle event classes instead.
154+
*
153155
* Creates a ODM specific LifecycleEventArgs.
154156
*
155157
* @param object $document
@@ -159,6 +161,11 @@ public function clearObjectChangeSet($uow, $object)
159161
*/
160162
public function createLifecycleEventArgsInstance($document, $documentManager)
161163
{
164+
@trigger_error(sprintf(
165+
'Using "%s()" method is deprecated since gedmo/doctrine-extensions 3.15 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: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,26 @@ public function clearObjectChangeSet($uow, $object)
180180
}
181181

182182
/**
183-
* Creates a ORM specific LifecycleEventArgs.
183+
* @deprecated use custom lifecycle event classes instead
184184
*
185-
* @param object $document
185+
* Creates an ORM specific LifecycleEventArgs
186+
*
187+
* @param object $object
186188
* @param EntityManagerInterface $entityManager
187189
*
188190
* @return LifecycleEventArgs
189191
*/
190-
public function createLifecycleEventArgsInstance($document, $entityManager)
192+
public function createLifecycleEventArgsInstance($object, $entityManager)
191193
{
192-
return new LifecycleEventArgs($document, $entityManager);
194+
@trigger_error(sprintf(
195+
'Using "%s()" method is deprecated since gedmo/doctrine-extensions 3.15 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+
203+
return new LifecycleEventArgs($object, $entityManager);
193204
}
194205
}

src/Mapping/Event/AdapterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
* @author Gediminas Morkevicius <[email protected]>
2323
*
24-
* @method LifecycleEventArgs createLifecycleEventArgsInstance(object $object, ObjectManager $manager)
24+
* @method LifecycleEventArgs createLifecycleEventArgsInstance(object $object, ObjectManager $manager) @deprecated
2525
* @method object getObject()
2626
*/
2727
interface AdapterInterface
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Doctrine Behavioral Extensions package.
7+
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Gedmo\SoftDeleteable\Event;
13+
14+
use Doctrine\Persistence\Event\LifecycleEventArgs;
15+
use Doctrine\Persistence\ObjectManager;
16+
17+
/**
18+
* @template TObjectManager of ObjectManager
19+
*
20+
* @template-extends LifecycleEventArgs<TObjectManager>
21+
*/
22+
final class PostSoftDeleteEventArgs extends LifecycleEventArgs
23+
{
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Doctrine Behavioral Extensions package.
7+
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Gedmo\SoftDeleteable\Event;
13+
14+
use Doctrine\Persistence\Event\LifecycleEventArgs;
15+
use Doctrine\Persistence\ObjectManager;
16+
17+
/**
18+
* @template TObjectManager of ObjectManager
19+
*
20+
* @template-extends LifecycleEventArgs<TObjectManager>
21+
*/
22+
final class PreSoftDeleteEventArgs extends LifecycleEventArgs
23+
{
24+
}

src/SoftDeleteable/SoftDeleteableListener.php

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
namespace Gedmo\SoftDeleteable;
1111

1212
use Doctrine\Common\EventArgs;
13+
use Doctrine\Common\EventManager;
1314
use Doctrine\ODM\MongoDB\DocumentManager;
1415
use Doctrine\ODM\MongoDB\UnitOfWork as MongoDBUnitOfWork;
1516
use Doctrine\ORM\EntityManagerInterface;
1617
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
1718
use Doctrine\Persistence\Mapping\ClassMetadata;
1819
use Doctrine\Persistence\ObjectManager;
1920
use Gedmo\Mapping\MappedEventSubscriber;
21+
use Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs;
22+
use Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs;
2023

2124
/**
2225
* SoftDeleteable listener
@@ -81,10 +84,17 @@ public function onFlush(EventArgs $args)
8184
continue; // want to hard delete
8285
}
8386

84-
$evm->dispatchEvent(
85-
self::PRE_SOFT_DELETE,
86-
$ea->createLifecycleEventArgsInstance($object, $om)
87-
);
87+
if ($evm->hasListeners(self::PRE_SOFT_DELETE)) {
88+
// @todo: in the next major remove check and only instantiate the event
89+
$preSoftDeleteEventArgs = $this->hasToDispatchNewEvent($evm, self::PRE_SOFT_DELETE, PreSoftDeleteEventArgs::class)
90+
? new PreSoftDeleteEventArgs($object, $om)
91+
: $ea->createLifecycleEventArgsInstance($object, $om);
92+
93+
$evm->dispatchEvent(
94+
self::PRE_SOFT_DELETE,
95+
$preSoftDeleteEventArgs
96+
);
97+
}
8898

8999
$reflProp->setValue($object, $date);
90100

@@ -98,10 +108,17 @@ public function onFlush(EventArgs $args)
98108
]);
99109
}
100110

101-
$evm->dispatchEvent(
102-
self::POST_SOFT_DELETE,
103-
$ea->createLifecycleEventArgsInstance($object, $om)
104-
);
111+
if ($evm->hasListeners(self::POST_SOFT_DELETE)) {
112+
// @todo: in the next major remove check and only instantiate the event
113+
$postSoftDeleteEventArgs = $this->hasToDispatchNewEvent($evm, self::POST_SOFT_DELETE, PostSoftDeleteEventArgs::class)
114+
? new PostSoftDeleteEventArgs($object, $om)
115+
: $ea->createLifecycleEventArgsInstance($object, $om);
116+
117+
$evm->dispatchEvent(
118+
self::POST_SOFT_DELETE,
119+
$postSoftDeleteEventArgs
120+
);
121+
}
105122
}
106123
}
107124
}
@@ -124,4 +141,32 @@ protected function getNamespace()
124141
{
125142
return __NAMESPACE__;
126143
}
144+
145+
/** @param class-string $eventClass */
146+
private function hasToDispatchNewEvent(EventManager $eventManager, string $eventName, string $eventClass): bool
147+
{
148+
foreach ($eventManager->getListeners($eventName) as $listener) {
149+
$reflMethod = new \ReflectionMethod($listener, $eventName);
150+
151+
$parameters = $reflMethod->getParameters();
152+
153+
if (
154+
1 !== count($parameters)
155+
|| !$parameters[0]->hasType()
156+
|| !$parameters[0]->getType() instanceof \ReflectionNamedType
157+
|| $eventClass !== $parameters[0]->getType()->getName()
158+
) {
159+
@trigger_error(sprintf(
160+
'Type-hinting to something different than "%s" in "%s::%s()" is deprecated.',
161+
$eventClass,
162+
get_class($listener),
163+
$reflMethod->getName()
164+
), E_USER_DEPRECATED);
165+
166+
return false;
167+
}
168+
}
169+
170+
return true;
171+
}
127172
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Doctrine Behavioral Extensions package.
7+
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Gedmo\Tests\SoftDeleteable\Fixture\Listener;
13+
14+
use Doctrine\Common\EventSubscriber;
15+
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
16+
use Gedmo\SoftDeleteable\SoftDeleteableListener;
17+
18+
final class WithLifecycleEventArgsFromODMTypeListener implements EventSubscriber
19+
{
20+
public function preSoftDelete(LifecycleEventArgs $args): void
21+
{
22+
}
23+
24+
public function postSoftDelete(LifecycleEventArgs $args): void
25+
{
26+
}
27+
28+
public function getSubscribedEvents(): array
29+
{
30+
return [
31+
SoftDeleteableListener::PRE_SOFT_DELETE,
32+
SoftDeleteableListener::POST_SOFT_DELETE,
33+
];
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Doctrine Behavioral Extensions package.
7+
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Gedmo\Tests\SoftDeleteable\Fixture\Listener;
13+
14+
use Doctrine\Common\EventSubscriber;
15+
use Doctrine\ORM\Event\LifecycleEventArgs;
16+
use Gedmo\SoftDeleteable\SoftDeleteableListener;
17+
18+
final class WithLifecycleEventArgsFromORMTypeListener implements EventSubscriber
19+
{
20+
public function preSoftDelete(LifecycleEventArgs $args): void
21+
{
22+
}
23+
24+
public function postSoftDelete(LifecycleEventArgs $args): void
25+
{
26+
}
27+
28+
public function getSubscribedEvents(): array
29+
{
30+
return [
31+
SoftDeleteableListener::PRE_SOFT_DELETE,
32+
SoftDeleteableListener::POST_SOFT_DELETE,
33+
];
34+
}
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Doctrine Behavioral Extensions package.
7+
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Gedmo\Tests\SoftDeleteable\Fixture\Listener;
13+
14+
use Doctrine\Common\EventSubscriber;
15+
use Doctrine\Persistence\ObjectManager;
16+
use Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs;
17+
use Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs;
18+
use Gedmo\SoftDeleteable\SoftDeleteableListener;
19+
20+
final class WithPreAndPostSoftDeleteEventArgsTypeListener implements EventSubscriber
21+
{
22+
/** @param PreSoftDeleteEventArgs<ObjectManager> $args */
23+
public function preSoftDelete(PreSoftDeleteEventArgs $args): void
24+
{
25+
}
26+
27+
/** @param PostSoftDeleteEventArgs<ObjectManager> $args */
28+
public function postSoftDelete(PostSoftDeleteEventArgs $args): void
29+
{
30+
}
31+
32+
public function getSubscribedEvents(): array
33+
{
34+
return [
35+
SoftDeleteableListener::PRE_SOFT_DELETE,
36+
SoftDeleteableListener::POST_SOFT_DELETE,
37+
];
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Doctrine Behavioral Extensions package.
7+
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Gedmo\Tests\SoftDeleteable\Fixture\Listener;
13+
14+
use Doctrine\Common\EventSubscriber;
15+
use Doctrine\Persistence\ObjectManager;
16+
use Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs;
17+
use Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs;
18+
use Gedmo\SoftDeleteable\SoftDeleteableListener;
19+
20+
final class WithoutTypeListener implements EventSubscriber
21+
{
22+
/** @param PreSoftDeleteEventArgs<ObjectManager> $args */
23+
public function preSoftDelete($args): void
24+
{
25+
}
26+
27+
/** @param PostSoftDeleteEventArgs<ObjectManager> $args */
28+
public function postSoftDelete($args): void
29+
{
30+
}
31+
32+
public function getSubscribedEvents(): array
33+
{
34+
return [
35+
SoftDeleteableListener::PRE_SOFT_DELETE,
36+
SoftDeleteableListener::POST_SOFT_DELETE,
37+
];
38+
}
39+
}

0 commit comments

Comments
 (0)