Подготовка нового резолвера сервисов

master
Rinsvent 3 years ago
parent 478cf10ea7
commit cf4961ff7c
  1. 13
      src/Resources/config/services.yaml
  2. 13
      src/Service/Transformer/AbstractTransformer.php
  3. 15
      src/Service/Transformer/Entity.php
  4. 26
      src/Service/Transformer/EntityTransformer.php
  5. 21
      src/Service/Transformer/ServiceResolver.php

@ -1,5 +1,18 @@
services:
_defaults:
autoconfigure: true
public: false
autowire: true
Rinsvent\RequestBundle\EventListener\RequestListener:
autowire: true
tags:
- { name: kernel.event_listener, event: kernel.request }
Rinsvent\RequestBundle\Service\Transformer\ServiceResolver:
bind:
$transformerLocator: !tagged_locator { tag: 'rinsvent.transformer', index_by: 'key', default_index_method: 'getLocatorKey' }
_instanceof:
Rinsvent\RequestBundle\Service\Transformer\AbstractTransformer:
tags: [ 'rinsvent.transformer' ]

@ -0,0 +1,13 @@
<?php
namespace Rinsvent\RequestBundle\Service\Transformer;
use Rinsvent\Data2DTO\Transformer\TransformerInterface;
abstract class AbstractTransformer implements TransformerInterface
{
public static function getLocatorKey()
{
return static::class;
}
}

@ -0,0 +1,15 @@
<?php
namespace Rinsvent\RequestBundle\Service\Transformer;
use Rinsvent\Data2DTO\Transformer\Meta;
#[\Attribute]
class Entity extends Meta
{
public const TYPE = 'service';
public function __construct(
public string $class
) {}
}

@ -0,0 +1,26 @@
<?php
namespace Rinsvent\RequestBundle\Service\Transformer;
use Doctrine\ORM\EntityManagerInterface;
use Rinsvent\Data2DTO\Transformer\Meta;
class EntityTransformer extends AbstractTransformer
{
public function __construct(
protected EntityManagerInterface $em
) {}
/**
* @param $data
* @param Entity $meta
*/
public function transform(&$data, Meta $meta): void
{
if (!is_int($data)) {
return;
}
$repository = $this->em->getRepository($meta->class);
$data = $repository->find((int)$data);
}
}

@ -0,0 +1,21 @@
<?php
namespace Rinsvent\RequestBundle\Service\Transformer;
use Rinsvent\Data2DTO\Resolver\TransformerResolverInterface;
use Rinsvent\Data2DTO\Transformer\Meta;
use Rinsvent\Data2DTO\Transformer\TransformerInterface;
use Symfony\Component\DependencyInjection\ServiceLocator;
class ServiceResolver implements TransformerResolverInterface
{
public function __construct(
private ServiceLocator $transformerLocator
) {}
public function resolve(Meta $meta): TransformerInterface
{
$transformerClass = $meta::class . 'Transformer';
return $this->transformerLocator->get($transformerClass);
}
}
Loading…
Cancel
Save