127 lines
7.1 KiB
YAML
127 lines
7.1 KiB
YAML
name: Serializer
|
|
class_comment: '# * @author Samuel Roze <samuel.roze@gmail.com>'
|
|
dependencies:
|
|
- name: Envelope
|
|
type: class
|
|
source: Symfony\Component\Messenger\Envelope
|
|
- name: LogicException
|
|
type: class
|
|
source: Symfony\Component\Messenger\Exception\LogicException
|
|
- name: MessageDecodingFailedException
|
|
type: class
|
|
source: Symfony\Component\Messenger\Exception\MessageDecodingFailedException
|
|
- name: NonSendableStampInterface
|
|
type: class
|
|
source: Symfony\Component\Messenger\Stamp\NonSendableStampInterface
|
|
- name: SerializedMessageStamp
|
|
type: class
|
|
source: Symfony\Component\Messenger\Stamp\SerializedMessageStamp
|
|
- name: SerializerStamp
|
|
type: class
|
|
source: Symfony\Component\Messenger\Stamp\SerializerStamp
|
|
- name: StampInterface
|
|
type: class
|
|
source: Symfony\Component\Messenger\Stamp\StampInterface
|
|
- name: JsonEncoder
|
|
type: class
|
|
source: Symfony\Component\Serializer\Encoder\JsonEncoder
|
|
- name: XmlEncoder
|
|
type: class
|
|
source: Symfony\Component\Serializer\Encoder\XmlEncoder
|
|
- name: ExceptionInterface
|
|
type: class
|
|
source: Symfony\Component\Serializer\Exception\ExceptionInterface
|
|
- name: ArrayDenormalizer
|
|
type: class
|
|
source: Symfony\Component\Serializer\Normalizer\ArrayDenormalizer
|
|
- name: DateTimeNormalizer
|
|
type: class
|
|
source: Symfony\Component\Serializer\Normalizer\DateTimeNormalizer
|
|
- name: ObjectNormalizer
|
|
type: class
|
|
source: Symfony\Component\Serializer\Normalizer\ObjectNormalizer
|
|
- name: SymfonySerializer
|
|
type: class
|
|
source: Symfony\Component\Serializer\Serializer
|
|
- name: SymfonySerializerInterface
|
|
type: class
|
|
source: Symfony\Component\Serializer\SerializerInterface
|
|
properties: []
|
|
methods:
|
|
- name: findFirstSerializerStamp
|
|
visibility: private
|
|
parameters:
|
|
- name: stamps
|
|
comment: "# * @author Samuel Roze <samuel.roze@gmail.com>\n# */\n# class Serializer\
|
|
\ implements SerializerInterface\n# {\n# public const MESSENGER_SERIALIZATION_CONTEXT\
|
|
\ = 'messenger_serialization';\n# private const STAMP_HEADER_PREFIX = 'X-Message-Stamp-';\n\
|
|
# \n# private SymfonySerializerInterface $serializer;\n# \n# public function __construct(\n\
|
|
# ?SymfonySerializerInterface $serializer = null,\n# private string $format =\
|
|
\ 'json',\n# private array $context = [],\n# ) {\n# $this->serializer = $serializer\
|
|
\ ?? self::create()->serializer;\n# $this->context += [self::MESSENGER_SERIALIZATION_CONTEXT\
|
|
\ => true];\n# }\n# \n# public static function create(): self\n# {\n# if (!class_exists(SymfonySerializer::class))\
|
|
\ {\n# throw new LogicException(\\sprintf('The \"%s\" class requires Symfony\\\
|
|
's Serializer component. Try running \"composer require symfony/serializer\" or\
|
|
\ use \"%s\" instead.', __CLASS__, PhpSerializer::class));\n# }\n# \n# $encoders\
|
|
\ = [new XmlEncoder(), new JsonEncoder()];\n# $normalizers = [new DateTimeNormalizer(),\
|
|
\ new ArrayDenormalizer(), new ObjectNormalizer()];\n# $serializer = new SymfonySerializer($normalizers,\
|
|
\ $encoders);\n# \n# return new self($serializer);\n# }\n# \n# public function\
|
|
\ decode(array $encodedEnvelope): Envelope\n# {\n# if (empty($encodedEnvelope['body'])\
|
|
\ || empty($encodedEnvelope['headers'])) {\n# throw new MessageDecodingFailedException('Encoded\
|
|
\ envelope should have at least a \"body\" and some \"headers\", or maybe you\
|
|
\ should implement your own serializer.');\n# }\n# \n# if (empty($encodedEnvelope['headers']['type']))\
|
|
\ {\n# throw new MessageDecodingFailedException('Encoded envelope does not have\
|
|
\ a \"type\" header.');\n# }\n# \n# $stamps = $this->decodeStamps($encodedEnvelope);\n\
|
|
# $stamps[] = new SerializedMessageStamp($encodedEnvelope['body']);\n# \n# $serializerStamp\
|
|
\ = $this->findFirstSerializerStamp($stamps);\n# \n# $context = $this->context;\n\
|
|
# if (null !== $serializerStamp) {\n# $context = $serializerStamp->getContext()\
|
|
\ + $context;\n# }\n# \n# try {\n# $message = $this->serializer->deserialize($encodedEnvelope['body'],\
|
|
\ $encodedEnvelope['headers']['type'], $this->format, $context);\n# } catch (ExceptionInterface\
|
|
\ $e) {\n# throw new MessageDecodingFailedException('Could not decode message:\
|
|
\ '.$e->getMessage(), $e->getCode(), $e);\n# }\n# \n# return new Envelope($message,\
|
|
\ $stamps);\n# }\n# \n# public function encode(Envelope $envelope): array\n# {\n\
|
|
# $context = $this->context;\n# /** @var SerializerStamp|null $serializerStamp\
|
|
\ */\n# if ($serializerStamp = $envelope->last(SerializerStamp::class)) {\n# $context\
|
|
\ = $serializerStamp->getContext() + $context;\n# }\n# \n# /** @var SerializedMessageStamp|null\
|
|
\ $serializedMessageStamp */\n# $serializedMessageStamp = $envelope->last(SerializedMessageStamp::class);\n\
|
|
# \n# $envelope = $envelope->withoutStampsOfType(NonSendableStampInterface::class);\n\
|
|
# \n# $headers = ['type' => $envelope->getMessage()::class] + $this->encodeStamps($envelope)\
|
|
\ + $this->getContentTypeHeader();\n# \n# return [\n# 'body' => $serializedMessageStamp\n\
|
|
# ? $serializedMessageStamp->getSerializedMessage()\n# : $this->serializer->serialize($envelope->getMessage(),\
|
|
\ $this->format, $context),\n# 'headers' => $headers,\n# ];\n# }\n# \n# private\
|
|
\ function decodeStamps(array $encodedEnvelope): array\n# {\n# $stamps = [];\n\
|
|
# foreach ($encodedEnvelope['headers'] as $name => $value) {\n# if (!str_starts_with($name,\
|
|
\ self::STAMP_HEADER_PREFIX)) {\n# continue;\n# }\n# \n# try {\n# $stamps[] =\
|
|
\ $this->serializer->deserialize($value, substr($name, \\strlen(self::STAMP_HEADER_PREFIX)).'[]',\
|
|
\ $this->format, $this->context);\n# } catch (ExceptionInterface $e) {\n# throw\
|
|
\ new MessageDecodingFailedException('Could not decode stamp: '.$e->getMessage(),\
|
|
\ $e->getCode(), $e);\n# }\n# }\n# if ($stamps) {\n# $stamps = array_merge(...$stamps);\n\
|
|
# }\n# \n# return $stamps;\n# }\n# \n# private function encodeStamps(Envelope\
|
|
\ $envelope): array\n# {\n# if (!$allStamps = $envelope->all()) {\n# return [];\n\
|
|
# }\n# \n# $headers = [];\n# foreach ($allStamps as $class => $stamps) {\n# $headers[self::STAMP_HEADER_PREFIX.$class]\
|
|
\ = $this->serializer->serialize($stamps, $this->format, $this->context);\n# }\n\
|
|
# \n# return $headers;\n# }\n# \n# /**\n# * @param StampInterface[] $stamps"
|
|
- name: getContentTypeHeader
|
|
visibility: private
|
|
parameters: []
|
|
comment: null
|
|
- name: getMimeTypeForFormat
|
|
visibility: private
|
|
parameters: []
|
|
comment: null
|
|
traits:
|
|
- Symfony\Component\Messenger\Envelope
|
|
- Symfony\Component\Messenger\Exception\LogicException
|
|
- Symfony\Component\Messenger\Exception\MessageDecodingFailedException
|
|
- Symfony\Component\Messenger\Stamp\NonSendableStampInterface
|
|
- Symfony\Component\Messenger\Stamp\SerializedMessageStamp
|
|
- Symfony\Component\Messenger\Stamp\SerializerStamp
|
|
- Symfony\Component\Messenger\Stamp\StampInterface
|
|
- Symfony\Component\Serializer\Encoder\JsonEncoder
|
|
- Symfony\Component\Serializer\Encoder\XmlEncoder
|
|
- Symfony\Component\Serializer\Exception\ExceptionInterface
|
|
- Symfony\Component\Serializer\Normalizer\ArrayDenormalizer
|
|
- Symfony\Component\Serializer\Normalizer\DateTimeNormalizer
|
|
- Symfony\Component\Serializer\Normalizer\ObjectNormalizer
|
|
interfaces:
|
|
- SerializerInterface
|