Skip to content

Commit

Permalink
Switch to service locator pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Mar 11, 2024
1 parent 76e753d commit 2114833
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 45 deletions.
17 changes: 16 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
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'
43 changes: 8 additions & 35 deletions src/Service/GenericData/V1/Hydrator/AssetHydratorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
) {
}
Expand All @@ -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);
}
}
20 changes: 11 additions & 9 deletions src/Service/ModelData/V1/Hydrator/AssetHydratorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
}

0 comments on commit 2114833

Please sign in to comment.