You are here

abstract class FeedsPlugin in Feeds 7.2

Same name and namespace in other branches
  1. 6 plugins/FeedsPlugin.inc \FeedsPlugin
  2. 7 plugins/FeedsPlugin.inc \FeedsPlugin

Implement source interface for all plugins.

Note how this class does not attempt to store source information locally. Doing this would break the model where source information is represented by an object that is being passed into a Feed object and its plugins.

Hierarchy

Expanded class hierarchy of FeedsPlugin

1 string reference to 'FeedsPlugin'
_feeds_feeds_plugins in ./feeds.plugins.inc
Break out for feeds_feed_plugins().

File

plugins/FeedsPlugin.inc, line 20
Definition of FeedsPlugin class.

View source
abstract class FeedsPlugin extends FeedsConfigurable implements FeedsSourceInterface {

  /**
   * The plugin definition.
   *
   * @var array
   */
  protected $pluginDefinition;

  /**
   * Constructs a FeedsPlugin object.
   *
   * A copy of FeedsConfigurable::__construct() that doesn't call
   * configDefaults() so that we avoid circular dependencies.
   *
   * @param string $id
   *   The importer id.
   */
  protected function __construct($id) {
    $this->id = $id;
    $this->export_type = FEEDS_EXPORT_NONE;
    $this->disabled = FALSE;
  }

  /**
   * Instantiates a FeedsPlugin object.
   *
   * Don't use directly, use feeds_plugin() instead.
   *
   * @see feeds_plugin()
   */
  public static function instance($class, $id, array $plugin_definition = array()) {
    if (!strlen($id)) {
      throw new InvalidArgumentException(t('Empty configuration identifier.'));
    }
    $instances =& drupal_static(__METHOD__, array());
    if (!isset($instances[$class][$id])) {
      $instance = new $class($id);

      // The ordering here is important. The plugin definition should be usable
      // in getConfig().
      $instance
        ->setPluginDefinition($plugin_definition);
      $instance
        ->setConfig($instance
        ->configDefaults());
      $instances[$class][$id] = $instance;
    }
    return $instances[$class][$id];
  }

  /**
   * Returns the type of plugin.
   *
   * @return string
   *   One of either 'fetcher', 'parser', or 'processor'.
   */
  public abstract function pluginType();

  /**
   * Returns the plugin definition.
   *
   * @return array
   *   The plugin definition array.
   *
   * @see ctools_get_plugins()
   */
  public function pluginDefinition() {
    return $this->pluginDefinition;
  }

  /**
   * Sets the plugin definition.
   *
   * This is protected since we're only using it in FeedsPlugin::instance().
   *
   * @param array $plugin_definition
   *   The plugin definition.
   */
  protected function setPluginDefinition(array $plugin_definition) {
    $this->pluginDefinition = $plugin_definition;
  }

  /**
   * Save changes to the configuration of this object.
   * Delegate saving to parent (= Feed) which will collect
   * information from this object by way of getConfig() and store it.
   */
  public function save() {
    feeds_importer($this->id)
      ->save();
  }

  /**
   * Overrides FeedsConfigurable::configDefaults().
   *
   * Invokes a hook to add in additional default configuration.
   */
  public function configDefaults() {
    $hook = 'feeds_' . $this
      ->pluginType() . '_config_defaults';
    return module_invoke_all($hook, $this) + parent::configDefaults();
  }

  /**
   * Returns TRUE if $this->sourceForm() returns a form.
   */
  public function hasSourceConfig() {
    $form = $this
      ->sourceForm(array());
    return !empty($form);
  }

  /**
   * Implements FeedsSourceInterface::sourceDefaults().
   */
  public function sourceDefaults() {
    $values = array_flip(array_keys($this
      ->sourceForm(array())));
    foreach ($values as $k => $v) {
      $values[$k] = '';
    }
    return $values;
  }

