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

Allow user to extend initialization #35

Merged
merged 1 commit into from
Aug 30, 2022
Merged

Conversation

roquie
Copy link
Contributor

@roquie roquie commented Aug 30, 2022

This PR allows user to override initialization methods in TemporalBridgeBootloader bootloader. For example, it can be useful for extending temporal sdk data converters.

Usage example:

First step

Note: In samples I use PHP 8.1

Create a SymfonyConverter class with following contents:

<?php

declare(strict_types=1);

namespace Library\TemporalSdkObjectConverter;

use Symfony\Component\Serializer\Encoder\JsonDecode;
use Symfony\Component\Serializer\Encoder\JsonEncode;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
use Temporal\Api\Common\V1\Payload;
use Temporal\DataConverter\Converter;
use Temporal\DataConverter\Type;
use Temporal\Exception\DataConverterException;

class SymfonyConverter extends Converter
{
    public function __construct(
        private readonly SerializerInterface $serializer = new Serializer(normalizers: [
            new BackedEnumNormalizer(),
            new ObjectNormalizer(),
        ], encoders: [
            new JsonEncode(),
            new JsonDecode()
        ])
    ) {}

    public function getEncodingType(): string
    {
        return 'json/object';
    }

    public function toPayload(mixed $value): ?Payload
    {
        if (!\is_object($value)) {
            return null;
        }

        return $this->create(
            $this->serializer->serialize($value, 'json')
        );
    }

    public function fromPayload(Payload $payload, Type $type): object
    {
        if (!$type->isClass()) {
            throw new DataConverterException('Unable to decode value using object converter.');
        }

        try {
            return $this->serializer->deserialize($payload->getData(), $type->getName(), 'json');
        } catch (ExceptionInterface $e) {
            throw new DataConverterException($e->getMessage(), $e->getCode(), $e);
        }
    }
}

Second step

To register a custom DataConverter within Spiral framework you can extend from TemporalBridgeBootloader bootloader and override initWorkerFactory method:

use Spiral\TemporalBridge\Bootloader\CustomTemporalDataConverter;
use Temporal\DataConverter\BinaryConverter;
use Temporal\DataConverter\DataConverter;
use Temporal\DataConverter\JsonConverter;
use Temporal\DataConverter\NullConverter;
use Temporal\DataConverter\ProtoJsonConverter;
use Library\TemporalSdkObjectConverter\SymfonyConverter;

class CustomTemporalDataConverter extends TemporalBridgeBootloader 
{
      protected function initWorkerFactory(): WorkerFactoryInterface
      {
          return new WorkerFactory(
              new DataConverter(
                  new NullConverter(),
                  new BinaryConverter(),
                  new ProtoJsonConverter(),
                  new SymfonyConverter(),
                  new JsonConverter(),
              ),
              Goridge::create()
        );
      }
}

We added here a SymfonyConverter which do (de-)serialize PHP objects.

P.S. Don't forget register the bootloader.

Now you can pass PHP objects arguments to activity/workflow methods.

@butschster butschster added the enhancement New feature or request label Aug 30, 2022
@butschster butschster changed the base branch from master to 2.0 August 30, 2022 15:31
@butschster butschster changed the base branch from 2.0 to master August 30, 2022 15:31
@butschster butschster changed the base branch from master to 2.0 August 30, 2022 15:32
@butschster butschster changed the base branch from 2.0 to master August 30, 2022 15:35
@butschster butschster changed the base branch from master to 2.0 August 30, 2022 15:35
@butschster butschster merged commit b5896ee into spiral:2.0 Aug 30, 2022
@butschster butschster self-assigned this Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants