vendor/sulu/sulu/src/Sulu/Component/DocumentManager/ProxyFactory.php line 116

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\Component\DocumentManager;
  11. use PHPCR\NodeInterface;
  12. use ProxyManager\Factory\LazyLoadingGhostFactory;
  13. use ProxyManager\Proxy\GhostObjectInterface;
  14. use ProxyManager\Proxy\LazyLoadingInterface;
  15. use Sulu\Component\DocumentManager\Collection\ChildrenCollection;
  16. use Sulu\Component\DocumentManager\Collection\ReferrerCollection;
  17. use Sulu\Component\DocumentManager\Event\HydrateEvent;
  18. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  19. /**
  20.  * Handle creation of proxies.
  21.  */
  22. class ProxyFactory
  23. {
  24.     /**
  25.      * @var LazyLoadingGhostFactory
  26.      */
  27.     private $proxyFactory;
  28.     /**
  29.      * @var EventDispatcherInterface
  30.      */
  31.     private $dispatcher;
  32.     /**
  33.      * @var DocumentRegistry
  34.      */
  35.     private $registry;
  36.     /**
  37.      * @var MetadataFactoryInterface
  38.      */
  39.     private $metadataFactory;
  40.     public function __construct(
  41.         LazyLoadingGhostFactory $proxyFactory,
  42.         EventDispatcherInterface $dispatcher,
  43.         DocumentRegistry $registry,
  44.         MetadataFactoryInterface $metadataFactory
  45.     ) {
  46.         $this->proxyFactory $proxyFactory;
  47.         $this->dispatcher $dispatcher;
  48.         $this->registry $registry;
  49.         $this->metadataFactory $metadataFactory;
  50.     }
  51.     /**
  52.      * Create a new proxy object from the given document for
  53.      * the given target node.
  54.      *
  55.      * TODO: We only pass the document here in order to correctly evaluate its locale
  56.      *       later. I wonder if it necessary.
  57.      *
  58.      * @param object $fromDocument
  59.      * @param array $options
  60.      *
  61.      * @return GhostObjectInterface
  62.      */
  63.     public function createProxyForNode($fromDocumentNodeInterface $targetNode$options = [])
  64.     {
  65.         // if node is already registered then just return the registered document
  66.         $locale $this->registry->getOriginalLocaleForDocument($fromDocument);
  67.         if ($this->registry->hasNode($targetNode$locale)) {
  68.             $document $this->registry->getDocumentForNode($targetNode$locale);
  69.             // If the parent is not loaded in the correct locale, reload it in the correct locale.
  70.             if ($this->registry->getOriginalLocaleForDocument($document) !== $locale) {
  71.                 $hydrateEvent = new HydrateEvent($targetNode$locale);
  72.                 $hydrateEvent->setDocument($document);
  73.                 $this->dispatcher->dispatch($hydrateEventEvents::HYDRATE);
  74.             }
  75.             return $document;
  76.         }
  77.         $initializer = function(LazyLoadingInterface $document$method, array $parameters, &$initializer) use (
  78.             $targetNode,
  79.             $options,
  80.             $locale
  81.         ) {
  82.             $hydrateEvent = new HydrateEvent($targetNode$locale$options);
  83.             $hydrateEvent->setDocument($document);
  84.             $this->dispatcher->dispatch($hydrateEventEvents::HYDRATE);
  85.             $initializer null;
  86.         };
  87.         $targetMetadata $this->metadataFactory->getMetadataForPhpcrNode($targetNode);
  88.         $proxy $this->proxyFactory->createProxy($targetMetadata->getClass(), $initializer);
  89.         $locale $this->registry->getOriginalLocaleForDocument($fromDocument);
  90.         $this->registry->registerDocument($proxy$targetNode$locale);
  91.         return $proxy;
  92.     }
  93.     /**
  94.      * Create a new children collection for the given document.
  95.      *
  96.      * @param object $document
  97.      *
  98.      * @return ChildrenCollection
  99.      */
  100.     public function createChildrenCollection($document, array $options = [])
  101.     {
  102.         $node $this->registry->getNodeForDocument($document);
  103.         $locale $this->registry->getOriginalLocaleForDocument($document);
  104.         return new ChildrenCollection(
  105.             $node,
  106.             $this->dispatcher,
  107.             $locale,
  108.             $options
  109.         );
  110.     }
  111.     /**
  112.      * Create a new collection of referrers from a list of referencing items.
  113.      *
  114.      * @param object $document
  115.      *
  116.      * @return ReferrerCollection
  117.      */
  118.     public function createReferrerCollection($document)
  119.     {
  120.         $node $this->registry->getNodeForDocument($document);
  121.         $locale $this->registry->getOriginalLocaleForDocument($document);
  122.         return new ReferrerCollection(
  123.             $node,
  124.             $this->dispatcher,
  125.             $locale
  126.         );
  127.     }
  128. }