Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Indexer / Classes.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Indexer;
4
5 use Drupal\drupalmoduleupgrader\IndexerBase;
6 use Drupal\drupalmoduleupgrader\IndexerUsageInterface;
7 use Pharborist\Filter;
8 use Pharborist\NodeCollection;
9 use Pharborist\NodeInterface;
10 use Pharborist\Objects\ClassNode;
11 use Pharborist\Objects\NewNode;
12 use Pharborist\Parser;
13
14 /**
15  * @Indexer(
16  *  id = "class"
17  * )
18  */
19 class Classes extends IndexerBase implements IndexerUsageInterface {
20
21   /**
22    * {@inheritdoc}
23    */
24   public function addFile($path) {
25     $doc = Parser::parseFile($path);
26
27     $doc
28       ->find(Filter::isInstanceOf('\Pharborist\Objects\ClassNode'))
29       ->each([ $this, 'add' ]);
30
31     $doc
32       ->find(Filter::isInstanceOf('\Pharborist\Objects\NewNode'))
33       ->each([ $this, 'add' ]);
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   public function add(NodeInterface $node) {
40     $fields = [
41       'file' => $node->getFilename(),
42       'type' => get_class($node),
43     ];
44
45     if ($node instanceof ClassNode) {
46       $fields['id'] = (string) $node->getName();
47       $fields['parent'] = (string) $node->getExtends();
48     }
49     elseif ($node instanceof NewNode) {
50       $fields['id'] = (string) $node->getClassName();
51     }
52     else {
53       throw new \InvalidArgumentException('Unexpected node type (expected ClassNode or NewNode).');
54     }
55
56     $this->db->insert($this->table)->fields($fields)->execute();
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function get($identifier) {
63     $file = $this->getQuery(['file'])
64       ->condition('id', $identifier)
65       ->execute()
66       ->fetchField();
67
68     return $this->target
69       ->open($file)
70       ->find(Filter::isClass($identifier))
71       ->get(0);
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function getFields() {
78     $fields = parent::getFields();
79
80     $fields['type'] = array(
81       'type' => 'varchar',
82       'length' => 255,
83       'not null' => TRUE,
84     );
85     $fields['parent'] = array(
86       'type' => 'varchar',
87       'length' => 255,
88     );
89
90     return $fields;
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   public function getUsages($identifier) {
97     $files = $this->getQuery(['file'])
98       ->distinct()
99       ->condition('id', $identifier)
100       ->condition('type', 'Pharborist\Objects\NewNode')
101       ->execute()
102       ->fetchCol();
103
104     $usages = new NodeCollection();
105     foreach ($files as $file) {
106       $this->target
107         ->open($file)
108         ->find(Filter::isInstanceOf('\Pharborist\Objects\NewNode'))
109         ->filter(function(NewNode $node) use ($identifier) {
110           return $node->getClassName() == $identifier;
111         })
112         ->addTo($usages);
113     }
114
115     return $usages;
116   }
117
118 }