5 * Contains \Drupal\drupalmoduleupgrader\Plugin\DMU\Indexer\Constants.
8 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Indexer;
10 use Drupal\drupalmoduleupgrader\ArrayIndexer;
11 use Drupal\drupalmoduleupgrader\IndexerUsageInterface;
12 use Pharborist\Constants\ConstantDeclarationNode;
13 use Pharborist\Constants\ConstantNode;
14 use Pharborist\Filter;
15 use Pharborist\Functions\DefineNode;
16 use Pharborist\NodeCollection;
17 use Pharborist\NodeInterface;
18 use Pharborist\Parser;
19 use Pharborist\Types\ScalarNode;
20 use Pharborist\Types\StringNode;
27 class Constants extends ArrayIndexer implements IndexerUsageInterface {
32 public function addFile($path) {
33 Parser::parseFile($path)
34 ->find(Filter::isInstanceOf('\Pharborist\Constants\ConstantNode', '\Pharborist\Functions\DefineNode', '\Pharborist\Constants\ConstantDeclarationNode'))
35 ->each([ $this, 'add' ]);
41 public function add(NodeInterface $node) {
42 if ($node instanceof DefineNode) {
43 list ($key, $value) = $node->getArguments();
44 if ($key instanceof StringNode && $value instanceof ScalarNode) {
45 $this->elements[ $key->toValue() ] = $value->toValue();
48 elseif ($node instanceof ConstantDeclarationNode) {
49 $value = $node->getValue();
50 if ($value instanceof ScalarNode) {
51 $this->elements[ (string) $node->getName() ] = $value->toValue();
54 elseif ($node instanceof ConstantNode) {
56 ->insert($this->table)
58 'id' => (string) $node->getConstantName(),
59 // Hardcoding file name, as file name resolution is unavailable due
60 // to changes upstream in Pharborist.
61 'file' => 'foo.module',
66 throw new \InvalidArgumentException('Unexpected node type (expected DefineNode, ConstantDeclarationNode, or ConstantNode).');
73 public function getUsages($identifier) {
74 $files = $this->getQuery(['file'])
76 ->condition('id', $identifier)
80 $usages = new NodeCollection();
81 foreach ($files as $file) {
84 ->find(Filter::isInstanceOf('\Pharborist\Constants\ConstantNode'))
85 ->filter(function(ConstantNode $node) use ($identifier) {
86 return $node->getConstantName() == $identifier;