Подготовка нового резолвера сервисов
This commit is contained in:
parent
478cf10ea7
commit
cf4961ff7c
@ -1,5 +1,18 @@
|
|||||||
services:
|
services:
|
||||||
|
_defaults:
|
||||||
|
autoconfigure: true
|
||||||
|
public: false
|
||||||
|
autowire: true
|
||||||
|
|
||||||
Rinsvent\RequestBundle\EventListener\RequestListener:
|
Rinsvent\RequestBundle\EventListener\RequestListener:
|
||||||
autowire: true
|
autowire: true
|
||||||
tags:
|
tags:
|
||||||
- { name: kernel.event_listener, event: kernel.request}
|
- { 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' ]
|
||||||
|
13
src/Service/Transformer/AbstractTransformer.php
Normal file
13
src/Service/Transformer/AbstractTransformer.php
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
15
src/Service/Transformer/Entity.php
Normal file
15
src/Service/Transformer/Entity.php
Normal file
@ -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
|
||||||
|
) {}
|
||||||
|
}
|
26
src/Service/Transformer/EntityTransformer.php
Normal file
26
src/Service/Transformer/EntityTransformer.php
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
21
src/Service/Transformer/ServiceResolver.php
Normal file
21
src/Service/Transformer/ServiceResolver.php
Normal file
@ -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…
Reference in New Issue
Block a user