QueuersService.php in Purge 8.3
File
src/Plugin/Purge/Queuer/QueuersService.php
View source
<?php
namespace Drupal\purge\Plugin\Purge\Queuer;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\purge\IteratingServiceBaseTrait;
use Drupal\purge\ModifiableServiceBaseTrait;
use Drupal\purge\ServiceBase;
class QueuersService extends ServiceBase implements QueuersServiceInterface {
use IteratingServiceBaseTrait;
use ModifiableServiceBaseTrait;
protected $configFactory;
public function __construct(PluginManagerInterface $pluginManager, ConfigFactoryInterface $config_factory) {
$this->pluginManager = $pluginManager;
$this->configFactory = $config_factory;
}
public function count() {
$this
->initializePluginInstances();
return count($this->instances);
}
public function get($plugin_id) {
$this
->initializePluginInstances();
foreach ($this as $queuer) {
if ($queuer
->getPluginId() === $plugin_id) {
return $queuer;
}
}
return FALSE;
}
public function getPluginsEnabled() {
if (is_null($this->pluginsEnabled)) {
$this->pluginsEnabled = [];
foreach ($this
->getPlugins() as $plugin_id => $definition) {
$enable_by_default = $definition['enable_by_default'] === TRUE;
$this->pluginsEnabled[$plugin_id] = $enable_by_default;
}
$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;
}
public function reload() {
parent::reload();
$this->configFactory = \Drupal::configFactory();
if (php_sapi_name() === 'cli') {
\Drupal::cache('config')
->deleteAll();
}
$this
->reloadIterator();
}
public function setPluginsEnabled(array $plugin_ids) {
$definitions = $this->pluginManager
->getDefinitions();
$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;
}
$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();
}
}
Classes
Name |
Description |
QueuersService |
Provides a service that provides access to loaded queuers. |