buildAutoCreatedChildNodeIdentifier() public static method

This is needed if multiple node variants are created through "createNode" with different dimension values. If child nodes with the same path and different identifiers exist, bad things can happen.
public static buildAutoCreatedChildNodeIdentifier ( string $childNodeName, string $identifier ) : string
$childNodeName string
$identifier string Identifier of the node where the child node should be created
return string The generated UUID like identifier
 /**
  * Create missing child nodes for the given node type
  *
  * @param NodeType $nodeType
  * @param string $workspaceName
  * @param boolean $dryRun
  * @return void
  */
 protected function createChildNodesByNodeType(NodeType $nodeType, $workspaceName, $dryRun)
 {
     $createdNodesCount = 0;
     $updatedNodesCount = 0;
     $nodeCreationExceptions = 0;
     $nodeIdentifiersWhichNeedUpdate = [];
     $nodeTypes = $this->nodeTypeManager->getSubNodeTypes($nodeType->getName(), false);
     $nodeTypes[$nodeType->getName()] = $nodeType;
     if ($this->nodeTypeManager->hasNodeType((string) $nodeType)) {
         $nodeType = $this->nodeTypeManager->getNodeType((string) $nodeType);
         $nodeTypeNames[$nodeType->getName()] = $nodeType;
     } else {
         $this->output->outputLine('Node type "%s" does not exist', array((string) $nodeType));
         exit(1);
     }
     /** @var $nodeType NodeType */
     foreach ($nodeTypes as $nodeTypeName => $nodeType) {
         $childNodes = $nodeType->getAutoCreatedChildNodes();
         foreach ($this->getNodeDataByNodeTypeAndWorkspace($nodeTypeName, $workspaceName) as $nodeData) {
             $context = $this->nodeFactory->createContextMatchingNodeData($nodeData);
             $node = $this->nodeFactory->createFromNodeData($nodeData, $context);
             if (!$node instanceof NodeInterface) {
                 continue;
             }
             foreach ($childNodes as $childNodeName => $childNodeType) {
                 try {
                     $childNode = $node->getNode($childNodeName);
                     $childNodeIdentifier = Utility::buildAutoCreatedChildNodeIdentifier($childNodeName, $node->getIdentifier());
                     if ($childNode === null) {
                         if ($dryRun === false) {
                             $node->createNode($childNodeName, $childNodeType, $childNodeIdentifier);
                             $this->output->outputLine('Auto created node named "%s" in "%s"', array($childNodeName, $node->getPath()));
                         } else {
                             $this->output->outputLine('Missing node named "%s" in "%s"', array($childNodeName, $node->getPath()));
                         }
                         $createdNodesCount++;
                     } elseif ($childNode->getIdentifier() !== $childNodeIdentifier) {
                         $nodeIdentifiersWhichNeedUpdate[$childNode->getIdentifier()] = $childNodeIdentifier;
                     }
                 } catch (\Exception $exception) {
                     $this->output->outputLine('Could not create node named "%s" in "%s" (%s)', array($childNodeName, $node->getPath(), $exception->getMessage()));
                     $nodeCreationExceptions++;
                 }
             }
         }
     }
     if (count($nodeIdentifiersWhichNeedUpdate) > 0) {
         if ($dryRun === false) {
             foreach ($nodeIdentifiersWhichNeedUpdate as $oldNodeIdentifier => $newNodeIdentifier) {
                 $queryBuilder = $this->entityManager->createQueryBuilder();
                 $queryBuilder->update(NodeData::class, 'n')->set('n.identifier', $queryBuilder->expr()->literal($newNodeIdentifier))->where('n.identifier = ?1')->setParameter(1, $oldNodeIdentifier);
                 $result = $queryBuilder->getQuery()->getResult();
                 $updatedNodesCount++;
                 $this->output->outputLine('Updated node identifier from %s to %s because it was not a "stable" identifier', [$oldNodeIdentifier, $newNodeIdentifier]);
             }
         } else {
             foreach ($nodeIdentifiersWhichNeedUpdate as $oldNodeIdentifier => $newNodeIdentifier) {
                 $this->output->outputLine('Child nodes with identifier "%s" need to change their identifier to "%s"', [$oldNodeIdentifier, $newNodeIdentifier]);
                 $updatedNodesCount++;
             }
         }
     }
     if ($createdNodesCount !== 0 || $nodeCreationExceptions !== 0 || $updatedNodesCount !== 0) {
         if ($dryRun === false) {
             if ($createdNodesCount > 0) {
                 $this->output->outputLine('Created %s new child nodes', array($createdNodesCount));
             }
             if ($updatedNodesCount > 0) {
                 $this->output->outputLine('Updated identifier of %s child nodes', array($updatedNodesCount));
             }
             if ($nodeCreationExceptions > 0) {
                 $this->output->outputLine('%s Errors occurred during child node creation', array($nodeCreationExceptions));
             }
             $this->persistenceManager->persistAll();
         } else {
             if ($createdNodesCount > 0) {
                 $this->output->outputLine('%s missing child nodes need to be created', array($createdNodesCount));
             }
             if ($updatedNodesCount > 0) {
                 $this->output->outputLine('%s identifiers of child nodes need to be updated', array($updatedNodesCount));
             }
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Creates, adds and returns a child node of this node. Also sets default
  * properties and creates default subnodes.
  *
  * @param string $name Name of the new node
  * @param NodeType $nodeType Node type of the new node (optional)
  * @param string $identifier The identifier of the node, unique within the workspace, optional(!)
  * @return NodeInterface
  * @api
  */
 public function createNode($name, NodeType $nodeType = null, $identifier = null)
 {
     $this->emitBeforeNodeCreate($this, $name, $nodeType, $identifier);
     $newNode = $this->createSingleNode($name, $nodeType, $identifier);
     if ($nodeType !== null) {
         foreach ($nodeType->getDefaultValuesForProperties() as $propertyName => $propertyValue) {
             if (substr($propertyName, 0, 1) === '_') {
                 ObjectAccess::setProperty($newNode, substr($propertyName, 1), $propertyValue);
             } else {
                 $newNode->setProperty($propertyName, $propertyValue);
             }
         }
         foreach ($nodeType->getAutoCreatedChildNodes() as $childNodeName => $childNodeType) {
             $childNodeIdentifier = Utility::buildAutoCreatedChildNodeIdentifier($childNodeName, $newNode->getIdentifier());
             $alreadyPresentChildNode = $newNode->getNode($childNodeName);
             if ($alreadyPresentChildNode === null) {
                 $newNode->createNode($childNodeName, $childNodeType, $childNodeIdentifier);
             }
         }
     }
     $this->context->getFirstLevelNodeCache()->flush();
     $this->emitNodeAdded($newNode);
     $this->emitAfterNodeCreate($newNode);
     return $newNode;
 }