Skip to content

Commit

Permalink
Use Pre and Post event args when it is type-hinted
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Dec 4, 2023
1 parent 93cfaab commit cb6be44
Show file tree
Hide file tree
Showing 18 changed files with 298 additions and 192 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ a release.
---

## [Unreleased]
### Added
- SoftDeleteable: `Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs` and
`Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs` classes.

### Deprecated
- Do not add type-hinted parameters `Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs` and
`Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs` classes to `preSoftDelete` and `postSoftDelete` events.
- The `createLifecycleEventArgsInstance()` method on `Gedmo\Mapping\Event\AdapterInterface`
implementations is deprecated, use your own subclass of `Doctrine\Persistence\Event\LifecycleEventArgs` as needed.

## [3.14.0]
### Added
- Support for Symfony 7
- Tree: Added `@template` and `@template-extends` annotations to the Tree repositories
- SoftDeleteable: `Gedmo\SoftDeleteable\Mapping\Event::createPreSoftDeleteEventArgs()` and `Gedmo\SoftDeleteable\Mapping\Event::createPostSoftDeleteEventArgs()` methods.

### Changed
- Dropped support for PHP < 7.4
Expand All @@ -33,7 +41,6 @@ a release.
### 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.
- `Gedmo\Mapping\Event\AdapterInterface::createLifecycleEventArgsInstance()` method.

## [3.13.0] - 2023-09-06
### Fixed
Expand Down
4 changes: 0 additions & 4 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/

use Rector\Config\RectorConfig;
use Rector\Php71\Rector\FuncCall\CountOnNullRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;

Expand All @@ -33,7 +32,4 @@

$rectorConfig->importNames();
$rectorConfig->importShortClasses(false);
$rectorConfig->skip([
CountOnNullRector::class,
]);
};
7 changes: 7 additions & 0 deletions src/Mapping/Event/Adapter/ODM.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ public function clearObjectChangeSet($uow, $object)
}

/**
* @deprecated to be removed in 4.0, use custom lifecycle event classes instead.
*
* Creates a ODM specific LifecycleEventArgs.
*
* @param object $document
Expand All @@ -159,6 +161,11 @@ public function clearObjectChangeSet($uow, $object)
*/
public function createLifecycleEventArgsInstance($document, $documentManager)
{
@trigger_error(sprintf(
'Using "%s()" method is deprecated since gedmo/doctrine-extensions 3.15 and will be removed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

return new LifecycleEventArgs($document, $documentManager);
}
}
13 changes: 12 additions & 1 deletion src/Mapping/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ public function clearObjectChangeSet($uow, $object)
}

/**
* Creates an ORM specific LifecycleEventArgs.
* @deprecated use custom lifecycle event classes instead
*
* Creates an ORM specific LifecycleEventArgs
*
* @param object $object
* @param EntityManagerInterface $entityManager
Expand All @@ -189,6 +191,15 @@ public function clearObjectChangeSet($uow, $object)
*/
public function createLifecycleEventArgsInstance($object, $entityManager)
{
@trigger_error(sprintf(
'Using "%s()" method is deprecated since gedmo/doctrine-extensions 3.15 and will be removed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

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#L200

Added line #L200 was not covered by tests
}

return new LifecycleEventArgs($object, $entityManager);
}
}
27 changes: 0 additions & 27 deletions src/SoftDeleteable/Event/ORM/PostSoftDeleteEventArgs.php

This file was deleted.

27 changes: 0 additions & 27 deletions src/SoftDeleteable/Event/ORM/PreSoftDeleteEventArgs.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
* file that was distributed with this source code.
*/

namespace Gedmo\SoftDeleteable\Event\ODM;
namespace Gedmo\SoftDeleteable\Event;

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

/**
* @template TObjectManager of ObjectManager
*
* @template-extends LifecycleEventArgs<TObjectManager>
*/
final class PostSoftDeleteEventArgs extends LifecycleEventArgs
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
* file that was distributed with this source code.
*/

