You are here

class PluginManager in Backup and Migrate 8.4

Class PluginManager.

@package BackupMigrate\Core\Plugin

Hierarchy

Expanded class hierarchy of PluginManager

2 files declare their use of PluginManager
BackupMigrate.php in lib/backup_migrate_core/src/Main/BackupMigrate.php
DBExcludeFilter.php in lib/backup_migrate_core/src/Filter/DBExcludeFilter.php
1 string reference to 'PluginManager'
BackupMigrate::__construct in lib/backup_migrate_core/src/Main/BackupMigrate.php

File

lib/backup_migrate_core/src/Plugin/PluginManager.php, line 17

Namespace

BackupMigrate\Core\Plugin
View source
class PluginManager implements PluginManagerInterface, ConfigurableInterface {
  use ConfigurableTrait;

  /**
   * @var \BackupMigrate\Core\Plugin\PluginInterface[]
   */
  protected $items;

  /**
   * @var \BackupMigrate\Core\Service\ServiceManagerInterface
   */
  protected $services;

  /**
   * @var \BackupMigrate\Core\File\TempFileManagerInterface
   */
  protected $tempFileManager;

  /**
   * @param \BackupMigrate\Core\Service\ServiceManagerInterface $services
   * @param \BackupMigrate\Core\Config\ConfigInterface $config
   */
  public function __construct(ServiceManagerInterface $services = NULL, ConfigInterface $config = NULL) {

    // Add the injected service locator for dependency injection into plugins.
    $this
      ->setServiceManager($services ? $services : new ServiceManager());

    // Set the configuration or a null object if no config was specified.
    $this
      ->setConfig($config ? $config : new Config());

    // Create an array to store the plugins themselves.
    $this->items = [];
  }

  /**
   * Set the configuration. Reconfigure all of the installed plugins.
   *
   * @param \BackupMigrate\Core\Config\ConfigInterface $config
   */
  public function setConfig(ConfigInterface $config) {

    // Set the configuration object to the one passed in.
    $this->config = $config;

    // Pass the appropriate configuration to each of the installed plugins.
    foreach ($this
      ->getAll() as $key => $plugin) {
      $this
        ->_configurePlugin($plugin, $key);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function add($id, PluginInterface $item) {
    $this
      ->_preparePlugin($item, $id);
    $this->items[$id] = $item;
  }

  /**
   * {@inheritdoc}
   **/
  public function get($id) {
    return isset($this->items[$id]) ? $this->items[$id] : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getAll() {
    return empty($this->items) ? [] : $this->items;
  }

  /**
   * Get all plugins that implement the given operation.
   *
   * @param string $op The name of the operation.
   *
   * @return \BackupMigrate\Core\Plugin\PluginInterface[]
   */
  public function getAllByOp($op) {
    $out = [];
    $weights = [];
    foreach ($this
      ->getAll() as $key => $plugin) {
      if ($plugin
        ->supportsOp($op)) {
        $out[$key] = $plugin;
        $weights[$key] = $plugin
          ->opWeight($op);
      }
    }
    array_multisort($weights, $out);
    return $out;
  }

  /**
   * {@inheritdoc}
   */
  public function call($op, $operand = NULL, $params = []) {

    // Run each of the installed plugins which implements the given operation.
    foreach ($this
      ->getAllByOp($op) as $plugin) {
      $operand = $plugin
        ->{$op}($operand, $params);
    }
    return $operand;
  }

  /**
   * {@inheritdoc}
   */
  public function map($op, $params = []) {
    $out = [];

    // Run each of the installed plugins which implements the given operation.
    foreach ($this
      ->getAllByOp($op) as $key => $plugin) {
      $out[$key] = $plugin
        ->{$op}($params);
    }
    return $out;
  }

  /**
   * Prepare the plugin for use. This is called when a plugin is added to the
   * manager and it configures the plugin according to the config object
   * injected into the manager. It also injects other dependencies as needed.
   *
   * @param \BackupMigrate\Core\Plugin\PluginInterface $plugin
   *   The plugin to prepare for use.
   * @param string $id
   *   The id of the plugin (to extract the correct settings).
   */
  protected function _preparePlugin(PluginInterface $plugin, $id) {

    // If this plugin can be configured, then pass in the configuration.
    $this
      ->_configurePlugin($plugin, $id);

    // Inject the available services.
    $this
      ->services()
      ->addClient($plugin);
  }

  /**
   * Set the configuration for the given plugin.
   *
   * @param $plugin
   * @param $id
   */
  protected function _configurePlugin(PluginInterface $plugin, $id) {

    // If this plugin can be configured, then pass in the configuration.
    if ($plugin instanceof ConfigurableInterface) {

      // Configure the plugin with the appropriate subset of the configuration.
      $config = $this
        ->confGet($id);

      // Set the config for the plugin.
      $plugin
        ->setConfig(new Config($config));

      // Get the configuration back from the plugin to populate defaults within the manager.
      $this
        ->config()
        ->set($id, $plugin
        ->config());
    }
  }

  /**
   * @return ServiceManagerInterface
   */
  public function services() {
    return $this->services;
  }

  /**
   * @param ServiceManagerInterface $services
   */
  public function setServiceManager($services) {
    $this->services = $services;

    // Inject or re-inject the services.
    foreach ($this
      ->getAll() as $key => $plugin) {
      $this
        ->services()
        ->addClient($plugin);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigurableTrait::$config protected property The object's configuration object.
ConfigurableTrait::$init protected property The initial configuration. These configuration options can be overriden by the config options but will not be overwritten. If the object is re-configured after construction any missing configuration options will revert to these values.
ConfigurableTrait::confGet public function Get a specific value from the configuration.
ConfigurableTrait::config public function Get the configuration object for this item.
ConfigurableTrait::configDefaults public function Get the default values for the plugin. 10
ConfigurableTrait::configErrors public function Get any validation errors in the config.
ConfigurableTrait::configSchema public function Get a default (blank) schema. 10
PluginManager::$items protected property
PluginManager::$services protected property
PluginManager::$tempFileManager protected property
PluginManager::add public function Add an item to the manager. Overrides PluginManagerInterface::add
PluginManager::call public function Call all plugins which support the given operation. Overrides PluginManagerInterface::call
PluginManager::get public function Get the item with the given id. Overrides PluginManagerInterface::get
PluginManager::getAll public function Get a list of all of the items. Overrides PluginManagerInterface::getAll
PluginManager::getAllByOp public function Get all plugins that implement the given operation. Overrides PluginManagerInterface::getAllByOp
PluginManager::map public function Call all plugins which support the given operation. Return the results in an array keyed by the plugin id. Overrides PluginManagerInterface::map
PluginManager::services public function
PluginManager::setConfig public function Set the configuration. Reconfigure all of the installed plugins. Overrides ConfigurableTrait::setConfig
PluginManager::setServiceManager public function
PluginManager::_configurePlugin protected function Set the configuration for the given plugin.
PluginManager::_preparePlugin protected function Prepare the plugin for use. This is called when a plugin is added to the manager and it configures the plugin according to the config object injected into the manager. It also injects other dependencies as needed.
PluginManager::__construct public function Overrides ConfigurableTrait::__construct
TranslatableTrait::$translator protected property
TranslatableTrait::setTranslator public function
TranslatableTrait::t public function Translate the given string if there is a translator service available.