Skip to content

Commit cb6be44

Browse files
committed
Use Pre and Post event args when it is type-hinted
1 parent 93cfaab commit cb6be44

18 files changed

+298
-192
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ 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
2433
- Support for Symfony 7
2534
- Tree: Added `@template` and `@template-extends` annotations to the Tree repositories
26-
- SoftDeleteable: `Gedmo\SoftDeleteable\Mapping\Event::createPreSoftDeleteEventArgs()` and `Gedmo\SoftDeleteable\Mapping\Event::createPostSoftDeleteEventArgs()` methods.
2735

2836
### Changed
2937
- Dropped support for PHP < 7.4
@@ -33,7 +41,6 @@ a release.
3341
### Deprecated
3442
- 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()`)
3543
- Calling `Gedmo\Uploadable\Event\UploadableBaseEventArgs::getEntityManager()` and `getEntity()`. Call `getObjectManager()` and `getObject()` instead.
36-
- `Gedmo\Mapping\Event\AdapterInterface::createLifecycleEventArgsInstance()` method.
3744

3845
## [3.13.0] - 2023-09-06
3946
### 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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ public function clearObjectChangeSet($uow, $object)
180180
}
181181

182182
/**
183-
* Creates an ORM specific LifecycleEventArgs.
183+
* @deprecated use custom lifecycle event classes instead
184+
*
185+
* Creates an ORM specific LifecycleEventArgs
184186
*
185187
* @param object $object
186188
* @param EntityManagerInterface $entityManager
@@ -189,6 +191,15 @@ public function clearObjectChangeSet($uow, $object)
189191
*/
190192
public function createLifecycleEventArgsInstance($object, $entityManager)
191193
{
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+
192203
return new LifecycleEventArgs($object, $entityManager);
193204
}
194205
}

src/SoftDeleteable/Event/ORM/PostSoftDeleteEventArgs.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/SoftDeleteable/Event/ORM/PreSoftDeleteEventArgs.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/SoftDeleteable/Event/ODM/PostSoftDeleteEventArgs.php renamed to src/SoftDeleteable/Event/PostSoftDeleteEventArgs.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Gedmo\SoftDeleteable\Event\ODM;
12+
namespace Gedmo\SoftDeleteable\Event;
1313

14-
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
14+
use Doctrine\Persistence\Event\LifecycleEventArgs;
15+
use Doctrine\Persistence\ObjectManager;
1516

17+
/**
18+
* @template TObjectManager of ObjectManager
19+
*
20+
* @template-extends LifecycleEventArgs<TObjectManager>
21+
*/
1622
final class PostSoftDeleteEventArgs extends LifecycleEventArgs
1723
{
1824
}

src/SoftDeleteable/Event/ODM/PreSoftDeleteEventArgs.php renamed to src/SoftDeleteable/Event/PreSoftDeleteEventArgs.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Gedmo\SoftDeleteable\Event\ODM;
12+
namespace Gedmo\SoftDeleteable\Event;
1313

14-
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
14+
use Doctrine\Persistence\Event\LifecycleEventArgs;
15+
use Doctrine\Persistence\ObjectManager;
1516

17+
/**
18+
* @template TObjectManager of ObjectManager
19+
*
20+
* @template-extends LifecycleEventArgs<TObjectManager>
21+
*/
1622
final class PreSoftDeleteEventArgs extends LifecycleEventArgs
1723
{
1824
}

src/SoftDeleteable/Mapping/Event/Adapter/ODM.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@
99

1010
namespace Gedmo\SoftDeleteable\Mapping\Event\Adapter;
1111

12-
use Doctrine\ODM\MongoDB\DocumentManager;
1312
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
14-
use Doctrine\Persistence\ObjectManager;
1513
use Gedmo\Mapping\Event\Adapter\ODM as BaseAdapterODM;
16-
use Gedmo\SoftDeleteable\Event\ODM\PostSoftDeleteEventArgs;
17-
use Gedmo\SoftDeleteable\Event\ODM\PreSoftDeleteEventArgs;
1814
use Gedmo\SoftDeleteable\Mapping\Event\SoftDeleteableAdapter;
1915

2016
/**
@@ -44,20 +40,4 @@ public function getDateValue($meta, $field)
4440

4541
return $datetime;
4642
}
47-
48-
/**
49-
* @param DocumentManager $manager
50-
*/
51-
public function createPreSoftDeleteEventArgs(object $object, ObjectManager $manager): PreSoftDeleteEventArgs
52-
{
53-
return new PreSoftDeleteEventArgs($object, $manager);
54-
}
55-
56-
/**
57-
* @param DocumentManager $manager
58-
*/
59-
public function createPostSoftDeleteEventArgs(object $object, ObjectManager $manager): PostSoftDeleteEventArgs
60-
{
61-
return new PostSoftDeleteEventArgs($object, $manager);
62-
}
6343
}

src/SoftDeleteable/Mapping/Event/Adapter/ORM.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@
1111

1212
use Doctrine\DBAL\Types\Type;
1313
use Doctrine\DBAL\Types\Types;
14-
use Doctrine\ORM\EntityManagerInterface;
1514
use Doctrine\ORM\Mapping\ClassMetadata;
16-
use Doctrine\Persistence\ObjectManager;
1715
use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM;
18-
use Gedmo\SoftDeleteable\Event\ORM\PostSoftDeleteEventArgs;
19-
use Gedmo\SoftDeleteable\Event\ORM\PreSoftDeleteEventArgs;
2016
use Gedmo\SoftDeleteable\Mapping\Event\SoftDeleteableAdapter;
2117

2218
/**
@@ -39,22 +35,6 @@ public function getDateValue($meta, $field)
3935
return $converter->convertToPHPValue($this->getRawDateValue($mapping), $platform);
4036
}
4137

42-
/**
43-
* @param EntityManagerInterface $manager
44-
*/
45-
public function createPreSoftDeleteEventArgs(object $object, ObjectManager $manager): PreSoftDeleteEventArgs
46-
{
47-
return new PreSoftDeleteEventArgs($object, $manager);
48-
}
49-
50-
/**
51-
* @param EntityManagerInterface $manager
52-
*/
53-
public function createPostSoftDeleteEventArgs(object $object, ObjectManager $manager): PostSoftDeleteEventArgs
54-
{
55-
return new PostSoftDeleteEventArgs($object, $manager);
56-
}
57-
5838
/**
5939
* Generates current timestamp for the specified mapping
6040
*

src/SoftDeleteable/Mapping/Event/SoftDeleteableAdapter.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,13 @@
99

1010
namespace Gedmo\SoftDeleteable\Mapping\Event;
1111

12-
use Doctrine\Persistence\Event\LifecycleEventArgs;
1312
use Doctrine\Persistence\Mapping\ClassMetadata;
14-
use Doctrine\Persistence\ObjectManager;
1513
use Gedmo\Mapping\Event\AdapterInterface;
1614

1715
/**
1816
* Doctrine event adapter for the SoftDeleteable extension.
1917
*
2018
* @author Gediminas Morkevicius <[email protected]>
21-
*
22-
* @method LifecycleEventArgs createPreSoftDeleteEventArgs(object $object, ObjectManager $manager)
23-
* @method LifecycleEventArgs createPostSoftDeleteEventArgs(object $object, ObjectManager $manager)
2419
*/
2520
interface SoftDeleteableAdapter extends AdapterInterface
2621
{

src/SoftDeleteable/SoftDeleteableListener.php

Lines changed: 51 additions & 16 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,15 +84,17 @@ public function onFlush(EventArgs $args)
8184
continue; // want to hard delete
8285
}
8386

84-
// @todo: in the next major remove check and call createPreSoftDeleteEventArgs
85-
$preSoftDeleteEventArgs = method_exists($ea, 'createPreSoftDeleteEventArgs')
86-
? $ea->createPreSoftDeleteEventArgs($object, $om)
87-
: $ea->createLifecycleEventArgsInstance($object, $om);
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);
8892

89-
$evm->dispatchEvent(
90-
self::PRE_SOFT_DELETE,
91-
$preSoftDeleteEventArgs
92-
);
93+
$evm->dispatchEvent(
94+
self::PRE_SOFT_DELETE,
95+
$preSoftDeleteEventArgs
96+
);
97+
}
9398

9499
$reflProp->setValue($object, $date);
95100

@@ -103,15 +108,17 @@ public function onFlush(EventArgs $args)
103108
]);
104109
}
105110

106-
// @todo: in the next major remove check and call createPostSoftDeleteEventArgs
107-
$postSoftDeleteEventArgs = method_exists($ea, 'createPostSoftDeleteEventArgs')
108-
? $ea->createPostSoftDeleteEventArgs($object, $om)
109-
: $ea->createLifecycleEventArgsInstance($object, $om);
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);
110116

111-
$evm->dispatchEvent(
112-
self::POST_SOFT_DELETE,
113-
$postSoftDeleteEventArgs
114-
);
117+
$evm->dispatchEvent(
118+
self::POST_SOFT_DELETE,
119+
$postSoftDeleteEventArgs
120+
);
121+
}
115122
}
116123
}
117124
}
@@ -134,4 +141,32 @@ protected function getNamespace()
134141
{
135142
return __NAMESPACE__;
136143
}
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+
}
137172
}

0 commit comments

Comments
 (0)