namespace Gedmo\SoftDeleteable\Event\ODM;
namespace Gedmo\SoftDeleteable\Event;

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

/**
* @template TObjectManager of ObjectManager
*
* @template-extends LifecycleEventArgs<TObjectManager>
*/
final class PreSoftDeleteEventArgs extends LifecycleEventArgs
{
}
20 changes: 0 additions & 20 deletions src/SoftDeleteable/Mapping/Event/Adapter/ODM.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@

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;

/**
Expand Down Expand Up @@ -44,20 +40,4 @@ 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);
}
}
20 changes: 0 additions & 20 deletions src/SoftDeleteable/Mapping/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@

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;

/**
Expand All @@ -39,22 +35,6 @@ 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
*
Expand Down
5 changes: 0 additions & 5 deletions src/SoftDeleteable/Mapping/Event/SoftDeleteableAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@

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 <gediminas.morkevicius@gmail.com>
*
* @method LifecycleEventArgs createPreSoftDeleteEventArgs(object $object, ObjectManager $manager)
* @method LifecycleEventArgs createPostSoftDeleteEventArgs(object $object, ObjectManager $manager)
*/
interface SoftDeleteableAdapter extends AdapterInterface
{
Expand Down
67 changes: 51 additions & 16 deletions src/SoftDeleteable/SoftDeleteableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
namespace Gedmo\SoftDeleteable;

use Doctrine\Common\EventArgs;
use Doctrine\Common\EventManager;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\UnitOfWork as MongoDBUnitOfWork;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
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 @@ -81,15 +84,17 @@ 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);
if ($evm->hasListeners(self::PRE_SOFT_DELETE)) {
// @todo: in the next major remove check and only instantiate the event
$preSoftDeleteEventArgs = $this->hasToDispatchNewEvent($evm, self::PRE_SOFT_DELETE, PreSoftDeleteEventArgs::class)
? new PreSoftDeleteEventArgs($object, $om)
: $ea->createLifecycleEventArgsInstance($object, $om);

$evm->dispatchEvent(
self::PRE_SOFT_DELETE,
$preSoftDeleteEventArgs
);
$evm->dispatchEvent(
self::PRE_SOFT_DELETE,
$preSoftDeleteEventArgs
);
}

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

Expand All @@ -103,15 +108,17 @@ 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);
if ($evm->hasListeners(self::POST_SOFT_DELETE)) {
// @todo: in the next major remove check and only instantiate the event
$postSoftDeleteEventArgs = $this->hasToDispatchNewEvent($evm, self::POST_SOFT_DELETE, PostSoftDeleteEventArgs::class)
? new PostSoftDeleteEventArgs($object, $om)
: $ea->createLifecycleEventArgsInstance($object, $om);

$evm->dispatchEvent(
self::POST_SOFT_DELETE,
$postSoftDeleteEventArgs
);
$evm->dispatchEvent(
self::POST_SOFT_DELETE,
$postSoftDeleteEventArgs
);
}
}
}
}
Expand All @@ -134,4 +141,32 @@ protected function getNamespace()
{
return __NAMESPACE__;
}

/** @param class-string $eventClass */
private function hasToDispatchNewEvent(EventManager $eventManager, string $eventName, string $eventClass): bool
{
foreach ($eventManager->getListeners($eventName) as $listener) {
$reflMethod = new \ReflectionMethod($listener, $eventName);

$parameters = $reflMethod->getParameters();

if (
1 !== count($parameters)
|| !$parameters[0]->hasType()
|| !$parameters[0]->getType() instanceof \ReflectionNamedType
|| $eventClass !== $parameters[0]->getType()->getName()
) {
@trigger_error(sprintf(
'Type-hinting to something different than "%s" in "%s::%s()" is deprecated.',
$eventClass,
get_class($listener),
$reflMethod->getName()
), E_USER_DEPRECATED);

return false;
}
}

return true;
}
}
Loading

0 comments on commit cb6be44

Please sign in to comment.