Skip to content

Commit

Permalink
making nodetypemanager return the right iterators and concentrating t…
Browse files Browse the repository at this point in the history
…he code about getting sub types in one place
  • Loading branch information
dbu committed Oct 12, 2011
1 parent 9a5df2a commit 6da2390
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
12 changes: 2 additions & 10 deletions src/Jackalope/NodeType/NodeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,7 @@ public function getDeclaredSupertypes()
*/
public function getSubtypes()
{
$ret = array();
foreach ($this->nodeTypeManager->getSubtypes($this->name) as $subtype) {
$ret[] = $this->nodeTypeManager->getNodeType($subtype);
}
return new ArrayIterator($ret);
return $this->nodeTypeManager->getSubtypes($this->name);
}

// inherit all doc
Expand All @@ -111,11 +107,7 @@ public function getSubtypes()
*/
public function getDeclaredSubtypes()
{
$ret = array();
foreach ($this->nodeTypeManager->getDeclaredSubtypes($this->name) as $subtype) {
$ret[] = $this->nodeTypeManager->getNodeType($subtype);
}
return new ArrayIterator($ret);
return $this->nodeTypeManager->getDeclaredSubtypes($this->name);
}

// inherit all doc
Expand Down
41 changes: 25 additions & 16 deletions src/Jackalope/NodeType/NodeTypeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,19 @@ protected function addNodeType(\PHPCR\NodeType\NodeTypeInterface $nodetype)
*
* @param string Nodename
*
* @return array of strings with the names of the subnodes
* @return Iterator according to NodeType::getDeclaredSubtypes
*
* @private
*/
public function getDeclaredSubtypes($nodeTypeName)
{
// TODO: do we need to call fetchNodeTypes(null) here?
// OPTIMIZE: any way to avoid loading all nodes at this point?
$this->fetchNodeTypes();

if (empty($this->nodeTree[$nodeTypeName])) {
return array();
}
return $this->nodeTree[$nodeTypeName];
return new ArrayIterator($this->nodeTree[$nodeTypeName]);
}

/**
Expand All @@ -157,15 +159,13 @@ public function getDeclaredSubtypes($nodeTypeName)
*/
public function getSubtypes($nodeTypeName)
{
// OPTIMIZE: any way to avoid loading all nodes at this point?
$this->fetchNodeTypes();
$ret = array();
if (empty($this->nodeTree[$nodeTypeName])) {
return array();
}

foreach ($this->nodeTree[$nodeTypeName] as $subnode) {
$ret = array_merge($ret, array($subnode), $this->getDeclaredSubtypes($subnode));
foreach ($this->nodeTree[$nodeTypeName] as $name => $subnode) {
$ret = array_merge($ret, array($name => $subnode), $this->nodeTree[$name]);
}
return $ret;
return new ArrayIterator($ret);
}

/**
Expand All @@ -180,9 +180,12 @@ private function addToNodeTree($nodetype)
{
foreach ($nodetype->getDeclaredSupertypeNames() as $declaredSupertypeName) {
if (isset($this->nodeTree[$declaredSupertypeName])) {
$this->nodeTree[$declaredSupertypeName] = array_merge($this->nodeTree[$declaredSupertypeName], array($nodetype->getName()));
$this->nodeTree[$declaredSupertypeName] =
array_merge($this->nodeTree[$declaredSupertypeName],
array($nodetype->getName() => $nodetype)
);
} else {
$this->nodeTree[$declaredSupertypeName] = array($nodetype->getName());
$this->nodeTree[$declaredSupertypeName] = array($nodetype->getName() => $nodetype);
}
}
}
Expand Down Expand Up @@ -213,7 +216,13 @@ public function getNodeType($nodeTypeName)
*/
public function hasNodeType($name)
{
$this->fetchNodeTypes($name);
try {
$this->fetchNodeTypes($name);
} catch (\PHPCR\NodeType\NoSuchNodeTypeException $e) {
// if we have not yet fetched all types and this type is not existing
// we get an exception. just ignore the exception, we don't have the type.
return false;
}
return isset($this->primaryTypes[$name]) || isset($this->mixinTypes[$name]);
}

Expand All @@ -224,7 +233,7 @@ public function hasNodeType($name)
public function getAllNodeTypes()
{
$this->fetchNodeTypes();
return new ArrayIterator(array_values(array_merge($this->primaryTypes, $this->mixinTypes)));
return new ArrayIterator(array_merge($this->primaryTypes, $this->mixinTypes));
}

// inherit all doc
Expand All @@ -234,7 +243,7 @@ public function getAllNodeTypes()
public function getPrimaryNodeTypes()
{
$this->fetchNodeTypes();
return new ArrayIterator(array_values($this->primaryTypes));
return new ArrayIterator($this->primaryTypes);
}

// inherit all doc
Expand All @@ -244,7 +253,7 @@ public function getPrimaryNodeTypes()
public function getMixinNodeTypes()
{
$this->fetchNodeTypes();
return new ArrayIterator(array_values($this->mixinTypes));
return new ArrayIterator($this->mixinTypes);
}

// inherit all doc
Expand Down

0 comments on commit 6da2390

Please sign in to comment.