You are here

protected function ConfigActionsService::getSourcePlugin in Config Actions 8

Return an instance of a source plugin.

Parameters

mixed $source:

string $type: source plugin id

string $base: optional base namespace

2 calls to ConfigActionsService::getSourcePlugin()
ConfigActionsService::loadSource in src/ConfigActionsService.php
Load data from a given source plugin.
ConfigActionsService::saveSource in src/ConfigActionsService.php
Save data from a given source plugin.

File

src/ConfigActionsService.php, line 155

Class

ConfigActionsService
Base class for config_actions plugins.

Namespace

Drupal\config_actions

Code

protected function getSourcePlugin($source, $type = '', $base = '') {
  if (is_string($source) && strpos($source, '::') > 0) {
    list($type, $source) = explode('::', $source);
  }

  // Return any cached Source
  $source_key = $this
    ->getSourceKey($source, $type, $base);
  if (isset($this->sourceCache[$source_key])) {
    return $this->sourceCache[$source_key];
  }

  // Create a new Source plugin

  /** @var \Drupal\config_actions\ConfigActionsSourceInterface $source_plugin */
  $source_plugin = NULL;
  $options = array(
    'source' => $source,
    'base' => $base,
  );
  $definitions = $this->sourceManager
    ->getDefinitions();
  if (!empty($type) && isset($definitions[$type])) {

    // First check if we want a specific type of plugin
    $source_plugin = $this->sourceManager
      ->createInstance($type, $options);
  }
  else {

    // Otherwise, run the auto detection on all plugins till we match.
    uasort($definitions, array(
      SortArray::class,
      'sortByWeightElement',
    ));
    foreach ($definitions as $plugin_id => $definition) {

      /** @var \Drupal\config_actions\ConfigActionsSourceInterface $plugin */
      $plugin = $this->sourceManager
        ->createInstance($plugin_id, $options);
      if ($plugin
        ->detect($source)) {
        $source_plugin = $plugin;
        break;
      }
    }
  }
  $this->sourceCache[$source_key] = $source_plugin;
  return $source_plugin;
}