vendor/sulu/article-bundle/Document/Structure/ContentProxyFactory.php line 79

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\ArticleBundle\Document\Structure;
  11. use ProxyManager\Factory\LazyLoadingValueHolderFactory;
  12. use ProxyManager\Proxy\LazyLoadingInterface;
  13. use ProxyManager\Proxy\VirtualProxyInterface;
  14. use Sulu\Component\Content\Compat\StructureInterface;
  15. use Sulu\Component\Content\ContentTypeManagerInterface;
  16. use Sulu\Component\Webspace\Analyzer\Attributes\RequestAttributes;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. /**
  19.  * Factory for content-proxies.
  20.  */
  21. class ContentProxyFactory
  22. {
  23.     /**
  24.      * @var ContentTypeManagerInterface
  25.      */
  26.     private $contentTypeManager;
  27.     /**
  28.      * @var LazyLoadingValueHolderFactory
  29.      */
  30.     private $proxyFactory;
  31.     /**
  32.      * @var RequestStack
  33.      */
  34.     private $requestStack;
  35.     public function __construct(
  36.         ContentTypeManagerInterface $contentTypeManager,
  37.         LazyLoadingValueHolderFactory $proxyFactory,
  38.         RequestStack $requestStack
  39.     ) {
  40.         $this->contentTypeManager $contentTypeManager;
  41.         $this->proxyFactory $proxyFactory;
  42.         $this->requestStack $requestStack;
  43.     }
  44.     /**
  45.      * Create content-proxy for given structure.
  46.      *
  47.      * @return VirtualProxyInterface
  48.      */
  49.     public function createContentProxy(StructureInterface $structure, array $data)
  50.     {
  51.         return $this->proxyFactory->createProxy(
  52.             \ArrayObject::class,
  53.             function(
  54.                 &$wrappedObject,
  55.                 LazyLoadingInterface $proxy,
  56.                 $method,
  57.                 array $parameters,
  58.                 &$initializer
  59.             ) use ($structure$data) {
  60.                 $initializer null;
  61.                 $wrappedObject = new \ArrayObject($this->resolveContent($structure$data));
  62.                 return true;
  63.             }
  64.         );
  65.     }
  66.     /**
  67.      * Resolve content from given data with the structure.
  68.      */
  69.     private function resolveContent(StructureInterface $structure, array $data): array
  70.     {
  71.         $structure->setWebspaceKey($this->getWebspaceKey());
  72.         $content = [];
  73.         foreach ($structure->getProperties(true) as $child) {
  74.             if (\array_key_exists($child->getName(), $data)) {
  75.                 $child->setValue($data[$child->getName()]);
  76.             }
  77.             $contentType $this->contentTypeManager->get($child->getContentTypeName());
  78.             $content[$child->getName()] = $contentType->getContentData($child);
  79.         }
  80.         return $content;
  81.     }
  82.     /**
  83.      * Create view-proxy for given structure.
  84.      *
  85.      * @return VirtualProxyInterface
  86.      */
  87.     public function createViewProxy(StructureInterface $structure, array $data)
  88.     {
  89.         return $this->proxyFactory->createProxy(
  90.             \ArrayObject::class,
  91.             function(
  92.                 &$wrappedObject,
  93.                 LazyLoadingInterface $proxy,
  94.                 $method,
  95.                 array $parameters,
  96.                 &$initializer
  97.             ) use ($structure$data) {
  98.                 $initializer null;
  99.                 $wrappedObject = new \ArrayObject($this->resolveView($structure$data));
  100.                 return true;
  101.             }
  102.         );
  103.     }
  104.     /**
  105.      * Resolve view from given data with the structure.
  106.      */
  107.     private function resolveView(StructureInterface $structure, array $data): array
  108.     {
  109.         $structure->setWebspaceKey($this->getWebspaceKey());
  110.         $view = [];
  111.         foreach ($structure->getProperties(true) as $child) {
  112.             if (\array_key_exists($child->getName(), $data)) {
  113.                 $child->setValue($data[$child->getName()]);
  114.             }
  115.             $contentType $this->contentTypeManager->get($child->getContentTypeName());
  116.             $view[$child->getName()] = $contentType->getViewData($child);
  117.         }
  118.         return $view;
  119.     }
  120.     private function getWebspaceKey(): ?string
  121.     {
  122.         $request $this->requestStack->getCurrentRequest();
  123.         if (!$request) {
  124.             return null;
  125.         }
  126.         /** @var RequestAttributes $attributes */
  127.         $attributes $request->attributes->get('_sulu');
  128.         if (!$attributes) {
  129.             return null;
  130.         }
  131.         return $attributes->getAttribute('webspaceKey');
  132.     }
  133. }