8000 [Serializer] Improve ObjectNormalizer performance by dunglas · Pull Request #16547 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

8000 [Serializer] Improve ObjectNormalizer performance #16547

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

Closed
wants to merge 2 commits into from
Closed
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
99 changes: 63 additions & 36 deletions src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
class ObjectNormalizer extends AbstractNormalizer
{
private static $attributesCache = array();

/**
* @var PropertyAccessorInterface
*/
Expand Down Expand Up @@ -58,42 +60,7 @@ public function normalize($object, $format = null, array $context = array())
}

$data = array();
$attributes = $this->getAllowedAttributes($object, $context, true);

// If not using groups, detect manually
if (false === $attributes) {
$attributes = array();

// methods
$reflClass = new \ReflectionClass($object);
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
if (
!$reflMethod->isStatic() &&
!$reflMethod->isConstructor() &&
!$reflMethod->isDestructor() &&
0 === $reflMethod->getNumberOfRequiredParameters()
) {
$name = $reflMethod->getName();

if (strpos($name, 'get') === 0 || strpos($name, 'has') === 0) {
// getters and hassers
$attributes[lcfirst(substr($name, 3))] = true;
} elseif (strpos($name, 'is') === 0) {
// issers
$attributes[lcfirst(substr($name, 2))] = true;
}
}
}

// properties
foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
if (!$reflProperty->isStatic()) {
$attributes[$reflProperty->getName()] = true;
}
}

$attributes = array_keys($attributes);
}
$attributes = $this->getAttributes($object, $context);

foreach ($attributes as $attribute) {
if (in_array($attribute, $this->ignoredAttributes)) {
Expand Down Expand Up @@ -162,4 +129,64 @@ public function denormalize($data, $class, $format = null, array $context = arra

return $object;
}

/**
* Gets and caches attributes for this class and context.
*
* @param object $object
* @param array $context
*
* @return array
*/
private function getAttributes($object, array $context)
{
$key = sprintf('%s-%s', get_class($object), serialize($context));

if (isset(self::$attributesCache[$key])) {
return self::$attributesCache[$key];
}

$allowedAttributes = $this->getAllowedAttributes($object, $context, true);

if (false !== $allowedAttributes) {
return self::$attributesCache[$key] = $allowedAttributes;
}

// If not using groups, detect manually
$attributes = array();

// methods
$reflClass = new \ReflectionClass($object);
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
if (
$reflMethod->getNumberOfRequiredParameters() !== 0 ||
$reflMethod->isStatic() ||
$reflMethod->isConstructor() ||
$reflMethod->isDestructor()
) {
continue;
}

$name = $reflMethod->getName();

if (strpos($name, 'get') === 0 || strpos($name, 'has') === 0) {
// getters and hassers
$attributes[lcfirst(substr($name, 3))] = true;
} elseif (strpos($name, 'is') === 0) {
// issers
$attributes[lcfirst(substr($name, 2))] = true;
}
}

// properties
foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
if ($reflProperty->isStatic()) {
continue;
}

$attributes[$reflProperty->getName()] = true;
}

return self::$attributesCache[$key] = array_keys($attributes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ObjectNormalizerTest
* @var ObjectNormalizer
*/
private $normalizer;
/**
Expand Down Expand Up @@ -239,6 +239,18 @@ public function testGroupsDenormalize()
$this->assertEquals($obj, $normalized);
}

public function testNormalizeNoPropertyInGroup()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$this->normalizer = new ObjectNormalizer($classMetadataFactory);
$this->normalizer->setSerializer($this->serializer);

$obj = new GroupDummy();
$obj->setFoo('foo');

$this->assertEquals(array(), $this->normalizer->normalize($obj, null, array('groups' => array('notExist'))));
}

public function testGroupsNormalizeWithNameConverter()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
Expand Down
0