Skip to content

Commit

Permalink
References with using proxy objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Stark committed Aug 19, 2011
1 parent c9fd14f commit fa1b77c
Show file tree
Hide file tree
Showing 12 changed files with 1,095 additions and 75 deletions.
45 changes: 45 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Configuration.php
Expand Up @@ -43,6 +43,7 @@ class Configuration
'validateDoctrineMetadata' => true,
'metadataDriverImpl' => null,
'documentNameMapper' => null,
'proxyNamespace' => 'MyPHPCRProxyNS'
);

/**
Expand Down Expand Up @@ -164,4 +165,48 @@ public function setDocumentNameMapper(DocumentNameMapperInterface $documentNameM
{
$this->attributes['documentNameMapper'] = $documentNameMapper;
}

/**
* Sets the directory where Doctrine generates any necessary proxy class files.
*
* @param string $dir
*/
public function setProxyDir($dir)
{
$this->attributes['proxyDir'] = $dir;
}

/**
* Gets the directory where Doctrine generates any necessary proxy class files.
*
* @return string
*/
public function getProxyDir()
{
if (!isset($this->attributes['proxyDir'])) {
$this->attributes['proxyDir'] = \sys_get_temp_dir();
}

return $this->attributes['proxyDir'];
}

/**
* Sets the namespace for Doctrine proxy class files.
*
* @param string $namespace
*/
public function setProxyNamespace($namespace)
{
$this->attributes['proxyNamespace'] = $namespace;
}

/**
* Gets the namespace for Doctrine proxy class files.
*
* @return string
*/
public function getProxyNamespace()
{
return $this->attributes['proxyNamespace'];
}
}
10 changes: 10 additions & 0 deletions lib/Doctrine/ODM/PHPCR/DocumentManager.php
Expand Up @@ -294,6 +294,16 @@ public function refresh($document)
return $this->unitOfWork->createDocument(get_class($document), $node, $hints);
}

/**
* Create the proxies actual document
*
* @param object $document
*/
public function createDocumentForProxy($document)
{
$this->getRepository(get_class($document))->createDocumentForProxy($document);
}

/**
* Get the child documents of a given document using an optional filter.
*
Expand Down
10 changes: 10 additions & 0 deletions lib/Doctrine/ODM/PHPCR/DocumentRepository.php
Expand Up @@ -174,6 +174,16 @@ public function refresh($document)
return $this->dm->refresh($document);
}

/**
* @param object $document
* @return void
*/
public function createDocumentForProxy($document)
{
$uow = $this->dm->getUnitOfWork();
$uow->createDocumentForProxy($this->documentName, $document);
}

