-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Conversation
@franmomu please trigger the workflows again, getEntity() should no longer be called by us. |
It is however noteworthy that there is use of plenty other deprecated functionality throughout the project (EntityManager::create) for example. Not all of it is trivial to work around and that unfortunately exceeds the time I can currently invest. |
doc/symfony6.md
Outdated
@@ -0,0 +1,445 @@ | |||
# Install Gedmo Doctrine extensions in Symfony 6 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is the integration with Symfony 6 related to the deprecation fix for EventArgs::getEntityManger()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not related to EntityManager, it's related to the deprecation of Event Subscribers. This comes from the Symfony Bridge: symfony/symfony#49586
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The simpler solution here might be to make the documentation link to the bundle at https://packagist.org/packages/stof/doctrine-extensions-bundle, which handles registering listeners instead of subscribers, with CI checks warning us if the events used by a listener are updated.
I think we are doing two things here, apart from avoiding calls to |
I'll be happy to remove the deprecation of the methods and open another PR for that. Should I? |
and #2634 for the holy trinity of pull requests to get rid of all deprecations for now |
@DubbleClick if you could please split this PR into two, one that fixes triggering deprecations (could be this one) and another one deprecating methods. The |
yep will do it tomorrow |
rebased it all to only show up in one commit and also created one for EntityManager::create deprecations at #2669 |
I'm not entirely sure how to get rid of all the warnings. Either there will be a warning that ObjectManager is returned rather than EntityManagerInterface, or there will be warnings that ObjectManager doesn't have methods like createQueryBuilder. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for keep working on this, sorry about reverting changes, but it's better if in the same commit there are only changes related to the current fix.
Sorry that I wasn't clear 😞, what I meant in #2639 (comment) was to only add the necessary code to not trigger deprecations.
I think adding ORM::setObjectManager
, UploadableBaseEventArgs::getObjectManager()
and UploadableBaseEventArgs::getObject()
makes sense if we deprecate the old ones, so I would leave that to the other PR.
No worries, I've removed everything not strictly necessary in order to not trigger deprecations now.
It'll be inevitable at some point, but it doesn't have to happen right now. |
@@ -12,7 +12,7 @@ | |||
namespace Gedmo\Tests\Mapping; | |||
|
|||
use Doctrine\ORM\EntityManager; | |||
use Doctrine\ORM\Event\LifecycleEventArgs; | |||
use Doctrine\Persistence\Event\LifecycleEventArgs; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is this code 😞
DoctrineExtensions/src/Mapping/MappedEventSubscriber.php
Lines 252 to 277 in 351caed
/** | |
* Get an event adapter to handle event specific | |
* methods | |
* | |
* @throws InvalidArgumentException if event is not recognized | |
* | |
* @return AdapterInterface | |
*/ | |
protected function getEventAdapter(EventArgs $args) | |
{ | |
$class = get_class($args); | |
if (preg_match('@Doctrine\\\([^\\\]+)@', $class, $m) && in_array($m[1], ['ODM', 'ORM'], true)) { | |
if (!isset($this->adapters[$m[1]])) { | |
$adapterClass = $this->getNamespace().'\\Mapping\\Event\\Adapter\\'.$m[1]; | |
if (!class_exists($adapterClass)) { | |
$adapterClass = 'Gedmo\\Mapping\\Event\\Adapter\\'.$m[1]; | |
} | |
$this->adapters[$m[1]] = new $adapterClass(); | |
} | |
$this->adapters[$m[1]]->setEventArgs($args); | |
return $this->adapters[$m[1]]; | |
} | |
throw new InvalidArgumentException('Event mapper does not support event arg class: '.$class); | |
} |
that do some funny things to determine which event adapter has to be instantiated and it's based on the event class namespace that should include ORM
or ODM
.
It would be awesome to be able to get rid of this code, but since it doesn't seem easy, maybe we should keep using the deprecated Doctrine\Persistence\Event\LifecycleEventArgs;
, once #2669 is merged we can use one of the new dedicated events.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GitHub doesn't have the correct emoji to express my feelings about that code. Sometimes I feel like code reflection goes just a little too far in some languages, hahaha.
Anyway, I've reverted back to Doctrine\ORM\Event\LifecycleEventArgs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding getEventAdapter()
, I stumbled with that yesterday in my tests 😥
GitHub doesn't have the correct emoji to express my feelings about that code. Sometimes I feel like code reflection goes just a little too far in some languages, hahaha.
😵 😵💫 🤣
It would be awesome to be able to get rid of this code, but since it doesn't seem easy, maybe we should keep using the deprecated Doctrine\Persistence\Event\LifecycleEventArgs;, once #2669 is merged we can use one of the new dedicated events.
Just for the records, Doctrine\ORM\Event\LifecycleEventArgs
is deprecated, but Doctrine\Persistence\Event\LifecycleEventArgs
is not.
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #2639 +/- ##
==========================================
- Coverage 79.64% 79.41% -0.23%
==========================================
Files 161 162 +1
Lines 8468 8482 +14
==========================================
- Hits 6744 6736 -8
- Misses 1724 1746 +22
☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this fine after adding the changelog, I'll approve since I'm not sure if this week I'll be available
src/Mapping/Event/Adapter/ORM.php
Outdated
.' and will throw a "%s" error in version 4.0.', | ||
__METHOD__, | ||
get_class($this->args), | ||
\TypeError::class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there won't be an instanceof
check here, I guess we can't even be sure if\Error
will be thrown. If that is the case, I guess we should say something like "it will be not supported" instead.
Hi, just for information I switched to the https://github.com/DubbleClick/DoctrineExtensions/commits/main branch on my project and works as expected. I still get deprecation related to the use of |
@phansys "If there won't be an instanceof check here, I guess we can't even be sure if\Error will be thrown. If that is the case, I guess we should say something like "it will be not supported" instead." No instanceof check is required, calling a method that doesn't exist throws an \Error.
@raziel057 thanks for the confirmation, but be warned that the fork will likely be deleted the day this is merged :p |
@DubbleClick No problem thanks we will switch back as soon as this is merged. |
What is strange (and wrong) at first glance to me, is that we will call a method that is not guaranteed by the object we are trying to call it from. In other words, we are allowing a method call without confirming whether that is possible. |
if (\method_exists($this->args, 'getObject')) {
return $this->args->getObject();
}
@trigger_error(sprintf(
'Calling "%s()" on an event of type "%s" 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(); becomes return $this->args->getObject(); If the method doesn't exist, PHP throws an \Error. |
Thanks for the clarification. |
I don't think it's a design flaw - doctrine changed its functionality to deprecate |
@phansys so what do you think we should do here, it would be nice to finally finish and merge this PR. |
First of all, sorry for the delay on the feedback. Currently, the PR has some deprecation messages that indicate what will be disallowed in the next major release, but IMO, there is no clear path about how the restriction will be implemented. public function getObject(): object
{
if (null === $this->args) {
throw new \LogicException(sprintf('Event args must be set before calling "%s()".', __METHOD__));
}
if (\method_exists($this->args, 'getObject')) {
return $this->args->getObject();
}
@trigger_error(sprintf(
'Calling "%s()" on an event of type "%s" 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();
} By instance, I don't know if the call to I think a comment with the code that must replace these deprecations should be enough. DoctrineExtensions/src/Mapping/Driver/Chain.php Lines 90 to 92 in aaa8c15
|
Isn't that necessarily the same behaviour as it is now? There's even less of a guarantee that the EventArgs implement I'm happy to write a todo block for the next major release, though. |
If this is the case, I'd choose a path that leaves under our own control the responsibility to throw an error explaining in a clear way why the configured |
and add instructions for the next major release
I added instructions and cleared up the error message a little better. Honestly, I am not even sure how far out of their way people would have to go to have DoctrineExtensions catch their own EventArgs implementations. All the Doctrine events we catch and our own implement |
Thanks @DubbleClick ❤️ |
When to expect a release of this? |
in all likelihood before orm 3.0, so I wouldn't worry about it :) |
* 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
tackles #2638 and #2640