You are here

class QueuersService in Purge 8.3

Provides a service that provides access to loaded queuers.

Hierarchy

Expanded class hierarchy of QueuersService

1 string reference to 'QueuersService'
purge.services.yml in ./purge.services.yml
purge.services.yml
1 service uses QueuersService
purge.queuers in ./purge.services.yml
Drupal\purge\Plugin\Purge\Queuer\QueuersService

File

src/Plugin/Purge/Queuer/QueuersService.php, line 14

Namespace

Drupal\purge\Plugin\Purge\Queuer
View source
class QueuersService extends ServiceBase implements QueuersServiceInterface {
  use IteratingServiceBaseTrait;
  use ModifiableServiceBaseTrait;

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

  /**
   * Construct the queuers 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 $queuer) {
      if ($queuer
        ->getPluginId() === $plugin_id) {
        return $queuer;
      }
    }
    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.
      $queuers = $this->configFactory
        ->get('purge.plugins')
        ->get('queuers');
      if (!is_null($queuers)) {
        foreach ($queuers as $inst) {
          if (isset($this->pluginsEnabled[$inst['plugin_id']])) {
            $this->pluginsEnabled[$inst['plugin_id']] = $inst['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('queuers');
    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('queuers', $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.
QueuersService::$configFactory protected property The factory for configuration objects.
QueuersService::count public function
QueuersService::get public function Get the requested queuer instance. Overrides QueuersServiceInterface::get
QueuersService::getPluginsEnabled public function Retrieve the configured plugin_ids that the service will use. Overrides ServiceBase::getPluginsEnabled
QueuersService::reload public function Reload the service and reinstantiate all enabled plugins. Overrides ServiceBase::reload
QueuersService::setPluginsEnabled public function Set the plugins used by the service and reload it. Overrides ModifiableServiceInterface::setPluginsEnabled
QueuersService::__construct public function Construct the queuers 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