/** * 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; }
/** * 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); }
/** * Is should return early if the node is not managed. */ public function testDocumentFromRegistryNoNode() { $this->hydrateEvent->hasDocument()->willReturn(true); $this->hydrateEvent->getNode()->willReturn($this->node->reveal()); $this->registry->hasNode($this->node->reveal())->willReturn(false); $this->subscriber->handleDocumentFromRegistry($this->hydrateEvent->reveal()); }
/** * @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()); }
/** * @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()); } }