Constants.php in Drupal 7 to 8/9 Module Upgrader 8
File
src/Plugin/DMU/Indexer/Constants.php
View source
<?php
namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Indexer;
use Drupal\drupalmoduleupgrader\ArrayIndexer;
use Drupal\drupalmoduleupgrader\IndexerUsageInterface;
use Pharborist\Constants\ConstantDeclarationNode;
use Pharborist\Constants\ConstantNode;
use Pharborist\Filter;
use Pharborist\Functions\DefineNode;
use Pharborist\NodeCollection;
use Pharborist\NodeInterface;
use Pharborist\Parser;
use Pharborist\Types\ScalarNode;
use Pharborist\Types\StringNode;
class Constants extends ArrayIndexer implements IndexerUsageInterface {
public function addFile($path) {
Parser::parseFile($path)
->find(Filter::isInstanceOf('\\Pharborist\\Constants\\ConstantNode', '\\Pharborist\\Functions\\DefineNode', '\\Pharborist\\Constants\\ConstantDeclarationNode'))
->each([
$this,
'add',
]);
}
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(),
'file' => 'foo.module',
])
->execute();
}
else {
throw new \InvalidArgumentException('Unexpected node type (expected DefineNode, ConstantDeclarationNode, or ConstantNode).');
}
}
public function getUsages($identifier) {
$files = $this
->getQuery([
'file',
])
->distinct()
->condition('id', $identifier)
->execute()
->fetchCol();
$usages = new NodeCollection();
foreach ($files as $file) {
$this->target
->open($file)
->find(Filter::isInstanceOf('\\Pharborist\\Constants\\ConstantNode'))
->filter(function (ConstantNode $node) use ($identifier) {
return $node
->getConstantName() == $identifier;
})
->addTo($usages);
}
return $usages;
}
}
Classes
Name |
Description |
Constants |
Plugin annotation
@Indexer(
id = "constant"
) |