Tamper.php in Tamper 7
Contains TamperPluginBase.
File
src/Tamper.phpView source
<?php
/**
* @file
* Contains TamperPluginBase.
*/
/**
* Wraps a plugin definition array.
*/
class TamperPluginDefinition {
/**
* The plugin definition id.
*
* @var string
*/
protected $id;
/**
* The plugin definition.
*
* @var array
*/
protected $definition;
/**
* Constructs a TamperPluginDefinition object.
*
* @param string $definition_id
* The plugin definition id.
* @param array $definition
* The definition array.
*/
public function __construct($definition_id, array $definition) {
$this->id = $definition_id;
$this->definition = $definition;
}
/**
* Returns a single plugin definition.
*
* @param string $id
* The plugin definition id.
*
* @return self
* A plugin definition object.
*/
public static function load($id) {
ctools_include('plugins');
return new self($id, ctools_get_plugins('tamper', 'plugins', $id));
}
/**
* Returns all plugin definitions.
*
* @return []TamperPluginDefinition
* A list of all available plugin definitions.
*/
public static function loadAll() {
ctools_include('plugins');
$definitions = array();
foreach (ctools_get_plugins('tamper', 'plugins') as $id => $definition) {
$definitions[$id] = new self($id, $definition);
}
return $definitions;
}
/**
* Returns the plugin id.
*
* @return string
* The plugin id.
*/
public function getId() {
return $this->id;
}
/**
* Returns the definition label.
*
* @return string
* The label of the plugin.
*/
public function getLabel() {
return $this->definition['name'];
}
/**
* Returns the plugin class.
*
* @return string
* The plugin class.
*/
protected function getClass() {
return $this->definition['class']['class'];
}
/**
* Determines whether this plugin is compatible with a given module.
*
* @param string $module
* The module to check.
*
* @return bool
* Returns true if compatible, and false if not.
*/
public function isCompatible($module) {
return empty($this->definition['compatible']) || in_array($module, $this->definition['compatible'], TRUE);
}
/**
* Creates a plugin instance.
*
* @param array $configuration
* (optional) The configuration of the plugin.
*
* @return TamperPluginInterface
* Returns the newly constructed plugin.
*/
public function createInstance(array $configuration = array()) {
$class = $this
->getClass();
return new $class($configuration, $this->id, $this);
}
}
/**
* Plugin interface for providing plugins to tamper.
*/
interface TamperPluginInterface {
/**
* Returns the plugin_id of the plugin instance.
*
* @return string
* The plugin_id of the plugin instance.
*/
public function getPluginId();
/**
* Returns the definition of the plugin implementation.
*
* @return array
* The plugin definition, as returned by the discovery object used by the
* plugin manager.
*/
public function getPluginDefinition();
/**
* Returns this plugin's configuration.
*
* @return array
* An array of this plugin's configuration.
*/
public function getConfiguration();
/**
* Sets the configuration for this plugin instance.
*
* @param array $configuration
* An associative array containing the plugin's configuration.
*/
public function setConfiguration(array $configuration);
/**
* Returns default configuration for this plugin.
*
* @return array
* An associative array with the default configuration.
*/
public function defaultConfiguration();
/**
* Form constructor.
*
* @param array $form
* An associative array containing the structure of the form.
* @param array &$form_state
* An associative array containing the current state of the form.
*
* @return array
* The form structure.
*/
public function buildConfigurationForm(array $form, array &$form_state);
/**
* Form validation handler.
*
* @param array &$form
* An associative array containing the structure of the form.
* @param array &$form_state
* An associative array containing the current state of the form.
*/
public function validateConfigurationForm(array &$form, array &$form_state);
/**
* Form submission handler.
*
* @param array &$form
* An associative array containing the structure of the form.
* @param array &$form_state
* An associative array containing the current state of the form.
*/
public function submitConfigurationForm(array &$form, array &$form_state);
/**
* Executes the plugin.
*
* @param string $value
* The value to transform.
*
* @return string
* The transformed value.
*/
public function execute($value);
/**
* Executes the plugin on multiple values.
*
* @param []string $values
* The list of values to execute.
*
* @return []string
* The list of transformed values.
*/
public function executeMultiple(array $values);
}
/**
* Base class for Tamper plugins.
*/
abstract class TamperPluginBase implements TamperPluginInterface {
/**
* The plugin id.
*
* @var string
*/
protected $pluginId;
/**
* The plugin implementation definition.
*
* @var TamperPluginDefinition
*/
protected $pluginDefinition;
/**
* Configuration information passed into the plugin.
*
* @var array
*/
protected $configuration;
/**
* Constructs a TamperPluginBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin id for the plugin instance.
* @param TamperPluginDefinition $plugin_definition
* The plugin implementation definition.
*/
public function __construct(array $configuration, $plugin_id, TamperPluginDefinition $plugin_definition) {
$this->pluginId = $plugin_id;
$this->pluginDefinition = $plugin_definition;
// Calling setConfiguration() ensures the configuration is clean and
// defaults are set.
$this
->setConfiguration($configuration);
}
/**
* {@inheritdoc}
*/
public function getPluginId() {
return $this->pluginId;
}
/**
* {@inheritdoc}
*/
public function getPluginDefinition() {
return $this->pluginDefinition;
}
/**
* {@inheritdoc}
*/
public function getConfiguration($key = NULL) {
if ($key === NULL) {
return $this->configuration;
}
if (isset($this->configuration[$key])) {
return $this->configuration[$key];
}
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$defaults = $this
->defaultConfiguration();
$this->configuration = array_intersect_key($configuration, $defaults) + $defaults;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, array &$form_state) {
// Validation is optional.
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, array &$form_state) {
$this
->setConfiguration($form_state['values']['tamper_configuration']);
}
/**
* {@inheritdoc}
*/
public function executeMultiple(array $values) {
foreach ($values as $delta => $value) {
$values[$delta] = $this
->execute($value);
}
return $values;
}
}
Classes
Name | Description |
---|---|
TamperPluginBase | Base class for Tamper plugins. |
TamperPluginDefinition | Wraps a plugin definition array. |
Interfaces
Name | Description |
---|---|
TamperPluginInterface | Plugin interface for providing plugins to tamper. |