vendor/ezsystems/ezplatform-kernel/eZ/Publish/Core/Repository/ProxyFactory/ProxyDomainMapper.php line 186

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace eZ\Publish\Core\Repository\ProxyFactory;
  8. use eZ\Publish\API\Repository\Repository;
  9. use eZ\Publish\API\Repository\Values\Content\Content;
  10. use eZ\Publish\API\Repository\Values\Content\ContentInfo;
  11. use eZ\Publish\API\Repository\Values\Content\Language;
  12. use eZ\Publish\API\Repository\Values\Content\Location;
  13. use eZ\Publish\API\Repository\Values\Content\Section;
  14. use eZ\Publish\API\Repository\Values\ContentType\ContentType;
  15. use eZ\Publish\API\Repository\Values\ContentType\ContentTypeGroup;
  16. use eZ\Publish\API\Repository\Values\User\User;
  17. use ProxyManager\Proxy\LazyLoadingInterface;
  18. /**
  19.  * @internal
  20.  */
  21. final class ProxyDomainMapper implements ProxyDomainMapperInterface
  22. {
  23.     /** @var \eZ\Publish\API\Repository\Repository */
  24.     private $repository;
  25.     /** @var \ProxyManager\Factory\LazyLoadingValueHolderFactory */
  26.     private $proxyGenerator;
  27.     public function __construct(Repository $repositoryProxyGeneratorInterface $proxyGenerator)
  28.     {
  29.         $this->repository $repository;
  30.         $this->proxyGenerator $proxyGenerator;
  31.     }
  32.     public function createContentProxy(
  33.         int $contentId,
  34.         array $prioritizedLanguages Language::ALL,
  35.         bool $useAlwaysAvailable true
  36.     ): Content {
  37.         $initializer = function (
  38.             &$wrappedObjectLazyLoadingInterface $proxy$method, array $parameters, &$initializer
  39.         ) use ($contentId$prioritizedLanguages$useAlwaysAvailable): bool {
  40.             $initializer null;
  41.             $wrappedObject $this->repository->getContentService()->loadContent(
  42.                 $contentId,
  43.                 $prioritizedLanguages,
  44.                 null,
  45.                 $useAlwaysAvailable
  46.             );
  47.             return true;
  48.         };
  49.         return $this->proxyGenerator->createProxy(Content::class, $initializer);
  50.     }
  51.     public function createContentInfoProxy(int $contentId): ContentInfo
  52.     {
  53.         $initializer = function (
  54.             &$wrappedObjectLazyLoadingInterface $proxy$method, array $parameters, &$initializer
  55.         ) use ($contentId): bool {
  56.             $initializer null;
  57.             $wrappedObject $this->repository->getContentService()->loadContentInfo(
  58.                 $contentId
  59.             );
  60.             return true;
  61.         };
  62.         return $this->proxyGenerator->createProxy(ContentInfo::class, $initializer);
  63.     }
  64.     public function createContentTypeProxy(
  65.         int $contentTypeId,
  66.         array $prioritizedLanguages Language::ALL
  67.     ): ContentType {
  68.         $initializer = function (
  69.             &$wrappedObjectLazyLoadingInterface $proxy$method, array $parameters, &$initializer
  70.         ) use ($contentTypeId$prioritizedLanguages): bool {
  71.             $initializer null;
  72.             $wrappedObject $this->repository->getContentTypeService()->loadContentType(
  73.                 $contentTypeId,
  74.                 $prioritizedLanguages
  75.             );
  76.             return true;
  77.         };
  78.         return $this->proxyGenerator->createProxy(ContentType::class, $initializer);
  79.     }
  80.     public function createContentTypeGroupProxy(
  81.         int $contentTypeGroupId,
  82.         array $prioritizedLanguages Language::ALL
  83.     ): ContentTypeGroup {
  84.         $initializer = function (
  85.             &$wrappedObjectLazyLoadingInterface $proxy$method, array $parameters, &$initializer
  86.         ) use ($contentTypeGroupId$prioritizedLanguages): bool {
  87.             $initializer null;
  88.             $wrappedObject $this->repository->getContentTypeService()->loadContentTypeGroup(
  89.                 $contentTypeGroupId,
  90.                 $prioritizedLanguages
  91.             );
  92.             return true;
  93.         };
  94.         return $this->proxyGenerator->createProxy(ContentTypeGroup::class, $initializer);
  95.     }
  96.     public function createContentTypeGroupProxyList(
  97.         array $contentTypeGroupIds,
  98.         array $prioritizedLanguages Language::ALL
  99.     ): array {
  100.         $groups = [];
  101.         foreach ($contentTypeGroupIds as $contentTypeGroupId) {
  102.             $groups[] = $this->createContentTypeGroupProxy($contentTypeGroupId$prioritizedLanguages);
  103.         }
  104.         return $groups;
  105.     }
  106.     public function createLanguageProxy(string $languageCode): Language
  107.     {
  108.         $initializer = function (
  109.             &$wrappedObjectLazyLoadingInterface $proxy$method, array $parameters, &$initializer
  110.         ) use ($languageCode): bool {
  111.             $initializer null;
  112.             $wrappedObject $this->repository->getContentLanguageService()->loadLanguage($languageCode);
  113.             return true;
  114.         };
  115.         return $this->proxyGenerator->createProxy(Language::class, $initializer);
  116.     }
  117.     public function createLanguageProxyList(array $languageCodes): array
  118.     {
  119.         $languages = [];
  120.         foreach ($languageCodes as $languageCode) {
  121.             $languages[] = $this->createLanguageProxy($languageCode);
  122.         }
  123.         return $languages;
  124.     }
  125.     public function createLocationProxy(
  126.         int $locationId,
  127.         array $prioritizedLanguages Language::ALL
  128.     ): Location {
  129.         $initializer = function (
  130.             &$wrappedObjectLazyLoadingInterface $proxy$method, array $parameters, &$initializer
  131.         ) use ($locationId$prioritizedLanguages): bool {
  132.             $initializer null;
  133.             $wrappedObject $this->repository->getLocationService()->loadLocation(
  134.                 $locationId,
  135.                 $prioritizedLanguages
  136.             );
  137.             return true;
  138.         };
  139.         return $this->proxyGenerator->createProxy(Location::class, $initializer);
  140.     }
  141.     public function createSectionProxy(int $sectionId): Section
  142.     {
  143.         $initializer = function (
  144.             &$wrappedObjectLazyLoadingInterface $proxy$method, array $parameters, &$initializer
  145.         ) use ($sectionId): bool {
  146.             $initializer null;
  147.             $wrappedObject $this->repository->getSectionService()->loadSection($sectionId);
  148.             return true;
  149.         };
  150.         return $this->proxyGenerator->createProxy(Section::class, $initializer);
  151.     }
  152.     public function createUserProxy(int $userId, array $prioritizedLanguages Language::ALL): User
  153.     {
  154.         $initializer = function (
  155.             &$wrappedObjectLazyLoadingInterface $proxy$method, array $parameters, &$initializer
  156.         ) use ($userId$prioritizedLanguages): bool {
  157.             $initializer null;
  158.             $wrappedObject $this->repository->getUserService()->loadUser($userId$prioritizedLanguages);
  159.             return true;
  160.         };
  161.         return $this->proxyGenerator->createProxy(User::class, $initializer);
  162.     }
  163. }