3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Indexer;
5 use Drupal\drupalmoduleupgrader\IndexerBase;
6 use Drupal\drupalmoduleupgrader\IndexerUsageInterface;
8 use Pharborist\NodeCollection;
9 use Pharborist\NodeInterface;
10 use Pharborist\Objects\ClassNode;
11 use Pharborist\Objects\NewNode;
12 use Pharborist\Parser;
19 class Classes extends IndexerBase implements IndexerUsageInterface {
24 public function addFile($path) {
25 $doc = Parser::parseFile($path);
28 ->find(Filter::isInstanceOf('\Pharborist\Objects\ClassNode'))
29 ->each([ $this, 'add' ]);
32 ->find(Filter::isInstanceOf('\Pharborist\Objects\NewNode'))
33 ->each([ $this, 'add' ]);
39 public function add(NodeInterface $node) {
41 'file' => $node->getFilename(),
42 'type' => get_class($node),
45 if ($node instanceof ClassNode) {
46 $fields['id'] = (string) $node->getName();
47 $fields['parent'] = (string) $node->getExtends();
49 elseif ($node instanceof NewNode) {
50 $fields['id'] = (string) $node->getClassName();
53 throw new \InvalidArgumentException('Unexpected node type (expected ClassNode or NewNode).');
56 $this->db->insert($this->table)->fields($fields)->execute();
62 public function get($identifier) {
63 $file = $this->getQuery(['file'])
64 ->condition('id', $identifier)
70 ->find(Filter::isClass($identifier))
77 public function getFields() {
78 $fields = parent::getFields();
80 $fields['type'] = array(
85 $fields['parent'] = array(
96 public function getUsages($identifier) {
97 $files = $this->getQuery(['file'])
99 ->condition('id', $identifier)
100 ->condition('type', 'Pharborist\Objects\NewNode')
104 $usages = new NodeCollection();
105 foreach ($files as $file) {
108 ->find(Filter::isInstanceOf('\Pharborist\Objects\NewNode'))
109 ->filter(function(NewNode $node) use ($identifier) {
110 return $node->getClassName() == $identifier;