From e83cd273e59fe084481c2898cfa28a0890171912 Mon Sep 17 00:00:00 2001 From: mattamon Date: Mon, 11 Mar 2024 14:24:06 +0100 Subject: [PATCH] Add service locator instead of match --- config/services.yaml | 19 ++++++- .../V1/Hydrator/AssetHydratorService.php | 55 ++++--------------- 2 files changed, 28 insertions(+), 46 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index 19330252c..83bf2f682 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -73,6 +73,8 @@ services: # Services Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\AssetHydratorServiceInterface: class: Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\AssetHydratorService + arguments: + $assetHydratorLocator: '@generic_data_index.asset_hydrator.service_locator' Pimcore\Bundle\StudioApiBundle\Service\AssetSearchServiceInterface: class: Pimcore\Bundle\StudioApiBundle\Service\AssetSearchService @@ -108,4 +110,19 @@ services: #Decorators Pimcore\Bundle\StudioApiBundle\ApiPlatform\OpenApiFactoryDecorator: - decorates: 'api_platform.openapi.factory' \ No newline at end of file + decorates: 'api_platform.openapi.factory' + + #Service Locator + generic_data_index.asset_hydrator.service_locator: + class: Symfony\Component\DependencyInjection\ServiceLocator + tags: ['container.service_locator'] + arguments: + - + Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Archive: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\ArchiveHydratorInterface' + Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Audio: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\AudioHydratorInterface' + Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Document: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\DocumentHydratorInterface' + Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Folder: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\FolderHydratorInterface' + Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Image: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\ImageHydratorInterface' + Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Text: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\TextHydratorInterface' + Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Unknown: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\UnknownHydratorInterface' + Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Video: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\VideoHydratorInterface' diff --git a/src/Service/GenericData/V1/Hydrator/AssetHydratorService.php b/src/Service/GenericData/V1/Hydrator/AssetHydratorService.php index 5a473e9f5..e9173798b 100644 --- a/src/Service/GenericData/V1/Hydrator/AssetHydratorService.php +++ b/src/Service/GenericData/V1/Hydrator/AssetHydratorService.php @@ -17,43 +17,13 @@ namespace Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator; use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\AssetSearchResultItem; -use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Archive as ArchiveItem; -use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Audio as AudioItem; -use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Document as DocumentItem; -use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Folder as FolderItem; -use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Image as ImageItem; -use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Text as TextItem; -use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Unknown as UnknownItem; -use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Video as VideoItem; use Pimcore\Bundle\StudioApiBundle\Dto\Asset; -use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Archive; -use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Audio; -use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Document; -use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Folder; -use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Image; -use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Text; -use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Unknown; -use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Video; -use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\ArchiveHydratorInterface; -use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\AudioHydratorInterface; -use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\DocumentHydratorInterface; -use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\FolderHydratorInterface; -use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\ImageHydratorInterface; -use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\TextHydratorInterface; -use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\UnknownHydratorInterface; -use Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\VideoHydratorInterface; +use Symfony\Contracts\Service\ServiceProviderInterface; final readonly class AssetHydratorService implements AssetHydratorServiceInterface { public function __construct( - private ArchiveHydratorInterface $archiveHydrator, - private AudioHydratorInterface $audioHydrator, - private DocumentHydratorInterface $documentHydrator, - private FolderHydratorInterface $folderHydrator, - private ImageHydratorInterface $imageHydrator, - private TextHydratorInterface $textHydrator, - private UnknownHydratorInterface $unknownHydrator, - private VideoHydratorInterface $videoHydrator, + private ServiceProviderInterface $assetHydratorLocator, private AssetHydratorInterface $assetHydrator ) { } @@ -61,20 +31,15 @@ public function __construct( /** * @param AssetSearchResultItem $item * - * @return Asset|Archive|Audio|Document|Folder|Image|Text|Unknown|Video + * @return Asset */ - public function hydrate(AssetSearchResultItem $item): Asset|Archive|Audio|Document|Folder|Image|Text|Unknown|Video + public function hydrate(AssetSearchResultItem $item): Asset { - return match (true) { - $item instanceof ArchiveItem => $this->archiveHydrator->hydrate($item), - $item instanceof AudioItem => $this->audioHydrator->hydrate($item), - $item instanceof DocumentItem => $this->documentHydrator->hydrate($item), - $item instanceof FolderItem => $this->folderHydrator->hydrate($item), - $item instanceof ImageItem => $this->imageHydrator->hydrate($item), - $item instanceof TextItem => $this->textHydrator->hydrate($item), - $item instanceof UnknownItem => $this->unknownHydrator->hydrate($item), - $item instanceof VideoItem => $this->videoHydrator->hydrate($item), - default => $this->assetHydrator->hydrate($item) - }; + $class = get_class($item); + if($this->assetHydratorLocator->has($class)) { + return $this->assetHydratorLocator->get($class)->hydrate($item); + } + + return $this->assetHydrator->hydrate($item); } }