Skip to content

Commit

Permalink
moved document to class mapping out of the UnitOfWork
Browse files Browse the repository at this point in the history
  • Loading branch information
lsmith77 committed Oct 24, 2011
1 parent bbdb011 commit 1cb200a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 39 deletions.
4 changes: 4 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Configuration.php
Expand Up @@ -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
Expand Down Expand Up @@ -155,6 +156,9 @@ public function getMetadataDriverImpl()
*/
public function getDocumentNameMapper()
{
if (empty($this->attributes['documentNameMapper'])) {
$this->setDocumentNameMapper(new DocumentNameMapper());
}
return $this->attributes['documentNameMapper'];
}

Expand Down
71 changes: 71 additions & 0 deletions lib/Doctrine/ODM/PHPCR/DocumentNameMapper.php
@@ -0,0 +1,71 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/

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);
}
}
17 changes: 14 additions & 3 deletions lib/Doctrine/ODM/PHPCR/DocumentNameMapperInterface.php
Expand Up @@ -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);
}
40 changes: 4 additions & 36 deletions lib/Doctrine/ODM/PHPCR/UnitOfWork.php
Expand Up @@ -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
*
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1cb200a

Please sign in to comment.