From 21148336a24f22937031ca7b46e5bc47c55cf603 Mon Sep 17 00:00:00 2001 From: mattamon Date: Mon, 11 Mar 2024 09:21:38 +0100 Subject: [PATCH] Switch to service locator pattern --- config/services.yaml | 17 +++++++- .../V1/Hydrator/AssetHydratorService.php | 43 ++++--------------- .../V1/Hydrator/AssetHydratorService.php | 20 +++++---- 3 files changed, 35 insertions(+), 45 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index 9563b2057..648beddff 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -74,6 +74,8 @@ services: # Services Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\AssetHydratorServiceInterface: class: Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\AssetHydratorService + arguments: + - '@generic_data_index.asset_hydrator.service_locator' Pimcore\Bundle\StudioApiBundle\Service\AssetSearchServiceInterface: class: Pimcore\Bundle\StudioApiBundle\Service\AssetSearchService @@ -118,6 +120,19 @@ services: # Model Hydrators Pimcore\Bundle\StudioApiBundle\Service\ModelData\V1\Hydrator\AssetHydratorServiceInterface: class: Pimcore\Bundle\StudioApiBundle\Service\ModelData\V1\Hydrator\AssetHydratorService + arguments: + - '@pimcore_model_data.asset_hydrator.service_locator' Pimcore\Bundle\StudioApiBundle\Service\ModelData\V1\Hydrator\Asset\ImageHydratorInterface: - class: Pimcore\Bundle\StudioApiBundle\Service\ModelData\V1\Hydrator\Asset\ImageHydrator \ No newline at end of file + class: Pimcore\Bundle\StudioApiBundle\Service\ModelData\V1\Hydrator\Asset\ImageHydrator + + #Service Locator + generic_data_index.asset_hydrator.service_locator: + class: Symfony\Component\DependencyInjection\ServiceLocator + arguments: + - Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Image: '@Pimcore\Bundle\StudioApiBundle\Service\GenericData\V1\Hydrator\Asset\ImageHydratorInterface' + + pimcore_model_data.asset_hydrator.service_locator: + class: Symfony\Component\DependencyInjection\ServiceLocator + arguments: + - Pimcore\Model\Asset\Image: '@Pimcore\Bundle\StudioApiBundle\Service\ModelData\V1\Hydrator\Asset\ImageHydratorInterface' \ No newline at end of file diff --git a/src/Service/GenericData/V1/Hydrator/AssetHydratorService.php b/src/Service/GenericData/V1/Hydrator/AssetHydratorService.php index 5a473e9f5..3f5c41350 100644 --- a/src/Service/GenericData/V1/Hydrator/AssetHydratorService.php +++ b/src/Service/GenericData/V1/Hydrator/AssetHydratorService.php @@ -17,14 +17,6 @@ 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; @@ -34,26 +26,12 @@ 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 ) { } @@ -65,16 +43,11 @@ public function __construct( */ public function hydrate(AssetSearchResultItem $item): Asset|Archive|Audio|Document|Folder|Image|Text|Unknown|Video { - 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); } } diff --git a/src/Service/ModelData/V1/Hydrator/AssetHydratorService.php b/src/Service/ModelData/V1/Hydrator/AssetHydratorService.php index 411c9f048..0c82c58f6 100644 --- a/src/Service/ModelData/V1/Hydrator/AssetHydratorService.php +++ b/src/Service/ModelData/V1/Hydrator/AssetHydratorService.php @@ -16,7 +16,6 @@ namespace Pimcore\Bundle\StudioApiBundle\Service\ModelData\V1\Hydrator; -use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\AssetSearchResultItem; use Pimcore\Bundle\StudioApiBundle\Dto\Asset; use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Archive; use Pimcore\Bundle\StudioApiBundle\Dto\Asset\Audio; @@ -26,25 +25,28 @@ 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\ModelData\V1\Hydrator\Asset\ImageHydratorInterface; +use Pimcore\Model\Asset as ModelAsset; +use Symfony\Contracts\Service\ServiceProviderInterface; + final readonly class AssetHydratorService implements AssetHydratorServiceInterface { public function __construct( - private ImageHydratorInterface $imageHydrator, + private ServiceProviderInterface $assetHydratorLocator, ) { } /** - * @param AssetSearchResultItem $item + * @param ModelAsset $item * * @return Asset|Archive|Audio|Document|Folder|Image|Text|Unknown|Video */ - public function hydrate(\Pimcore\Model\Asset $item): Asset|Archive|Audio|Document|Folder|Image|Text|Unknown|Video + public function hydrate(ModelAsset $item): Asset|Archive|Audio|Document|Folder|Image|Text|Unknown|Video { - return match (true) { - $item instanceof \Pimcore\Model\Asset\Image => $this->imageHydrator->hydrate($item), - default => null - }; + $class = get_class($item); + if($this->assetHydratorLocator->has($class)) { + return $this->assetHydratorLocator->get($class)->hydrate($item); + } + return new Asset($item->getId()); } }