Apache Zeta Components Manual :: File Source for memory.php
Source for file memory.php
Documentation is available at memory.php
* File containing the ezcTreeMemory class.
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @version //autogentag//
* ezcTreeMemory is an implementation of a tree backend that operates on
* an in-memory tree structure. Meta-information is kept in objects of the
* ezcTreeMemoryNode class.
* $tree = ezcTreeMemory::create( new ezcTreeMemoryDataStore() );
* $tree = new ezcTreeMemory( new ezcTreeMemoryDataStore() );
* See {@link ezcTree} for examples on how to operate on the tree.
* @property-read ezcTreeXmlDataStore $store
* The data store that is used for retrieving/storing data.
* @property string $nodeClassName
* Which class is used as tree node - this class *must* inherit
* @version //autogentag//
* Contains a list of all nodes, indexed by node ID that link directly to the create node so that they can be looked up quickly.
* @var array(string=>ezcTreeMemoryNode)
private $nodeList =
array();
* Contains the root node.
* Stores the last auto generated ID that was used.
* @var integer $autoNodeId
* Constructs a new ezcTreeMemory object.
* The store that is used for data storage should be passed as the
* @param ezcTreeMemoryDataStore $store
protected function __construct( ezcTreeMemoryDataStore $store )
* This method generates the next node ID.
return $this->autoNodeId;
* A factory method that creates a new empty tree using the data store $store.
* @param ezcTreeMemoryDataStore $store
public static function create( ezcTreeMemoryDataStore $store )
$newTree->nodeList =
null;
$newTree->rootNode =
null;
* Returns whether the node with ID $nodeId exists.
return isset
( $this->nodeList[$nodeId] );
* Returns the node identified by the ID $nodeId.
* @throws ezcTreeInvalidIdException if there is no node with ID $nodeId
return $this->getNodeById( $nodeId )->node;
* Returns the node container for node $nodeId.
* @throws ezcTreeInvalidIdException if there is no node with ID $nodeId
* @return ezcTreeMemoryNode
private function getNodeById( $nodeId )
return $this->nodeList[$nodeId];
* Returns all the children of the node with ID $nodeId.
* @return ezcTreeNodeList
$treeNode =
$this->getNodeById( $nodeId );
foreach ( $treeNode->children as $nodeId =>
$child )
$list->addNode( $child->node );
* Returns the parent node of the node with ID $nodeId.
* This method returns null if there is no parent node.
$treeNode =
$this->getNodeById( $nodeId );
$parentNode =
$treeNode->parent;
return $parentNode !==
null ?
$parentNode->node :
null;
* Returns all the nodes in the path from the root node to the node with ID
* $nodeId, including those two nodes.
* @return ezcTreeNodeList
$memoryNode =
$this->getNodeById( $nodeId );
$nodes[] =
$memoryNode->node;
$memoryNode =
$memoryNode->parent;
while ( $memoryNode !==
null )
$nodes[] =
$memoryNode->node;
$memoryNode =
$memoryNode->parent;
* Adds the children nodes of the node $memoryNode to the
* @param ezcTreeNodeList $list
* @param ezcTreeMemoryNode $memoryNode
private function addChildNodesDepthFirst( ezcTreeNodeList $list, ezcTreeMemoryNode $memoryNode )
foreach ( $memoryNode->children as $nodeId =>
$childMemoryNode )
$list->addNode( $childMemoryNode->node );
$this->addChildNodesDepthFirst( $list, $childMemoryNode );
* Returns the node with ID $nodeId and all its children, sorted accoring to
* the {@link http://en.wikipedia.org/wiki/Depth-first_search Depthth-first sorting}
* @return ezcTreeNodeList
$memoryNode =
$this->getNodeById( $nodeId );
$list->addNode( $memoryNode->node );
$this->addChildNodesDepthFirst( $list, $memoryNode );
* Alias for fetchSubtreeDepthFirst().
* @return ezcTreeNodeList
* Adds the children nodes of the node $memoryNode to the
* @param ezcTreeNodeList $list
* @param ezcTreeMemoryNode $memoryNode
private function addChildNodesBreadthFirst( ezcTreeNodeList $list, ezcTreeMemoryNode $memoryNode )
foreach ( $memoryNode->children as $nodeId =>
$childMemoryNode )
$list->addNode( $childMemoryNode->node );
foreach ( $memoryNode->children as $nodeId =>
$childMemoryNode )
$this->addChildNodesBreadthFirst( $list, $childMemoryNode );
* Returns the node with ID $nodeId and all its children, sorted according to
* the {@link http://en.wikipedia.org/wiki/Breadth-first_search Breadth-first sorting}
* @return ezcTreeNodeList
$memoryNode =
$this->getNodeById( $nodeId );
$list->addNode( $memoryNode->node );
$this->addChildNodesBreadthFirst( $list, $memoryNode );
* Returns the number of direct children of the node with ID $nodeId.
return count( $this->getNodeById( $nodeId )->children );
* Helper method that iterates recursively over the children of $node to
* count the number of children.
* @param ezcTreeMemoryNode $node
private function countChildNodes( &$count, ezcTreeMemoryNode $node )
foreach ( $node->children as $nodeId =>
$node )
$this->countChildNodes( $count, $node );
* Returns the number of children of the node with ID $nodeId, recursively
$node =
$this->getNodeById( $nodeId );
$this->countChildNodes( $count, $node );
* Returns the distance from the root node to the node with ID $nodeId
$childNode =
$this->getNodeById( $nodeId );
while ( $childNode !==
null )
$childNode =
$childNode->parent;
* Returns whether the node with ID $nodeId has children
return count( $this->getNodeById( $nodeId )->children ) >
0;
* Returns whether the node with ID $childId is a direct child of the node
* @param string $parentId
public function isChildOf( $childId, $parentId )
$childNode =
$this->getNodeById( $childId );
$parentNode =
$this->getNodeById( $parentId );
if ( $childNode->parent->node ===
$parentNode->node )
* Returns whether the node with ID $childId is a direct or indirect child
* of the node with ID $parentId
* @param string $parentId
$childNode =
$this->getNodeById( $childId );
$parentNode =
$this->getNodeById( $parentId );
if ( $childNode ===
$parentNode )
while ( $childNode !==
null )
if ( $childNode->node ===
$parentNode->node )
$childNode =
$childNode->parent;
* Returns whether the nodes with IDs $child1Id and $child2Id are siblings
* (ie, the share the same parent)
* @param string $child1Id
* @param string $child2Id
$elem1 =
$this->getNodeById( $child1Id );
$elem2 =
$this->getNodeById( $child2Id );
( $child1Id !==
$child2Id ) &&
( $elem1->parent ===
$elem2->parent )
* Sets a new node as root node, this wipes also out the whole tree
* @param ezcTreeNode $node
// wipe nodelist and data
$this->nodeList =
array();
$this->store->deleteDataForAllNodes();
$newObj =
new ezcTreeMemoryNode( $node, array() );
$this->rootNode =
$newObj;
$this->nodeList[$node->id] =
$newObj;
* This methods returns null if there is no root node.
return $this->rootNode->node;
* Adds the node $childNode as child of the node with ID $parentId
* @param string $parentId
* @param ezcTreeNode $childNode
public function addChild( $parentId, ezcTreeNode $childNode )
$this->addTransactionItem( new ezcTreeTransactionItem( ezcTreeTransactionItem::ADD, $childNode, null, $parentId ) );
$parentMemoryNode =
$this->getNodeById( $parentId );
$newObj =
new ezcTreeMemoryNode( $childNode, array(), $parentMemoryNode );
$parentMemoryNode->children[$childNode->id] =
$newObj;
$this->nodeList[$childNode->id] =
$newObj;
* Deletes the node with ID $nodeId from the tree, including all its children
public function delete( $nodeId )
$this->addTransactionItem( new ezcTreeTransactionItem( ezcTreeTransactionItem::DELETE, null, $nodeId ) );
$nodeToDelete =
$this->getNodeById( $nodeId );
// fetch the whole subtree and delete all the associated data
$children =
$nodeToDelete->node->fetchSubtree();
$this->store->deleteDataForNodes( $children );
// Use the parent to remove the child
unset
( $nodeToDelete->parent->children[$nodeId] );
// Remove the node and all its children
unset
( $this->nodeList[$nodeId] );
* Moves the node with ID $nodeId as child to the node with ID $targetParentId
* @param string $targetParentId
public function move( $nodeId, $targetParentId )
$this->addTransactionItem( new ezcTreeTransactionItem( ezcTreeTransactionItem::MOVE, null, $nodeId, $targetParentId ) );
$nodeToMove =
$this->getNodeById( $nodeId );
$newParent =
$this->getNodeById( $targetParentId );
// new placement for node
$newParent->children[$nodeId] =
$nodeToMove;
// remove old location from previous parent
unset
( $nodeToMove->parent->children[$nodeId] );
// update parent attribute of the node
$nodeToMove->parent =
$newParent;
* Fixates the transaction.