-
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support child restriction #706
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
use Doctrine\Common\ClassLoader; | ||
use Doctrine\Instantiator\Instantiator; | ||
use Doctrine\Instantiator\InstantiatorInterface; | ||
use Doctrine\ODM\PHPCR\Exception\OutOfBoundsException; | ||
|
||
/** | ||
* Metadata class | ||
|
@@ -328,6 +329,23 @@ class ClassMetadata implements ClassMetadataInterface | |
*/ | ||
public $parentClasses = array(); | ||
|
||
/** | ||
* READ-ONLY: Child class restrictions. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the phpdoc should explain that if this list is empty, it means we don't check the classes of the children. to make a leaf node, use the explicit isLeaf field. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
* | ||
* If empty then any classes are permitted. | ||
* | ||
* @var array | ||
*/ | ||
public $childClasses = array(); | ||
|
||
/** | ||
* READ-ONLY: If the document should be act as a leaf-node and therefore | ||
* not be allowed children. | ||
* | ||
* @var boolean | ||
*/ | ||
public $isLeaf = false; | ||
|
||
/** | ||
* The inherited fields of this class | ||
* | ||
|
@@ -453,6 +471,57 @@ public function validateIdentifier() | |
} | ||
} | ||
|
||
/** | ||
* Validate that childClasses is empty if isLeaf is true. | ||
* | ||
* @throws MappingException if there is a conflict between isLeaf and childClasses. | ||
*/ | ||
public function validateChildClasses() | ||
{ | ||
if (count($this->childClasses) > 0 && $this->isLeaf) { | ||
throw new MappingException(sprintf( | ||
'Cannot map a document as a leaf and define child classes for "%s"', | ||
$this->name | ||
)); | ||
} | ||
} | ||
|
||
/** | ||
* Assert that the given class FQN can be a child of the document this | ||
* metadata represents. | ||
* | ||
* @param string $classFqn | ||
* @throws OutOfBoundsException | ||
*/ | ||
public function assertValidChildClass(ClassMetadata $class) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dbu moved the validation logic to the |
||
{ | ||
if ($this->isLeaf()) { | ||
throw new OutOfBoundsException(sprintf( | ||
'Document "%s" has been mapped as a leaf. It cannot have children', | ||
$this->name | ||
)); | ||
} | ||
|
||
$childClasses = $this->getChildClasses(); | ||
|
||
if (0 === count($childClasses)) { | ||
return; | ||
} | ||
|
||
foreach ($childClasses as $childClass) { | ||
if ($class->name === $childClass || $class->reflClass->isSubclassOf($childClass)) { | ||
return; | ||
} | ||
} | ||
|
||
throw new OutOfBoundsException(sprintf( | ||
'Document "%s" does not allow children of type "%s". Allowed child classes "%s"', | ||
$this->name, | ||
$class->name, | ||
implode('", "', $childClasses) | ||
)); | ||
} | ||
|
||
/** | ||
* Validate whether this class needs to be referenceable. | ||
* | ||
|
@@ -1123,6 +1192,49 @@ public function getParentClasses() | |
return $this->parentClasses; | ||
} | ||
|
||
/** | ||
* Return the class names or interfaces that children of this document must | ||
* be an instance of. | ||
* | ||
* @return string[] | ||
*/ | ||
public function getChildClasses() | ||
{ | ||
return $this->childClasses; | ||
} | ||
|
||
/** | ||
* Set the class names or interfaces that children of this document must be | ||
* instance of. | ||
* | ||
* @param string[] $childClasses | ||
*/ | ||
public function setChildClasses(array $childClasses) | ||
{ | ||
$this->childClasses = $childClasses; | ||
} | ||
|
||
/** | ||
* Return true if this is designated as a leaf node. | ||
* | ||
* @return bool | ||
*/ | ||
public function isLeaf() | ||
{ | ||
return $this->isLeaf; | ||
} | ||
|
||
/** | ||
* Set if this document should act as a leaf node. | ||
* | ||
* @param bool $isLeaf | ||
*/ | ||
public function setIsLeaf($isLeaf) | ||
{ | ||
$this->isLeaf = $isLeaf; | ||
} | ||
|
||
|
||
/** | ||
* Checks whether the class will generate an id via the repository. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -256,6 +256,7 @@ protected function validateRuntimeMetadata($class, $parent) | |
$class->validateIdentifier(); | ||
$class->validateReferenceable(); | ||
$class->validateReferences(); | ||
$class->validateChildClasses(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. kind of strange that we don't have just one validate method on ClassMetadata and call that here, and keep the details of what needs to be validated local to ClassMetadata... but not subject of this PR, so lets keep it like this here. |
||
$class->validateLifecycleCallbacks($this->getReflectionService()); | ||
$class->validateTranslatables(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,7 @@ | |
use PHPCR\Util\PathHelper; | ||
use PHPCR\Util\NodeHelper; | ||
use Jackalope\Session as JackalopeSession; | ||
use Doctrine\ODM\PHPCR\Exception\OutOfBoundsException; | ||
|
||
/** | ||
* Unit of work class | ||
|
@@ -2332,6 +2333,8 @@ private function executeInserts($documents) | |
} | ||
|
||
$parentNode = $this->session->getNode(PathHelper::getParentPath($id)); | ||
$this->validateChildClass($parentNode, $class); | ||
|
||
$nodename = PathHelper::getNodeName($id); | ||
$node = $parentNode->addNode($nodename, $class->nodeType); | ||
if ($class->node) { | ||
|
@@ -2731,6 +2734,9 @@ private function executeMoves($documents) | |
); | ||
} | ||
|
||
$parentNode = $this->session->getNode(PathHelper::getParentPath($targetPath)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this comes at a cost of loading another document There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. .. or rather a node. in some cases that node will already have been loaded (f.e. parent mappings always load the parent node), I will benchmark to check the performance cost. |
||
$this->validateChildClass($parentNode, $class); | ||
|
||
$this->session->move($sourcePath, $targetPath); | ||
|
||
// update fields nodename, parentMapping and depth if they exist in this type | ||
|
@@ -3968,4 +3974,23 @@ public function invokeGlobalEvent($eventName, EventArgs $event) | |
$this->eventManager->dispatchEvent($eventName, $event); | ||
} | ||
} | ||
|
||
/** | ||
* If the parent node has child restrictions, ensure that the given | ||
* class name is within them. | ||
* | ||
* @param NodeInterface $parentNode | ||
* @param string $classFqn | ||
*/ | ||
private function validateChildClass(NodeInterface $parentNode, ClassMetadata $class) | ||
{ | ||
$parentClass = $this->documentClassMapper->getClassName($this->dm, $parentNode); | ||
|
||
if (null === $parentClass) { | ||
return; | ||
} | ||
|
||
$metadata = $this->dm->getClassMetadata($parentClass); | ||
$metadata->assertValidChildClass($class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\Models\CMS; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM; | ||
|
||
/** | ||
* @PHPCRODM\Document(childClasses={"Doctrine\Tests\Models\CMS\CmsArticle"}) | ||
*/ | ||
class CmsArticleFolder | ||
{ | ||
/** | ||
* @PHPCRODM\Id | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @PHPCRODM\Children() | ||
*/ | ||
public $articles; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\Models\CMS; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM; | ||
|
||
/** | ||
* @PHPCRODM\Document( | ||
* childClasses={ | ||
* "Doctrine\Tests\Models\CMS\CmsBlogPost" | ||
* } | ||
* ) | ||
*/ | ||
class CmsBlogFolder | ||
{ | ||
/** | ||
* @PHPCRODM\Id() | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @PHPCRODM\Children() | ||
*/ | ||
public $posts; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add the header to styleci. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think styleci ignores the test folder. |
||
namespace Documents; | ||
|
||
namespace Doctrine\Tests\Models\CMS; | ||
|
||
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM; | ||
use Doctrine\ODM\PHPCR\DocumentRepository; | ||
use Doctrine\ODM\PHPCR\Id\RepositoryIdInterface; | ||
|
||
/** | ||
* @PHPCRODM\Document() | ||
*/ | ||
class CmsBlogInvalidChild | ||
{ | ||
/** @PHPCRODM\Id(strategy="parent") */ | ||
public $id; | ||
|
||
/** @PHPCRODM\NodeName() */ | ||
public $name; | ||
|
||
/** @PHPCRODM\ParentDocument() */ | ||
public $parent; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dbu changed to an attribute as there are no examples of node-value elements in Doctrine. /cc @wouterj