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

Generated hydrators #933

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"jms/metadata": "~1.1",
"doctrine/annotations": "^1.0",
"doctrine/instantiator": "^1.0.3",
"hoa/compiler": "^3.17.08.08"
"hoa/compiler": "^3.17.08.08",
"ocramius/generated-hydrator": "^2.2"
},
"suggest": {
"symfony/yaml": "Required if you'd like to use the YAML metadata format.",
Expand Down
2 changes: 1 addition & 1 deletion src/Accessor/AccessorStrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface AccessorStrategyInterface
* @param PropertyMetadata $metadata
* @return mixed
*/
public function getValue(object $object, PropertyMetadata $metadata);
public function getValue(object $object, PropertyMetadata $metadata, $context);

/**
* @param object $object
Expand Down
28 changes: 27 additions & 1 deletion src/Accessor/DefaultAccessorStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,47 @@

namespace JMS\Serializer\Accessor;

use GeneratedHydrator\Configuration;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\PropertyMetadata;

/**
* @author Asmir Mustafic <[email protected]>
*/
final class DefaultAccessorStrategy implements AccessorStrategyInterface
{
private $gen = array();

public function getValue(object $object, PropertyMetadata $metadata)
public function getValue(object $object, PropertyMetadata $metadata, $context)
{

if (!$metadata->getter && array_key_exists($metadata->name, $context)) {
return $context[$metadata->name];
}

return $metadata->getValue($object);
}

public function setValue(object $object, $value, PropertyMetadata $metadata): void
{
$metadata->setValue($object, $value);
}

public function startAccessing(object $object, ClassMetadata $metadata)
{
$class = get_class($object);

if (!isset($this->gen[$class])) {
$config = new Configuration($class);
$hydratorClass = $config->createFactory()->getHydratorClass();
$this->gen[$class] = new $hydratorClass();
}

return $this->gen[$class]->extract($object);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@Majkl578 generators are generated and cached, so is "production" ready after the warmup.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The code inside the "if" is never executed during the benchmark, so should not make it slower

}

public function endAccessing(object $object, ClassMetadata $metadata, $context)
{

}
}
2 changes: 1 addition & 1 deletion src/Accessor/ExpressionAccessorStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(ExpressionEvaluatorInterface $evaluator, AccessorStr
$this->evaluator = $evaluator;
}

public function getValue(object $object, PropertyMetadata $metadata)
public function getValue(object $object, PropertyMetadata $metadata, $context)
{
if ($metadata instanceof ExpressionPropertyMetadata) {
return $this->evaluator->evaluate($metadata->expression, ['object' => $object]);
Expand Down
7 changes: 6 additions & 1 deletion src/GraphNavigator/SerializationGraphNavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ public function accept($data, array $type = null)
}

$this->visitor->startVisitingObject($metadata, $data, $type);

$context = $this->accessor->startAccessing($data, $metadata);

foreach ($metadata->propertyMetadata as $propertyMetadata) {
if ($this->exclusionStrategy->shouldSkipProperty($propertyMetadata, $this->context)) {
continue;
Expand All @@ -231,7 +234,7 @@ public function accept($data, array $type = null)
continue;
}

$v = $this->accessor->getValue($data, $propertyMetadata);
$v = $this->accessor->getValue($data, $propertyMetadata, $context);

if (null === $v && $this->shouldSerializeNull !== true) {
continue;
Expand All @@ -242,6 +245,8 @@ public function accept($data, array $type = null)
$this->context->popPropertyMetadata();
}

$this->accessor->endAccessing($data, $metadata, $context);

$this->afterVisitingObject($metadata, $data, $type);

return $this->visitor->endVisitingObject($metadata, $data, $type);
Expand Down