简体   繁体   中英

Symfony serializer configure yaml

I have my custom service, eg. 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' #????

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM