/** * 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 not register on PERSIST when there is already a document. */ public function testHandleRegisterPersistAlreadyExists() { $this->persistEvent->getDocument()->willReturn($this->document); $this->persistEvent->getNode()->willReturn($this->node->reveal()); $this->persistEvent->getLocale()->willReturn('fr'); $this->registry->registerDocument($this->document, $this->node->reveal(), 'fr')->shouldNotBeCalled(); $this->registry->updateLocale($this->document, 'fr')->shouldBeCalled(); $this->registry->hasDocument($this->document)->willReturn(true); $this->subscriber->handlePersist($this->persistEvent->reveal()); }
private function handleRegister(AbstractMappingEvent $event) { $document = $event->getDocument(); $node = $event->getNode(); $locale = $event->getLocale(); if ($this->documentRegistry->hasDocument($document)) { $this->documentRegistry->updateLocale($document, $locale); return; } $this->documentRegistry->registerDocument($document, $node, $locale); }
/** * @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()); } }