/**
  * Create a new proxy object from the given document for
  * the given target node.
  *
  * TODO: We only pass the document here in order to correctly evaluate its locale
  *       later. I wonder if it necessary.
  *
  * @param object $fromDocument
  * @param NodeInterface $targetNode
  *
  * @return \ProxyManager\Proxy\GhostObjectInterface
  */
 public function createProxyForNode($fromDocument, NodeInterface $targetNode)
 {
     $eventDispatcher = $this->dispatcher;
     $registry = $this->registry;
     $targetMetadata = $this->metadataFactory->getMetadataForPhpcrNode($targetNode);
     // if node is already registered then just return the registered document
     if ($this->registry->hasNode($targetNode)) {
         $document = $this->registry->getDocumentForNode($targetNode);
         $locale = $registry->getOriginalLocaleForDocument($fromDocument);
         // If the parent is not loaded in the correct locale, reload it in the correct locale.
         if ($registry->getOriginalLocaleForDocument($document) !== $locale) {
             $hydrateEvent = new HydrateEvent($targetNode, $locale);
             $hydrateEvent->setDocument($document);
             $this->dispatcher->dispatch(Events::HYDRATE, $hydrateEvent);
         }
         return $document;
     }
     $initializer = function (LazyLoadingInterface $document, $method, array $parameters, &$initializer) use($fromDocument, $targetNode, $eventDispatcher, $registry) {
         $locale = $registry->getOriginalLocaleForDocument($fromDocument);
         $hydrateEvent = new HydrateEvent($targetNode, $locale);
         $hydrateEvent->setDocument($document);
         $eventDispatcher->dispatch(Events::HYDRATE, $hydrateEvent);
         $initializer = null;
     };
     $proxy = $this->proxyFactory->createProxy($targetMetadata->getClass(), $initializer);
     $locale = $registry->getOriginalLocaleForDocument($fromDocument);
     $this->registry->registerDocument($proxy, $targetNode, $locale);
     return $proxy;
 }
 /**
  * It should set the document on hydrate if the document for the node to
  * be hydrated is already in the registry.
  */
 public function testDocumentFromRegistry()
 {
     $this->hydrateEvent->hasDocument()->willReturn(false);
     $this->hydrateEvent->getNode()->willReturn($this->node->reveal());
     $this->hydrateEvent->getLocale()->willReturn('fr');
     $this->registry->hasNode($this->node->reveal())->willReturn(true);
     $this->registry->getDocumentForNode($this->node->reveal())->willReturn($this->document);
     $this->hydrateEvent->setDocument($this->document)->shouldBeCalled();
     $this->subscriber->handleDocumentFromRegistry($this->hydrateEvent->reveal());
 }
 /**
  * If there is already a document for the node registered, use that.
  *
  * @param HydrateEvent $event
  */
 public function handleDocumentFromRegistry(HydrateEvent $event)
 {
     if ($event->hasDocument()) {
         return;
     }
     $node = $event->getNode();
     if (!$this->documentRegistry->hasNode($node)) {
         return;
     }
     $document = $this->documentRegistry->getDocumentForNode($node);
     $event->setDocument($document);
 }
 /**
  * @param ObjectEvent $event
  */
 public function onPostDeserialize(ObjectEvent $event)
 {
     $document = $event->getObject();
     if (!$document instanceof PageDocument) {
         return;
     }
     if (!$document->getUuid()) {
         return;
     }
     try {
         $node = $this->session->getNodeByIdentifier($document->getUuid());
     } catch (ItemNotFoundException $e) {
         return;
     }
     if ($this->registry->hasNode($node)) {
         $registeredDocument = $this->registry->getDocumentForNode($node);
         $this->registry->deregisterDocument($registeredDocument);
     }
     // TODO use the original locale somehow
     $this->registry->registerDocument($document, $node, $document->getLocale());
 }
Beispiel #5
0
 /**
  * @param ObjectEvent $event
  */
 public function onPostDeserialize(ObjectEvent $event)
 {
     $document = $event->getObject();
     // only register documents
     if (!$this->metadataFactory->hasMetadataForClass(get_class($document))) {
         return;
     }
     if (!$document->getUuid()) {
         return;
     }
     try {
         $node = $this->nodeManager->find($document->getUuid());
     } catch (DocumentNotFoundException $e) {
         return;
     }
     if ($this->registry->hasNode($node, $document->getLocale())) {
         $registeredDocument = $this->registry->getDocumentForNode($node, $document->getLocale());
         $this->registry->deregisterDocument($registeredDocument);
     }
     // TODO use the original locale somehow
     if (!$this->registry->hasDocument($document)) {
         $this->registry->registerDocument($document, $node, $document->getLocale());
     }
 }