  /**
   * Callback methods, exposes source form.
   */
  public function sourceForm($source_config) {
    return array();
  }

  /**
   * Validation handler for sourceForm.
   */
  public function sourceFormValidate(&$source_config) {
  }

  /**
   * A source is being saved.
   */
  public function sourceSave(FeedsSource $source) {
  }

  /**
   * A source is being deleted.
   */
  public function sourceDelete(FeedsSource $source) {
  }

  /**
   * Loads on-behalf implementations from mappers/ directory.
   *
   * FeedsProcessor::map() does not load from mappers/ as only node and user
   * processor ship with on-behalf implementations.
   *
   * @see FeedsNodeProcessor::map()
   * @see FeedsUserProcessor::map()
   *
   * @todo: Use CTools Plugin API.
   */
  public static function loadMappers() {
    static $loaded = FALSE;
    if (!$loaded) {
      $path = drupal_get_path('module', 'feeds') . '/mappers';
      $files = drupal_system_listing('/.*\\.inc$/', $path, 'name', 0);
      foreach ($files as $file) {
        if (strstr($file->uri, '/mappers/')) {
          require_once DRUPAL_ROOT . '/' . $file->uri;
        }
      }
    }
    $loaded = TRUE;
  }

  /**
   * Get all available plugins.
   */
  public static function all() {
    ctools_include('plugins');
    $plugins = ctools_get_plugins('feeds', 'plugins');
    $result = array();
    foreach ($plugins as $key => $info) {
      if (!empty($info['hidden'])) {
        continue;
      }
      $result[$key] = $info;
    }

    // Sort plugins by name and return.
    uasort($result, 'feeds_plugin_compare');
    return $result;
  }

  /**
   * Determines whether given plugin is derived from given base plugin.
   *
   * @param $plugin_key
   *   String that identifies a Feeds plugin key.
   * @param $parent_plugin
   *   String that identifies a Feeds plugin key to be tested against.
   *
   * @return
   *   TRUE if $parent_plugin is directly *or indirectly* a parent of $plugin,
   *   FALSE otherwise.
   */
  public static function child($plugin_key, $parent_plugin) {
    ctools_include('plugins');
    $plugins = ctools_get_plugins('feeds', 'plugins');
    if (!isset($plugins[$plugin_key])) {

      // Plugin is not available.
      return FALSE;
    }
    $info = $plugins[$plugin_key];
    if (empty($info['handler']['parent'])) {
      return FALSE;
    }
    elseif ($info['handler']['parent'] == $parent_plugin) {
      return TRUE;
    }
    else {
      return self::child($info['handler']['parent'], $parent_plugin);
    }
  }

  /**
   * Determines the type of a plugin.
   *
   * @todo PHP5.3: Implement self::type() and query with $plugin_key::type().
   *
   * @param $plugin_key
   *   String that identifies a Feeds plugin key.
   *
   * @return
   *   One of the following values:
   *   'fetcher' if the plugin is a fetcher
   *   'parser' if the plugin is a parser
   *   'processor' if the plugin is a processor
   *   FALSE otherwise.
   */
  public static function typeOf($plugin_key) {
    if (self::child($plugin_key, 'FeedsFetcher')) {
      return 'fetcher';
    }
    elseif (self::child($plugin_key, 'FeedsParser')) {
      return 'parser';
    }
    elseif (self::child($plugin_key, 'FeedsProcessor')) {
      return 'processor';
    }
    return FALSE;
  }

  /**
   * Gets all available plugins of a particular type.
   *
   * @param $type
   *   'fetcher', 'parser' or 'processor'
   */
  public static function byType($type) {
    $plugins = self::all();
    $result = array();
    foreach ($plugins as $key => $info) {
      if ($type == self::typeOf($key)) {
        $result[$key] = $info;
      }
    }
    return $result;
  }

