/**
  * 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());
 }
Exemplo n.º 2
0
 /**
  * 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;
 }
 /**
  * @param HydrateEvent $event
  */
 public function handleHydrate(HydrateEvent $event)
 {
     // don't need to instantiate the document if it is already existing.
     if ($event->hasDocument()) {
         return;
     }
     $node = $event->getNode();
     $document = $this->getDocumentFromNode($node);
     $event->setDocument($document);
 }
 /**
  * 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 RefreshEvent $event
  */
 public function handleRefresh(RefreshEvent $event)
 {
     $document = $event->getDocument();
     $node = $this->documentRegistry->getNodeForDocument($document);
     $locale = $this->documentRegistry->getLocaleForDocument($document);
     // revert/reload the node to the persisted state
     $node->revert();
     // rehydrate the document
     $hydrateEvent = new HydrateEvent($node, $locale);
     $hydrateEvent->setDocument($document);
     $this->eventDispatcher->dispatch(Events::HYDRATE, $hydrateEvent);
 }