You are here

class ProcessorsService in Purge 8.3

Provides a service that provides access to loaded processors.

Hierarchy

Expanded class hierarchy of ProcessorsService

1 string reference to 'ProcessorsService'
purge.services.yml in ./purge.services.yml
purge.services.yml
1 service uses ProcessorsService
purge.processors in ./purge.services.yml
Drupal\purge\Plugin\Purge\Processor\ProcessorsService

File

src/Plugin/Purge/Processor/ProcessorsService.php, line 14

Namespace

Drupal\purge\Plugin\Purge\Processor
View source
class ProcessorsService extends ServiceBase implements ProcessorsServiceInterface {
  use IteratingServiceBaseTrait;
  use ModifiableServiceBaseTrait;

  /**
   * The factory for configuration objects.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Construct the processors service.
   *
   * @param \Drupal\Component\Plugin\PluginManagerInterface $pluginManager
   *   The plugin manager for this service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   */
  public function __construct(PluginManagerInterface $pluginManager, ConfigFactoryInterface $config_factory) {
    $this->pluginManager = $pluginManager;
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   *
   * @ingroup countable
   */
  public function count() {
    $this
      ->initializePluginInstances();
    return count($this->instances);
  }

  /**
   * {@inheritdoc}
   */
  public function get($plugin_id) {
    $this
      ->initializePluginInstances();
    foreach ($this as $processor) {
      if ($processor
        ->getPluginId() === $plugin_id) {
        return $processor;
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getPluginsEnabled() {
    if (is_null($this->pluginsEnabled)) {

      // Build a mapping of all plugins and whether they are enabled by default.
      $this->pluginsEnabled = [];
      foreach ($this
        ->getPlugins() as $plugin_id => $definition) {
        $enable_by_default = $definition['enable_by_default'] === TRUE;
        $this->pluginsEnabled[$plugin_id] = $enable_by_default;
      }

      // Override the mapping with information stored in CMI, then filter out
      // everything that isn't enabled and finally flip the array with just ids.
      $processors = $this->configFactory
        ->get('purge.plugins')
        ->get('processors');
      if (!is_null($processors)) {
        foreach ($processors as $setting) {
          if (isset($this->pluginsEnabled[$setting['plugin_id']])) {
            $this->pluginsEnabled[$setting['plugin_id']] = $setting['status'];
          }
        }
      }
      foreach ($this->pluginsEnabled as $plugin_id => $status) {
        if (!$status) {
          unset($this->pluginsEnabled[$plugin_id]);
        }
      }
      $this->pluginsEnabled = array_keys($this->pluginsEnabled);
    }
    return $this->pluginsEnabled;
  }

  /**
   * {@inheritdoc}
   */
  public function reload() {
    parent::reload();

    // Without this, the tests will throw "failed to instantiate user-supplied
    // statement class: CREATE TABLE {cache_config}".
    // phpcs:ignore DrupalPractice.Objects.GlobalDrupal.GlobalDrupal
    $this->configFactory = \Drupal::configFactory();

    // Drush commands appreciate it when the config cache gets cleared.
    if (php_sapi_name() === 'cli') {

      // phpcs:ignore DrupalPractice.Objects.GlobalDrupal.GlobalDrupal
      \Drupal::cache('config')
        ->deleteAll();
    }
    $this
      ->reloadIterator();
  }

  /**
   * {@inheritdoc}
   */
  public function setPluginsEnabled(array $plugin_ids) {
    $definitions = $this->pluginManager
      ->getDefinitions();

    // Gather all plugins mentioned in CMI and those available right now, set
    // them disabled first. Then flip the switch for given plugin_ids.
    $setting_assoc = [];
    $instances = $this->configFactory
      ->get('purge.plugins')
      ->get('processors');
    if (!is_null($instances)) {
      foreach ($instances as $inst) {
        $setting_assoc[$inst['plugin_id']] = FALSE;
      }
    }
    foreach ($definitions as $definition) {
      $setting_assoc[$definition['id']] = FALSE;
    }
    foreach ($plugin_ids as $plugin_id) {
      if (!isset($definitions[$plugin_id])) {
        throw new \LogicException('Invalid plugin_id.');
      }
      $setting_assoc[$plugin_id] = TRUE;
    }

    // Convert the array to the CMI storage format and commit.
    $setting = [];
    foreach ($setting_assoc as $plugin_id => $status) {
      $setting[] = [
        'plugin_id' => $plugin_id,
        'status' => $status,
      ];
    }
    $this->configFactory
      ->getEditable('purge.plugins')
      ->set('processors', $setting)
      ->save();
    $this
      ->reload();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
IteratingServiceBaseTrait::$instances protected property Holds all instantiated plugins.
IteratingServiceBaseTrait::$position protected property Current iterator position.
IteratingServiceBaseTrait::current public function Return the current element.
IteratingServiceBaseTrait::initializePluginInstances protected function Instantiate all enabled plugins or check that they are present.
IteratingServiceBaseTrait::key public function Return the key of the current element.
IteratingServiceBaseTrait::next public function Move forward to next element. 1
IteratingServiceBaseTrait::reloadIterator protected function Rewind the iterator and destruct loaded plugin instances.
IteratingServiceBaseTrait::rewind public function Rewind the Iterator to the first element.
IteratingServiceBaseTrait::valid public function Checks if current position is valid.
ModifiableServiceBaseTrait::getPluginsAvailable public function Retrieve the plugin IDs of plugins that can be enabled.
ProcessorsService::$configFactory protected property The factory for configuration objects.
ProcessorsService::count public function
ProcessorsService::get public function Get the requested processor instance. Overrides ProcessorsServiceInterface::get
ProcessorsService::getPluginsEnabled public function Retrieve the configured plugin_ids that the service will use. Overrides ServiceBase::getPluginsEnabled
ProcessorsService::reload public function Reload the service and reinstantiate all enabled plugins. Overrides ServiceBase::reload
ProcessorsService::setPluginsEnabled public function Set the plugins used by the service and reload it. Overrides ModifiableServiceInterface::setPluginsEnabled
ProcessorsService::__construct public function Construct the processors service.
ServiceBase::$pluginManager protected property The plugin manager for the given service.
ServiceBase::$plugins protected property The list of all available plugins and their definitions.
ServiceBase::$pluginsEnabled protected property The list of all enabled plugins and their definitions.
ServiceBase::getPlugins public function Retrieve a list of all available plugins providing the service. Overrides ServiceInterface::getPlugins 1
ServiceBase::isPluginEnabled public function Find out whether the given plugin_id is enabled. Overrides ServiceInterface::isPluginEnabled
ServiceProviderBase::alter public function Modifies existing service definitions. Overrides ServiceModifierInterface::alter 5
ServiceProviderBase::register public function Registers services to the container. Overrides ServiceProviderInterface::register 1