  /**
   * Implements FeedsConfigurable::dependencies().
   */
  public function dependencies() {
    $dependencies = parent::dependencies();

    // Find out which module provides this plugin.
    $plugin_info = $this
      ->pluginDefinition();
    if (isset($plugin_info['module'])) {
      $dependencies[$plugin_info['module']] = $plugin_info['module'];
    }
    return $dependencies;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsConfigurable::$config protected property Holds the actual configuration information.
FeedsConfigurable::$disabled protected property CTools export enabled status of this object.
FeedsConfigurable::$export_type protected property CTools export type of this object.
FeedsConfigurable::$id protected property An unique identifier for the configuration.
FeedsConfigurable::addConfig public function Similar to setConfig but adds to existing configuration.
FeedsConfigurable::configForm public function Returns configuration form for this object. 6
FeedsConfigurable::configFormSubmit public function Submission handler for configForm(). 2
FeedsConfigurable::configFormValidate public function Validation handler for configForm(). 3
FeedsConfigurable::copy public function Copy a configuration. 1
FeedsConfigurable::doesExist public function Determine whether this object is persistent. 1
FeedsConfigurable::existing public function Determines whether this object is persistent and enabled. 1
FeedsConfigurable::getConfig public function Implements getConfig(). 1
FeedsConfigurable::hasConfigForm public function Returns whether or not the configurable has a config form.
FeedsConfigurable::isEnabled public function Determine whether this object is enabled.
FeedsConfigurable::setConfig public function Set configuration.
FeedsConfigurable::validateConfig public function Validates the configuration. 2
FeedsConfigurable::__get public function Overrides magic method __get().
FeedsConfigurable::__isset public function Override magic method __isset(). This is needed due to overriding __get().
FeedsPlugin::$pluginDefinition protected property The plugin definition.
FeedsPlugin::all public static function Get all available plugins.
FeedsPlugin::byType public static function Gets all available plugins of a particular type.
FeedsPlugin::child public static function Determines whether given plugin is derived from given base plugin.
FeedsPlugin::configDefaults public function Overrides FeedsConfigurable::configDefaults(). Overrides FeedsConfigurable::configDefaults 4
FeedsPlugin::dependencies public function Implements FeedsConfigurable::dependencies(). Overrides FeedsConfigurable::dependencies 1
FeedsPlugin::hasSourceConfig public function Returns TRUE if $this->sourceForm() returns a form. Overrides FeedsSourceInterface::hasSourceConfig
FeedsPlugin::instance public static function Instantiates a FeedsPlugin object. Overrides FeedsConfigurable::instance
FeedsPlugin::loadMappers public static function Loads on-behalf implementations from mappers/ directory.
FeedsPlugin::pluginDefinition public function Returns the plugin definition.
FeedsPlugin::pluginType abstract public function Returns the type of plugin. 4
FeedsPlugin::save public function Save changes to the configuration of this object. Delegate saving to parent (= Feed) which will collect information from this object by way of getConfig() and store it. Overrides FeedsConfigurable::save 1
FeedsPlugin::setPluginDefinition protected function Sets the plugin definition.
FeedsPlugin::sourceDefaults public function Implements FeedsSourceInterface::sourceDefaults(). Overrides FeedsSourceInterface::sourceDefaults 1
FeedsPlugin::sourceDelete public function A source is being deleted. Overrides FeedsSourceInterface::sourceDelete 2
FeedsPlugin::sourceForm public function Callback methods, exposes source form. Overrides FeedsSourceInterface::sourceForm 3
FeedsPlugin::sourceFormValidate public function Validation handler for sourceForm. Overrides FeedsSourceInterface::sourceFormValidate 2
FeedsPlugin::sourceSave public function A source is being saved. Overrides FeedsSourceInterface::sourceSave 2
FeedsPlugin::typeOf public static function Determines the type of a plugin.
FeedsPlugin::__construct protected function Constructs a FeedsPlugin object. Overrides FeedsConfigurable::__construct