You are here

public function Constants::add in Drupal 7 to 8/9 Module Upgrader 8

Overrides IndexerBase::add

File

src/Plugin/DMU/Indexer/Constants.php, line 36

Class

Constants
Plugin annotation @Indexer( id = "constant" )

Namespace

Drupal\drupalmoduleupgrader\Plugin\DMU\Indexer

Code

public function add(NodeInterface $node) {
  if ($node instanceof DefineNode) {
    list($key, $value) = $node
      ->getArguments();
    if ($key instanceof StringNode && $value instanceof ScalarNode) {
      $this->elements[$key
        ->toValue()] = $value
        ->toValue();
    }
  }
  elseif ($node instanceof ConstantDeclarationNode) {
    $value = $node
      ->getValue();
    if ($value instanceof ScalarNode) {
      $this->elements[(string) $node
        ->getName()] = $value
        ->toValue();
    }
  }
  elseif ($node instanceof ConstantNode) {
    $this->db
      ->insert($this->table)
      ->fields([
      'id' => (string) $node
        ->getConstantName(),
      // Hardcoding file name, as file name resolution is unavailable due
      // to changes upstream in Pharborist.
      'file' => 'foo.module',
    ])
      ->execute();
  }
  else {
    throw new \InvalidArgumentException('Unexpected node type (expected DefineNode, ConstantDeclarationNode, or ConstantNode).');
  }
}