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

Added support for Protobuf serializer. #31

Merged
merged 5 commits into from
Mar 26, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jobs:
os: >-
['ubuntu-latest']
stability: >-
['prefer-stable']
['prefer-stable', 'prefer-lowest']
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
"spiral/config": "^3.7",
"doctrine/annotations": "^1.12 || ^2.0",
"spiral/serializer": "^3.7",
"symfony/property-info": "^6.4 || ^7.0",
"symfony/serializer": "^6.4 || ^7.0",
"symfony/property-access": "^6.4 || ^7.0"
},
"require-dev": {
"google/protobuf": "^v4.26",
"phpunit/phpunit": "^10.2",
"spiral/testing": "^2.7",
"symfony/yaml": "^6.4 || ^7.0",
Expand All @@ -36,6 +38,8 @@
},
"autoload-dev": {
"psr-4": {
"GPBMetadata\\": "tests/generated/GPBMetadata",
"Service\\": "tests/generated/Service",
"Spiral\\Serializer\\Symfony\\Tests\\App\\": "tests/app",
"Spiral\\Serializer\\Symfony\\Tests\\": "tests/src"
}
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
backupStaticProperties="false">
<testsuites>
<testsuite name="Symfony Serializer Bridge Test Suite">
<directory>./tests/</directory>
<directory>./tests/src/</directory>
</testsuite>
</testsuites>
<coverage>
Expand Down
1 change: 1 addition & 0 deletions src/Bootloader/SerializerBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ private function configureSerializer(
$registry->register('symfony-json', new Serializer($serializer, 'json'));
$registry->register('symfony-csv', new Serializer($serializer, 'csv'));
$registry->register('symfony-xml', new Serializer($serializer, 'xml'));
$registry->register('symfony-proto', new Serializer($serializer, 'proto'));

if (\class_exists(Dumper::class)) {
$registry->register('symfony-yaml', new Serializer($serializer, 'yaml'));
Expand Down
48 changes: 48 additions & 0 deletions src/Encoder/ProtobufEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Spiral\Serializer\Symfony\Encoder;

use Google\Protobuf\Internal\Message;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Exception\RuntimeException;

/**
* This is a proxy class to the ProtobufNormalizer. All the logic of encoding and decoding is in the ProtobufNormalizer.
*/
final class ProtobufEncoder implements EncoderInterface, DecoderInterface
{
public const FORMAT = 'protobuf';
private const ALTERNATIVE_FORMAT = 'proto';

public function __construct()
{
if (!\class_exists(Message::class)) {
throw new RuntimeException('Package `google/protobuf` is not installed.');
}
}

public function decode(string $data, string $format, array $context = []): mixed
{
// All the logic is in the ProtobufNormalizer
return $data;
}

public function encode(mixed $data, string $format, array $context = []): string
{
// All the logic is in the ProtobufNormalizer
return $data;
}

public function supportsDecoding(string $format): bool
{
return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format;
}

public function supportsEncoding(string $format): bool
{
return $this->supportsDecoding($format);
}
}
5 changes: 5 additions & 0 deletions src/EncodersRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Spiral\Serializer\Symfony;

use Spiral\Serializer\Symfony\Encoder\ProtobufEncoder;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Encoder;
use Symfony\Component\Yaml\Dumper;
use Google\Protobuf\Internal\Message;

class EncodersRegistry implements EncodersRegistryInterface
{
Expand Down Expand Up @@ -58,5 +60,8 @@ private function registerDefaultEncoders(): void
if (\class_exists(Dumper::class)) {
$this->register(new Encoder\YamlEncoder());
}
if (\class_exists(Message::class)) {
$this->register(new ProtobufEncoder());
}
}
}
73 changes: 73 additions & 0 deletions src/Normalizer/ProtobufMessageNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Spiral\Serializer\Symfony\Normalizer;

use Google\Protobuf\Internal\Message;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Exception\RuntimeException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class ProtobufMessageNormalizer implements NormalizerInterface, DenormalizerInterface
{
public function __construct()
{
if (!\class_exists(Message::class)) {
throw new RuntimeException('Package `google/protobuf` is not installed.');
}
}

public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
{
\assert(\is_string($data), 'The data must be a string.');
\assert(\is_a($type, Message::class, true), 'The type must be a subclass of Message.');

/**
* @psalm-suppress UnsafeInstantiation
*/
$object = new $type();

try {
$object->mergeFromString($data);
} catch (\Throwable $e) {
throw new NotNormalizableValueException(
\sprintf('The data is not a valid "%s" protobuf binary representation.', $type),
);
}
return $object;
}

public function supportsDenormalization(
mixed $data,
string $type,
?string $format = null,
array $context = [],
): bool {
return \is_string($data) && \is_a($type, Message::class, true);
}

public function normalize(
mixed $object,
?string $format = null,
array $context = [],
): array|string|int|float|bool|\ArrayObject|null {
return $object->serializeToString();
}

public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $data instanceof Message;
}

/**
* @return array<class-string, bool>
*/
public function getSupportedTypes(?string $format): array
{
return [
Message::class => true,
];
}
}
6 changes: 6 additions & 0 deletions src/NormalizersRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use Ramsey\Uuid\UuidInterface;
use Spiral\Boot\Environment\DebugMode;
use Spiral\Serializer\Symfony\Normalizer\ProtobufMessageNormalizer;
use Spiral\Serializer\Symfony\Normalizer\RamseyUuidNormalizer;
use Symfony\Component\Messenger\Transport\Serialization\Normalizer\FlattenExceptionNormalizer;
use Google\Protobuf\Internal\Message;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
Expand Down Expand Up @@ -105,5 +107,9 @@ classMetadataFactory: $factory,
if (\interface_exists(UuidInterface::class)) {
$this->register(new RamseyUuidNormalizer(), 155);
}

if (\class_exists(Message::class)) {
$this->register(new ProtobufMessageNormalizer(), 10);
}
}
}
36 changes: 36 additions & 0 deletions tests/generated/GPBMetadata/Test.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions tests/generated/Service/DetailsMessageForException.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions tests/generated/Service/EmptyMessage.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading