$classMetadataFactory = new CacheClassMetadataFactory(
new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())),
new ApcuAdapter('SymfonyMetadata')
);
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
->setCacheItemPool(new ApcuAdapter('SymfonyPropertyAccessor'))
->getPropertyAccessor();
$reflectionExtractor = new ReflectionExtractor();
$phpDocExtractor = new PhpDocExtractor();
$propertyTypeExtractor = new PropertyInfoExtractor(
[$reflectionExtractor],
[$phpDocExtractor, $reflectionExtractor],
[$phpDocExtractor],
[$reflectionExtractor],
[$reflectionExtractor]
);
$propertyTypeExtractor = new PropertyInfoCacheExtractor($propertyTypeExtractor, new ApcuAdapter('SymfonyPropertyAccessor'));
$normalizer = new ObjectNormalizer(
$classMetadataFactory,
null,
$propertyAccessor,
$propertyTypeExtractor
);
$arrayNormalizer = new ArrayDenormalizer();
$serializer = new Serializer([$arrayNormalizer, $normalizer]);
return $serializer->denormalize($array, $className);
$array = уже готовый массив с данными.
className = название класса
$reflectionExtractor = new ReflectionExtractor();
$phpDocExtractor = new PhpDocExtractor();
$propertyTypeExtractor = new PropertyInfoExtractor(
[$reflectionExtractor],
[$phpDocExtractor, $reflectionExtractor],
[$phpDocExtractor],
[$reflectionExtractor],
[$reflectionExtractor]
);
$normalizer = new ObjectNormalizer(
null,
null,
null,
$propertyTypeExtractor
);
$arrayNormalizer = new ArrayDenormalizer();
$serializer = new Serializer([$arrayNormalizer, $normalizer]);
return $serializer->denormalize($array, $className);
$normalizer = new ObjectNormalizer(
null,
null,
null,
new ReflectionExtractor()
);
$arrayNormalizer = new ArrayDenormalizer();
$serializer = new Serializer([$arrayNormalizer, $normalizer]);
return $serializer->denormalize($array, $className);
$reflectionExtractor = new ReflectionExtractor();
$phpDocExtractor = new PhpDocExtractor();
$propertyTypeExtractor = new PropertyInfoExtractor(
[$reflectionExtractor],
[$phpDocExtractor, $reflectionExtractor],
[$phpDocExtractor],
[$reflectionExtractor],
[$reflectionExtractor]
);
Performance¶
To figure which normalizer (or denormalizer) must be used to handle an object, the Serializer class will call the supportsNormalization() (or supportsDenormalization()) of all registered normalizers (or denormalizers) in a loop.
The result of these methods can vary depending on the object to serialize, the format and the context. That's why the result is not cached by default and can result in a significant performance bottleneck.
However, most normalizers (and denormalizers) always return the same result when the object's type and the format are the same, so the result can be cached. To do so, make those normalizers (and denormalizers) implement the CacheableSupportsMethodInterface and return true when hasCacheableSupportsMethod() is called.
Если запустить composer test, то будет видно как оно медленно работает.
Файл
В коде можно поискать вот так SerializeHelper::generateModelFromData, попадете в тесты и там есть пример настоящих данных, на которых я все проверяю.