-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Instantiator - Add an interface and default implementation to instantiate objects #38487
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
Conversation
8f60a0a
to
8f36f2a
Compare
src/Symfony/Component/Serializer/Context/ChildContextFactory.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Instantiator/InstantiatorResult.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Normalizer/Factory/GetSetMethodNormalizerFactory.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Normalizer/Factory/ObjectNormalizerFactory.php
Outdated
Show resolved
Hide resolved
0c3913b
to
a572179
Compare
src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
Outdated
Show resolved
Hide resolved
a572179
to
fd03ce5
Compare
I fixed all tests related to this PR, last errors are related to |
…on to instantiate objects
…on to instantiate objects symfony#30956
f9e4d11
to
847de48
Compare
src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php
Outdated
Show resolved
Hide resolved
460e883
to
701cd0f
Compare
public function setDenormalizer(DenormalizerInterface $denormalizer) | ||
{ | ||
$this->denormalizer = $denormalizer; | ||
|
||
// Because we need a denormalizer in the Instantiator and we create it in the construct method, it won't get it. | ||
// So we are obliged to overwrite this method in order to give the denormalizer to the Instantiator. | ||
$this->instantiator->setDenormalizer($denormalizer); | ||
} |
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.
This is the strange part. Instantiator
needs a denormalizer to work, this is why this class has the DenormalizerAwareInterface
interface. But since the Instantiator
can be created in the __construct
so it will never receive a denormalizer (the DenormalizerAwareInterface
will be catch either by the Serializer
or by the DIC, here none of them will be triggered).
So in order to still get that denormalizer, we overwrite the setDenormalizer
method here and give the denormalizer to the Instantiator
.
src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
Outdated
Show resolved
Hide resolved
if (!\array_key_exists(Instantiator::INSTANTIATOR_CONSTRUCTOR, $context)) { | ||
$context[Instantiator::INSTANTIATOR_CONSTRUCTOR] = \Closure::fromCallable([$this, 'getConstructor']); | ||
} |
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.
Compatibility layer for the old getConstructor
function
src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
Outdated
Show resolved
Hide resolved
701cd0f
to
224c8c6
Compare
2301613
to
093ce3a
Compare
…on to instantiate objects symfony#30956
093ce3a
to
f6fc79d
Compare
@Korbeil I've had a quick look at the PR, I think the idea behind is good but I have some concerns about the actual implementation (e.g. the instantiator implementing the normalizer interface). I had to tweak a lot the object normalizer for a project at our company and hope to back-port some of it. I'll try to come up with a PoC for it and maybe we can combine our efforts there (although this will probably be later in the month) |
Hey @theofidry, glad to have some feedback on this PR. |
@Korbeil agreed; Checked this on my side, based on our needs I think what is lacking in this PR (although could be introduced progressively) is the extension points for everything the instantiator does:
But I think the main issue here is that things are so inter-dependent at the moment, it requires more elements to be extracted first (e.g. creating of a default context, child context, extracting attributes...) without which you end up with an instantiator-normalizer like you came up with but which in the end is as bloated. |
Based on the latest comment here, I think it's time to close this PR. |
Related to #30818, Replace #30925
This add a new interface and default implementation to instantiate objects
OBJECT_TO_POPULATE was not keep here, as i don't think it should be the responsability of an instantiator to handle that. And if we want to have this responsability we can always add a new implementation with a decoration system.
I try to look at var exporter instantiator also and unfortunetaly @nicolas-grekas i cannot use this, since the behavior of this component is to not use the constructor, in the serializer we want to use it, or at least it used to do that and we cannot change this behavior.
But we can use var exporter implementation in a future PR (will not be the default however just another way of doing it) here if someone does not want to call the constructor.
PS: This PR is following #30956 after main branch moved from
master
to5.x
TODO