Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix EventArgs::getEntityManger deprecation #2639

Merged
merged 6 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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')) {
DubbleClick marked this conversation as resolved.
Show resolved Hide resolved
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);

Check warning on line 111 in src/Mapping/Event/Adapter/ORM.php

View check run for this annotation

Codecov / codecov/patch

src/Mapping/Event/Adapter/ORM.php#L105-L111

Added lines #L105 - L111 were not covered by tests

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

Expand All @@ -104,6 +119,21 @@
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);

Check warning on line 135 in src/Mapping/Event/Adapter/ORM.php

View check run for this annotation

Codecov / codecov/patch

src/Mapping/Event/Adapter/ORM.php#L129-L135

Added lines #L129 - L135 were not covered by tests

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 @@
* @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);

Check warning on line 108 in src/Uploadable/Event/UploadableBaseEventArgs.php

View check run for this annotation

Codecov / codecov/patch

src/Uploadable/Event/UploadableBaseEventArgs.php#L105-L108

Added lines #L105 - L108 were not covered by tests

return $this->em;

Check warning on line 110 in src/Uploadable/Event/UploadableBaseEventArgs.php

View check run for this annotation

Codecov / codecov/patch

src/Uploadable/Event/UploadableBaseEventArgs.php#L110

Added line #L110 was not covered by tests
}

/**
* Retrieve associated EntityManager
*
* @return ObjectManager
*/
public function getObjectManager()

Check warning on line 118 in src/Uploadable/Event/UploadableBaseEventArgs.php

View check run for this annotation

Codecov / codecov/patch

src/Uploadable/Event/UploadableBaseEventArgs.php#L118

Added line #L118 was not covered by tests
{
return $this->em;
DubbleClick marked this conversation as resolved.
Show resolved Hide resolved
}
Expand All @@ -110,6 +126,21 @@
* @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);

Check warning on line 133 in src/Uploadable/Event/UploadableBaseEventArgs.php

View check run for this annotation

Codecov / codecov/patch

src/Uploadable/Event/UploadableBaseEventArgs.php#L130-L133

Added lines #L130 - L133 were not covered by tests

return $this->entity;

Check warning on line 135 in src/Uploadable/Event/UploadableBaseEventArgs.php

View check run for this annotation

Codecov / codecov/patch

src/Uploadable/Event/UploadableBaseEventArgs.php#L135

Added line #L135 was not covered by tests
}

/**
* Retrieve associated Object
*
* @return object
*/
public function getObject()

Check warning on line 143 in src/Uploadable/Event/UploadableBaseEventArgs.php

View check run for this annotation

Codecov / codecov/patch

src/Uploadable/Event/UploadableBaseEventArgs.php#L143

Added line #L143 was not covered by tests
{
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())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could even replace these mocks to use a class that is no deprecated, like Doctrine\ORM\Event\PrePersistEventArgs or Doctrine\ORM\Event\PreUpdateEventArgs.

->method('getEntityManager');
->method('getObjectManager');

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

$eventAdapter = new EventAdapterORM();
Expand Down
Loading