View source
<?php
namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Indexer;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\drupalmoduleupgrader\IndexerBase;
use Drupal\drupalmoduleupgrader\IndexerExecutionInterface;
use Drupal\drupalmoduleupgrader\IndexerUsageInterface;
use Drupal\drupalmoduleupgrader\Utility\Filter\ContainsLogicFilter;
use Pharborist\Filter;
use Pharborist\Functions\FunctionDeclarationNode;
use Pharborist\NodeCollection;
use Pharborist\NodeInterface;
use Pharborist\Parser;
class Functions extends IndexerBase implements IndexerExecutionInterface, IndexerUsageInterface {
protected function prepareID($id) {
return preg_replace('/^hook_/', $this->target
->id() . '_', $id);
}
public function has($identifier) {
return parent::has($this
->prepareID($identifier));
}
public function hasAny(array $identifiers) {
return parent::hasAny(array_map([
$this,
'prepareID',
], $identifiers));
}
public function hasAll(array $identifiers) {
return parent::hasAll(array_map([
$this,
'prepareID',
], $identifiers));
}
public function addFile($path) {
if (!class_exists('Pharborist\\Parser')) {
\Drupal::logger("Drupalmoduleupgrader")
->error("Have you ran 'composer up' in the drupalmoduleupgrader folder yet?", [
"missing",
]);
throw new \Exception("The Pharborist\\Parser class was not found, please make sure to run 'composer up' in the drupalmoduleupgrader folder and try again.");
}
$doc = Parser::parseFile($path);
$doc
->children(Filter::isInstanceOf('\\Pharborist\\Functions\\FunctionDeclarationNode'))
->each([
$this,
'add',
]);
$doc
->find(Filter::isInstanceOf('\\Pharborist\\Functions\\FunctionCallNode'))
->each([
$this,
'add',
]);
}
public function add(NodeInterface $node) {
$fields = [
'id' => (string) $node
->getName(),
'file' => $node
->getFilename(),
'type' => get_class($node),
];
if ($node instanceof FunctionDeclarationNode) {
$logical = new ContainsLogicFilter();
$logical
->whitelist('t');
$logical
->whitelist('drupal_get_path');
$fields['has_logic'] = (int) $node
->is($logical);
}
$this->db
->insert($this->table)
->fields($fields)
->execute();
}
public function delete($id) {
parent::delete($this
->prepareID($id));
}
public function get($identifier) {
$identifier = $this
->prepareID($identifier);
$file = $this
->getQuery([
'file',
])
->condition('id', $identifier)
->execute()
->fetchField();
return $this->target
->open($file)
->children(Filter::isFunction($identifier))
->get(0);
}
public function getMultiple(array $identifiers) {
return parent::getMultiple(array_map([
$this,
'prepareID',
], $identifiers));
}
public function getFields() {
$fields = parent::getFields();
$fields['type'] = [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
];
$fields['has_logic'] = [
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
];
return $fields;
}
public function getQuery(array $fields = []) {
return parent::getQuery($fields)
->condition('type', 'Pharborist\\Functions\\FunctionDeclarationNode');
}
public function hasExecutable($identifier) {
if ($this
->has($identifier)) {
$ret = $this
->getQuery()
->condition('id', $this
->prepareID($identifier))
->condition('has_logic', 0)
->countQuery()
->execute()
->fetchField();
return $ret;
}
else {
return FALSE;
}
}
public function execute($identifier, array $arguments = []) {
$function = $this
->prepareID($identifier);
if (function_exists($function)) {
return call_user_func_array($function, $arguments);
}
else {
if ($this
->hasExecutable($function)) {
eval($this
->get($function)
->get(0)
->getText());
return $this
->execute($function, $arguments);
}
else {
$variables = [
'@function' => $function,
];
throw new \LogicException((new FormattableMarkup('Cowardly refusing to execute @function.', $variables))
->__toString());
}
}
}
public function getUsages($identifier) {
$function = $this
->prepareID($identifier);
$files = $this
->getQuery([
'file',
])
->distinct()
->condition('id', $function)
->condition('type', 'Pharborist\\Functions\\FunctionCallNode')
->execute()
->fetchCol();
$usages = new NodeCollection();
foreach ($files as $file) {
$this->target
->open($file)
->find(Filter::isFunctionCall($function))
->addTo($usages);
}
return $usages;
}
}