0

I have my custom service, e.g.. MyService with serializer

<?php
    
declare(strict_types=1);
    
namespace App\Common;
    
use Symfony\Component\Serializer\SerializerInterface;
    
class MyService
{
    public function __construct(private readonly SerializerInterface $serializer)
    {
    }
}

I need to use serializer like this:

new Serializer(
    normalizers: [
        new ObjectNormalizer(propertyTypeExtractor: new ReflectionExtractor()),
    ],
    encoders: [new JsonEncoder()]
);

How should i configure that via services.yaml ?

    App\Common\MyService:
        arguments:
            $serializer: '@serializer' #????
IMSoP
  • 89,526
  • 13
  • 117
  • 169
mat.twg
  • 438
  • 1
  • 5
  • 11

1 Answers1

0

fixed: i created new serializer component

final class MySerializer extends \Symfony\Component\Serializer\Serializer
{
    public function __construct()
    {
        $normalizers = [
            new ObjectNormalizer(propertyTypeExtractor: new ReflectionExtractor()),
        ];
        $encoders = [new JsonEncoder()];
        parent::__construct($normalizers, $encoders);
    }
}

and inject into my service...

class MyService
{
    public function __construct(private readonly MySerializer $serializer)
    {
    }
}

without configs... looks like symfony style...

mat.twg
  • 438
  • 1
  • 5
  • 11