/**
* @return string
*/
Expand Down
Expand Up @@ -10,6 +10,7 @@ final class Document extends Annotation
public $alias;
public $repositoryClass;
public $isVersioned;
public $referenceable;
}
final class MappedSuperclass extends Annotation
{
Expand Down Expand Up @@ -75,15 +76,13 @@ final class ArrayField extends Property
class Reference extends Annotation
{
public $targetDocument;
public $weak = true;
}
final class ReferenceOne extends Reference
{
public $cascade = array();
}
final class ReferenceMany extends Reference
{
public $cascade = array();
public $mappedBy;
}
class Child extends Annotation
{
Expand Down
43 changes: 15 additions & 28 deletions lib/Doctrine/ODM/PHPCR/Mapping/ClassMetadataInfo.php
Expand Up @@ -205,6 +205,13 @@ class ClassMetadataInfo implements ClassMetadata
*/
public $versionField = null;

/**
* determines if the document is referenceable or not
*
* @var bool
*/
public $referenceable = false;

/**
* READ-ONLY: The Id generator options.
*
Expand Down Expand Up @@ -371,6 +378,14 @@ public function setVersioned($versionable)
$this->isVersioned = $versionable;
}

/**
* @param bool $referenceable
*/
public function setReferenceable($referenceable)
{
$this->referenceable = $referenceable;
}

/**
* @param string $nodeType
*/
Expand Down Expand Up @@ -459,26 +474,6 @@ public function mapField(array $mapping)

$mapping = $this->validateAndCompleteFieldMapping($mapping);

if (isset($mapping['reference']) && $mapping['type'] === 'one') {
$mapping['association'] = self::TO_ONE;
}
if (isset($mapping['reference']) && $mapping['type'] === 'many') {
$mapping['association'] = self::TO_MANY;
}

$mapping['isOwningSide'] = true;
$mapping['isInverseSide'] = false;
if (isset($mapping['reference'])) {
if (isset($mapping['inversedBy']) && $mapping['inversedBy']) {
$mapping['isOwningSide'] = true;
$mapping['isInverseSide'] = false;
}
if (isset($mapping['mappedBy']) && $mapping['mappedBy']) {
$mapping['isInverseSide'] = true;
$mapping['isOwningSide'] = false;
}
}

if (!isset($mapping['multivalue'])) {
$mapping['multivalue'] = false;
}
Expand Down Expand Up @@ -568,8 +563,6 @@ protected function validateAndCompleteAssociationMapping($mapping)
public function mapManyToOne($mapping)
{
$mapping = $this->validateAndCompleteAssociationMapping($mapping);

$mapping['isOwning'] = true;
$mapping['type'] = self::MANY_TO_ONE;

$this->storeAssociationMapping($mapping);
Expand All @@ -578,12 +571,6 @@ public function mapManyToOne($mapping)
public function mapManyToMany($mapping)
{
$mapping = $this->validateAndCompleteAssociationMapping($mapping);

if (!empty($mapping['mappedBy'])) {
$mapping['isOwning'] = false;
} else {
$mapping['isOwning'] = true;
}
$mapping['type'] = self::MANY_TO_MANY;

$this->storeAssociationMapping($mapping);
Expand Down
17 changes: 5 additions & 12 deletions lib/Doctrine/ODM/PHPCR/Mapping/Driver/AnnotationDriver.php
Expand Up @@ -142,6 +142,11 @@ public function loadMetadataForClass($className, ClassMetadata $class)
}
$class->setNodeType($documentAnnot->nodeType);

if (isset($documentAnnot->referenceable) && $documentAnnot->referenceable)
{
$class->setReferenceable(true);
}

if ($documentAnnot->repositoryClass) {
$class->setCustomRepositoryClassName($documentAnnot->repositoryClass);
}
Expand All @@ -167,21 +172,9 @@ public function loadMetadataForClass($className, ClassMetadata $class)
$mapping = array_merge($mapping, (array) $fieldAnnot);
$class->mapChildren($mapping);
} elseif ($fieldAnnot instanceof \Doctrine\ODM\PHPCR\Mapping\Annotations\ReferenceOne) {
$cascade = 0;
foreach ($fieldAnnot->cascade as $cascadeMode) {
$cascade += constant('Doctrine\ODM\PHPCR\Mapping\ClassMetadata::CASCADE_' . strtoupper($cascadeMode));
}
$fieldAnnot->cascade = $cascade;

$mapping = array_merge($mapping, (array) $fieldAnnot);
$class->mapManyToOne($mapping);
} elseif ($fieldAnnot instanceof \Doctrine\ODM\PHPCR\Mapping\Annotations\ReferenceMany) {
$cascade = 0;
foreach ($fieldAnnot->cascade as $cascadeMode) {
$cascade += constant('Doctrine\ODM\PHPCR\Mapping\ClassMetadata::CASCADE_' . strtoupper($cascadeMode));
}
$fieldAnnot->cascade = $cascade;

$mapping = array_merge($mapping, (array) $fieldAnnot);
$class->mapManyToMany($mapping);
}
Expand Down
8 changes: 8 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Proxy/Proxy.php
@@ -0,0 +1,8 @@
<?php

namespace Doctrine\ODM\PHPCR\Proxy;

interface Proxy
{

}
41 changes: 41 additions & 0 deletions lib/Doctrine/ODM/PHPCR/Proxy/ProxyException.php
@@ -0,0 +1,41 @@
<?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\Proxy;

/**
* PHPCR ODM Proxy Exception
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
* @since 1.0
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class ProxyException extends \Doctrine\ODM\PHPCR\PHPCRException
{
public static function proxyDirectoryRequired()
{
return new self("You must configure a proxy directory. See docs for details");
}

public static function proxyNamespaceRequired()
{
return new self("You must configure a proxy namespace. See docs for details");
}
}

0 comments on commit fa1b77c

Please sign in to comment.