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 e27f8bf
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 186 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ 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.
- `Gedmo\Mapping\Event\AdapterInterface::createLifecycleEventArgsInstance()` method.

## [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 +38,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
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/*
* 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\Tests\SoftDeleteable\Fixture\Listener;

use Doctrine\Common\EventSubscriber;
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
use Gedmo\SoftDeleteable\SoftDeleteableListener;

final class WithLifecycleEventArgsFromODMTypeListener implements EventSubscriber
{
public function preSoftDelete(LifecycleEventArgs $args): void
{
}

public function postSoftDelete(LifecycleEventArgs $args): void
{
}

public function getSubscribedEvents(): array
{
return [
SoftDeleteableListener::PRE_SOFT_DELETE,
SoftDeleteableListener::POST_SOFT_DELETE,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/*
* 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\Tests\SoftDeleteable\Fixture\Listener;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Gedmo\SoftDeleteable\SoftDeleteableListener;

final class WithLifecycleEventArgsFromORMTypeListener implements EventSubscriber
{
public function preSoftDelete(LifecycleEventArgs $args): void
{
}

public function postSoftDelete(LifecycleEventArgs $args): void
{
}

public function getSubscribedEvents(): array
{
return [
SoftDeleteableListener::PRE_SOFT_DELETE,
SoftDeleteableListener::POST_SOFT_DELETE,
];
}
}
Loading

0 comments on commit e27f8bf

Please sign in to comment.