public function adoptNode(NodeInterface $node, $recursive = false)
{
if ($node->getContext() === $this && $node->dimensionsAreMatchingTargetDimensionValues()) {
return $node;
}
$this->emitBeforeAdoptNode($node, $this, $recursive);
$existingNode = $this->getNodeByIdentifier($node->getIdentifier());
if ($existingNode !== null) {
if ($existingNode->dimensionsAreMatchingTargetDimensionValues()) {
$adoptedNode = $existingNode;
} else {
$adoptedNode = $existingNode->createVariantForContext($this);
}
} else {
$adoptedNode = $node->createVariantForContext($this);
}
$this->firstLevelNodeCache->setByIdentifier($adoptedNode->getIdentifier(), $adoptedNode);
if ($recursive) {
$childNodes = $node->getChildNodes();
/** @var NodeInterface $childNode */
foreach ($childNodes as $childNode) {
if (!$childNode->getNodeType()->isAggregate()) {
$this->adoptNode($childNode, true);
}
}
}
$this->emitAfterAdoptNode($node, $this, $recursive);
return $adoptedNode;
}