Skip to content

Commit

Permalink
fix EventArgs::getEntityManger deprecation (#2639)
Browse files Browse the repository at this point in the history
* use EventArgs::getObjectManager (fix DoctrineBundle 2.11 deprecation)

* add deprecation messages to test

* fix deprecation error type

* add changelog note

* clear up deprecation messages

and add instructions for the next major release

* re-add type to depreaction message
  • Loading branch information
DubbleClick authored Oct 29, 2023
1 parent aaa8c15 commit 7a7612a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ a release.

## [Unreleased]

## [3.13.0]
### 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.

## [3.13.0] - 2023-09-06
### Fixed
- References: fixed condition in `XML` Driver that did not allow to retrieve from the entity definition the `mappedBy` and `inversedBy` fields.
- Fix bug collecting metadata for inherited mapped classes
Expand Down
30 changes: 30 additions & 0 deletions src/Mapping/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ public function getObjectManager()
throw new \LogicException(sprintf('Event args must be set before calling "%s()".', __METHOD__));
}

// todo: for the next major release, uncomment the next line:
// return $this->args->getObjectManager();
// and remove anything past this
if (\method_exists($this->args, 'getObjectManager')) {
return $this->args->getObjectManager();
}

@trigger_error(sprintf(
'Calling "%s()" on event args of class "%s" that does not implement "getObjectManager()" is deprecated since gedmo/doctrine-extensions 3.x'
.' and will throw a "%s" error in version 4.0.',
__METHOD__,
get_class($this->args),
\Error::class
), E_USER_DEPRECATED);

return $this->args->getEntityManager();
}

Expand All @@ -104,6 +119,21 @@ public function getObject(): object
throw new \LogicException(sprintf('Event args must be set before calling "%s()".', __METHOD__));
}

// todo: for the next major release, uncomment the next line:
// return $this->args->getObject();
// and remove anything past this
if (\method_exists($this->args, 'getObject')) {
return $this->args->getObject();
}

@trigger_error(sprintf(
'Calling "%s()" on event args of class "%s" that does not imeplement "getObject()" is deprecated since gedmo/doctrine-extensions 3.x'
.' and will throw a "%s" error in version 4.0.',
__METHOD__,
get_class($this->args),
\Error::class
), E_USER_DEPRECATED);

return $this->args->getEntity();
}

Expand Down
31 changes: 31 additions & 0 deletions src/Uploadable/Event/UploadableBaseEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Doctrine\Common\EventArgs;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectManager;
use Gedmo\Uploadable\FileInfo\FileInfoInterface;
use Gedmo\Uploadable\UploadableListener;

Expand Down Expand Up @@ -100,6 +101,21 @@ public function getListener()
* @return EntityManagerInterface
*/
public function getEntityManager()
{
@trigger_error(sprintf(
'"%s()" is deprecated since gedmo/doctrine-extensions 3.x and will be removed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

return $this->em;
}

/**
* Retrieve associated EntityManager
*
* @return ObjectManager
*/
public function getObjectManager()
{
return $this->em;
}
Expand All @@ -110,6 +126,21 @@ public function getEntityManager()
* @return object
*/
public function getEntity()
{
@trigger_error(sprintf(
'"%s()" is deprecated since gedmo/doctrine-extensions 3.x and will be removed in version 4.0.',
__METHOD__
), E_USER_DEPRECATED);

return $this->entity;
}

/**
* Retrieve associated Object
*
* @return object
*/
public function getObject()
{
return $this->entity;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Gedmo/Mapping/MappingEventAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public function testAdapterBehavior(): void
->disableOriginalConstructor()
->getMock();
$eventArgsMock->expects(static::once())
->method('getEntityManager');
->method('getObjectManager');

$eventArgsMock->expects(static::once())
->method('getEntity')
->method('getObject')
->willReturn(new \stdClass());

$eventAdapter = new EventAdapterORM();
Expand Down

0 comments on commit 7a7612a

Please sign in to comment.