Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Indexer / Constants.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\drupalmoduleupgrader\Plugin\DMU\Indexer\Constants.
6  */
7
8 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Indexer;
9
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;
21
22 /**
23  * @Indexer(
24  *  id = "constant"
25  * )
26  */
27 class Constants extends ArrayIndexer implements IndexerUsageInterface {
28
29   /**
30    * {@inheritdoc}
31    */
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' ]);
36   }
37
38   /**
39    * {@inheritdoc}
40    */
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();
46       }
47     }
48     elseif ($node instanceof ConstantDeclarationNode) {
49       $value = $node->getValue();
50       if ($value instanceof ScalarNode) {
51         $this->elements[ (string) $node->getName() ] = $value->toValue();
52       }
53     }
54     elseif ($node instanceof ConstantNode) {
55       $this->db
56         ->insert($this->table)
57         ->fields([
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',
62         ])
63         ->execute();
64     }
65     else {
66       throw new \InvalidArgumentException('Unexpected node type (expected DefineNode, ConstantDeclarationNode, or ConstantNode).');
67     }
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function getUsages($identifier) {
74     $files = $this->getQuery(['file'])
75       ->distinct()
76       ->condition('id', $identifier)
77       ->execute()
78       ->fetchCol();
79
80     $usages = new NodeCollection();
81     foreach ($files as $file) {
82       $this->target
83         ->open($file)
84         ->find(Filter::isInstanceOf('\Pharborist\Constants\ConstantNode'))
85         ->filter(function(ConstantNode $node) use ($identifier) {
86           return $node->getConstantName() == $identifier;
87         })
88         ->addTo($usages);
89     }
90
91     return $usages;
92   }
93
94 }