class Functions in Drupal 7 to 8/9 Module Upgrader 8
Plugin annotation
@Indexer(
id = "function"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\drupalmoduleupgrader\IndexerBase implements ContainerFactoryPluginInterface, IndexerInterface
- class \Drupal\drupalmoduleupgrader\Plugin\DMU\Indexer\Functions implements IndexerExecutionInterface, IndexerUsageInterface
- class \Drupal\drupalmoduleupgrader\IndexerBase implements ContainerFactoryPluginInterface, IndexerInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of Functions
6 files declare their use of Functions
- DeleteTest.php in tests/
src/ Unit/ Plugin/ DMU/ Fixer/ DeleteTest.php - FlagHookTest.php in tests/
src/ Unit/ Plugin/ DMU/ Analyzer/ FlagHookTest.php - FunctionsTest.php in tests/
src/ Unit/ Plugin/ DMU/ Indexer/ FunctionsTest.php - HookFormAlterTest.php in tests/
src/ Unit/ Plugin/ DMU/ Analyzer/ HookFormAlterTest.php - HookPermissionTest.php in tests/
src/ Unit/ Plugin/ DMU/ Analyzer/ HookPermissionTest.php
File
- src/
Plugin/ DMU/ Indexer/ Functions.php, line 21
Namespace
Drupal\drupalmoduleupgrader\Plugin\DMU\IndexerView source
class Functions extends IndexerBase implements IndexerExecutionInterface, IndexerUsageInterface {
protected function prepareID($id) {
return preg_replace('/^hook_/', $this->target
->id() . '_', $id);
}
/**
* {@inheritdoc}
*/
public function has($identifier) {
return parent::has($this
->prepareID($identifier));
}
/**
* {@inheritdoc}
*/
public function hasAny(array $identifiers) {
return parent::hasAny(array_map([
$this,
'prepareID',
], $identifiers));
}
/**
* {@inheritdoc}
*/
public function hasAll(array $identifiers) {
return parent::hasAll(array_map([
$this,
'prepareID',
], $identifiers));
}
/**
* {@inheritdoc}
*/
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',
]);
}
/**
* {@inheritdoc}
*/
public function add(NodeInterface $node) {
/** @var \Pharborist\Functions\FunctionDeclarationNode|\Pharborist\Functions\FunctionCallNode $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();
}
/**
* {@inheritdoc}
*/
public function delete($id) {
parent::delete($this
->prepareID($id));
}
/**
* {@inheritdoc}
*/
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);
}
/**
* {@inheritdoc}
*/
public function getMultiple(array $identifiers) {
return parent::getMultiple(array_map([
$this,
'prepareID',
], $identifiers));
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function getQuery(array $fields = []) {
return parent::getQuery($fields)
->condition('type', 'Pharborist\\Functions\\FunctionDeclarationNode');
}
/**
* {@inheritdoc}
*/
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;
}
}
/**
* {@inheritdoc}
*/
public function execute($identifier, array $arguments = []) {
$function = $this
->prepareID($identifier);
// If the function already exists, we can safely assume that it's already
// been scanned for dangerous logic and evaluated into existence.
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());
}
}
}
/**
* {@inheritdoc}
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
Functions:: |
public | function |
Overrides IndexerBase:: |
|
Functions:: |
public | function |
Overrides IndexerInterface:: |
|
Functions:: |
public | function |
Overrides IndexerBase:: |
|
Functions:: |
public | function |
Overrides IndexerExecutionInterface:: |
|
Functions:: |
public | function |
Overrides IndexerInterface:: |
|
Functions:: |
public | function |
Overrides IndexerBase:: |
|
Functions:: |
public | function |
Overrides IndexerBase:: |
|
Functions:: |
public | function |
Overrides IndexerBase:: |
|
Functions:: |
public | function |
Overrides IndexerUsageInterface:: |
|
Functions:: |
public | function |
Overrides IndexerBase:: |
|
Functions:: |
public | function |
Overrides IndexerBase:: |
|
Functions:: |
public | function |
Overrides IndexerBase:: |
|
Functions:: |
public | function |
Returns if the specified index object can be evaluated and executed safely. Overrides IndexerExecutionInterface:: |
|
Functions:: |
protected | function | ||
IndexerBase:: |
protected | property | ||
IndexerBase:: |
protected | property | ||
IndexerBase:: |
protected | property | ||
IndexerBase:: |
public | function |
Overrides IndexerInterface:: |
|
IndexerBase:: |
public | function |
Overrides IndexerInterface:: |
1 |
IndexerBase:: |
public | function |
Overrides IndexerInterface:: |
|
IndexerBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
IndexerBase:: |
public | function |
Overrides IndexerInterface:: |
|
IndexerBase:: |
public | function |
Overrides IndexerInterface:: |
|
IndexerBase:: |
public | function |
Overrides IndexerInterface:: |
1 |
IndexerBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |