public function execute(Node $node, Context $context) { if ($node instanceof MacroReferenceNode) { $path = $node->getMacroPath(); // Provided that users might want some things hardcoded, everything // under the 'macro' root node will be considered as macros if (false === strpos($path, Path::SEP)) { $path = 'macro' . Path::SEP . $path; } $macro = (new Path($path))->find($context->getGraph()); if (!$macro) { throw new CompilerException(sprintf("'%s': '%s' macro does not exist", $node->getPath(), $node->getValue())); } if (1 !== count($macro)) { throw new CompilerException(sprintf("'%s': '%s' multiple targets found", $node->getPath(), $node->getValue())); } $node->getParent()->replaceChild($node->getName(), reset($macro)->duplicate($node->getName())); } }
public function execute(Node $node, Context $context) { $sorted = array(); $orphans = array(); $path = $node->getPath(); foreach ($node->getChildren() as $key => $child) { if ($child->hasChild('inherit')) { $parent = $child->getChild('inherit')->getValue(); if (!is_string($parent)) { $context->logCritical(sprintf("%s: %s: malformed inherit directive", $path, $key)); } if (!$node->hasChild($parent)) { $context->logCritical(sprintf("%s: %s: cannot inherit from non existing: %s", $path, $key, $parent)); } if ($key === $parent) { $context->logCritical(sprintf("%s: %s: cannot inherit from itself", $path, $key)); } $child->removeChild('inherit'); $orphans[$key] = $parent; } else { $sorted[$key] = array(); } } while (!empty($orphans)) { $count = count($orphans); foreach ($orphans as $key => $parent) { if (isset($sorted[$parent])) { $sorted[$parent][] = $key; $sorted[$key] = array(); unset($orphans[$key]); } } if (count($orphans) === $count) { $context->logCritical(sprintf("%s: circular dependency detected", $path)); } } foreach (array_filter($sorted) as $parent => $children) { foreach ($children as $name) { $node->getChild($name)->setAttribute('inherits', $node->getChild($parent)->getPath()); } } }
public function execute(Node $node, Context $context) { $path = $node->getPath(); foreach ($this->map as $pattern => $class) { $attributes = Path::match($path, $pattern); if ($attributes !== false) { /* @var $replacement \USync\AST\Drupal\DrupalNodeInterface */ if ($node->isTerminal()) { $replacement = new $class($node->getName(), $node->getValue()); } else { $replacement = new $class($node->getName()); // @todo fix this 2-step terminal check if ($replacement->isTerminal()) { $replacement = new $class($node->getName(), $node->getValue()); } else { $replacement->mergeWith($node); } } $replacement->setAttributes($node->getAttributes() + $attributes); $node->getParent()->replaceChild($node->getName(), $replacement); break; } } }