From 1cb200a406607c6df35e5982ef671e1f94b279fc Mon Sep 17 00:00:00 2001 From: Lukas Kahwe Smith Date: Mon, 24 Oct 2011 10:07:46 +0200 Subject: [PATCH] moved document to class mapping out of the UnitOfWork --- lib/Doctrine/ODM/PHPCR/Configuration.php | 4 ++ lib/Doctrine/ODM/PHPCR/DocumentNameMapper.php | 71 +++++++++++++++++++ .../ODM/PHPCR/DocumentNameMapperInterface.php | 17 ++++- lib/Doctrine/ODM/PHPCR/UnitOfWork.php | 40 ++--------- 4 files changed, 93 insertions(+), 39 deletions(-) create mode 100644 lib/Doctrine/ODM/PHPCR/DocumentNameMapper.php diff --git a/lib/Doctrine/ODM/PHPCR/Configuration.php b/lib/Doctrine/ODM/PHPCR/Configuration.php index 3b33871dd..1e8cdbc41 100644 --- a/lib/Doctrine/ODM/PHPCR/Configuration.php +++ b/lib/Doctrine/ODM/PHPCR/Configuration.php @@ -22,6 +22,7 @@ use Doctrine\ODM\PHPCR\Mapping\Driver\Driver; use Doctrine\ODM\PHPCR\Mapping\Driver\BuiltinDocumentsDriver; use Doctrine\ODM\PHPCR\DocumentNameMapperInterface; +use Doctrine\ODM\PHPCR\DocumentNameMapper; /** * Configuration class @@ -155,6 +156,9 @@ public function getMetadataDriverImpl() */ public function getDocumentNameMapper() { + if (empty($this->attributes['documentNameMapper'])) { + $this->setDocumentNameMapper(new DocumentNameMapper()); + } return $this->attributes['documentNameMapper']; } diff --git a/lib/Doctrine/ODM/PHPCR/DocumentNameMapper.php b/lib/Doctrine/ODM/PHPCR/DocumentNameMapper.php new file mode 100644 index 000000000..fbf25912a --- /dev/null +++ b/lib/Doctrine/ODM/PHPCR/DocumentNameMapper.php @@ -0,0 +1,71 @@ +. + */ + +namespace Doctrine\ODM\PHPCR; + +use Doctrine\ODM\PHPCR\DocumentManager; + +use PHPCR\NodeInterface; +use PHPCR\PropertyType; + +class DocumentNameMapper implements DocumentNameMapperInterface +{ + /** + * Determine the document name from a given node + * + * @param DocumentManager + * @param NodeInterface $node + * @param string $documentName + * + * @return string + * + * @throws \RuntimeException if no class name could be determined + */ + public function getClassName(DocumentManager $dm, NodeInterface $node, $documentName = null) + { + if (isset($documentName)) { + $className = $documentName; + } else if ($node->hasProperty('phpcr:class')) { + $className = $node->getProperty('phpcr:class')->getString(); + } else if ($node->hasProperty('phpcr:alias')) { + $aliasName = $node->getProperty('phpcr:alias')->getString(); + $class = $dm->getMetadataFactory()->getMetadataForAlias($aliasName); + $className = $class->name; + } + + // default to the built in generic document class + if (empty($className)) { + $className = 'Doctrine\ODM\PHPCR\Document\Generic'; + } + + return $className; + } + + /** + * Determine the document name from a given node + * + * @param DocumentManager + * @param NodeInterface $node + * @param string $className + */ + public function writeMetadata(DocumentManager $dm, NodeInterface $node, $className) + { + $node->setProperty('phpcr:class', $className, PropertyType::STRING); + } +} diff --git a/lib/Doctrine/ODM/PHPCR/DocumentNameMapperInterface.php b/lib/Doctrine/ODM/PHPCR/DocumentNameMapperInterface.php index ac30f5335..337a33349 100644 --- a/lib/Doctrine/ODM/PHPCR/DocumentNameMapperInterface.php +++ b/lib/Doctrine/ODM/PHPCR/DocumentNameMapperInterface.php @@ -28,10 +28,21 @@ interface DocumentNameMapperInterface * Determine the document name from a given node * * @param DocumentManager - * @param string $documentName * @param NodeInterface $node - * @param boolean $writeMetadata + * @param string $documentName + * * @return string + * + * @throws \RuntimeException if no class name could be determined + */ + function getClassName(DocumentManager $dm, NodeInterface $node, $documentName = null); + + /** + * Determine the document name from a given node + * + * @param DocumentManager + * @param NodeInterface $node + * @param string $className */ - function getClassName(DocumentManager $dm, NodeInterface $node, $documentName = null, $writeMetadata = false); + function writeMetadata(DocumentManager $dm, NodeInterface $node, $className); } diff --git a/lib/Doctrine/ODM/PHPCR/UnitOfWork.php b/lib/Doctrine/ODM/PHPCR/UnitOfWork.php index ece9d91cd..675d1ac74 100644 --- a/lib/Doctrine/ODM/PHPCR/UnitOfWork.php +++ b/lib/Doctrine/ODM/PHPCR/UnitOfWork.php @@ -180,38 +180,6 @@ public function refreshDocumentForProxy($documentName, $document) $this->createDocument($documentName, $node, $hints); } - /** - * @param NodeInterface $node - * @param $documentName - * - * @return string class name - */ - private function getClassName(NodeInterface $node, $documentName = null) - { - if ($this->documentNameMapper) { - $className = $this->documentNameMapper->getClassName($this->dm, $node, $documentName, $this->writeMetadata); - if (empty($className)) { - throw new \InvalidArgumentException('Could not determine document class for node'); - } - } else { - if (isset($documentName)) { - $className = $documentName; - } else if ($node->hasProperty('phpcr:class')) { - $className = $node->getProperty('phpcr:class')->getString(); - } else if ($node->hasProperty('phpcr:alias')) { - $aliasName = $node->getProperty('phpcr:alias')->getString(); - $class = $this->dm->getMetadataFactory()->getMetadataForAlias($aliasName); - $className = $class->name; - } - - // default to the built in generic document class - if (empty($className)) { - $className = 'Doctrine\ODM\PHPCR\Document\Generic'; - } - } - - return $className; - } /** * Create a document given class, data and the doc-id and revision * @@ -222,7 +190,7 @@ private function getClassName(NodeInterface $node, $documentName = null) */ public function createDocument($documentName, $node, array &$hints = array()) { - $className = $this->getClassName($node, $documentName); + $className = $this->documentNameMapper->getClassName($this->dm, $node, $documentName); $class = $this->dm->getClassMetadata($className); $documentState = array(); @@ -384,7 +352,7 @@ private function createProxy($node, $className = null) } if (null === $className) { - $className = $this->getClassName($node); + $className = $this->documentNameMapper->getClassName($this->dm, $node); } $proxyDocument = $this->dm->getProxyFactory()->getProxy($className, $targetId); @@ -888,7 +856,7 @@ private function executeInserts($documents) $node = $this->nodesMap[$oid]; if ($this->writeMetadata) { - $node->setProperty('phpcr:class', $class->name, PropertyType::STRING); + $this->documentNameMapper->writeMetadata($this->dm, $node, $class->name); } try { @@ -947,7 +915,7 @@ private function executeUpdates($documents, $dispatchEvents = true) $node = $this->nodesMap[$oid]; if ($this->writeMetadata) { - $node->setProperty('phpcr:class', $class->name, PropertyType::STRING); + $this->documentNameMapper->writeMetadata($this->dm, $node, $class->name); } if ($dispatchEvents) {