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));
}
}
}
}