tarte_au_citron-1.0.0-beta1/src/ServicesManager.php

src/ServicesManager.php
<?php

namespace Drupal\tarte_au_citron;

use Drupal\Component\Plugin\Mapper\MapperInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Session\AccountProxyInterface;

/**
 * Gathers the services plugins.
 */
class ServicesManager extends DefaultPluginManager implements ServicesManagerInterface, MapperInterface {

  /**
   * List of all available services.
   *
   * @var array
   */
  protected $optionList = NULL;

  /**
   * The config object.
   *
   * @var \Drupal\Core\Config\ImmutableConfig|null
   */
  protected $config = NULL;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * The tarte_au_citron cache bin.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;

  /**
   * Constructs a new SectionStorageManager object.
   *
   * @param \Traversable $namespaces
   *   An object that implements \Traversable which contains the root paths
   *   keyed by the corresponding namespace to look for plugin implementations.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   Cache backend instance to use.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler to invoke the alter hook with.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *   The current user.
   */
  public function __construct(
    \Traversable $namespaces,
    CacheBackendInterface $cache_backend,
    ModuleHandlerInterface $module_handler,
    ConfigFactoryInterface $config_factory,
    AccountProxyInterface $current_user,
  ) {
    parent::__construct(
      'Plugin/tarte_au_citron',
      $namespaces,
      $module_handler,
      'Drupal\tarte_au_citron\ServicePluginInterface',
      'Drupal\tarte_au_citron\Annotation\TarteAuCitronService'
    );
    $this->alterInfo('tarte_au_citron_services_info');
    $this->setCacheBackend($cache_backend, 'tarte_au_citron_plugins');

    $this->config = $config_factory->get('tarte_au_citron.settings');
    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public function getServicesOptionList(): array {
    if (!isset($this->optionList)) {
      $this->optionList = [];
      foreach ($this->getDefinitions() as $id => $definition) {
        $this->optionList[$id] = $definition['title'];
      }
      asort($this->optionList, SORT_STRING | SORT_FLAG_CASE | SORT_NATURAL);
    }

    return $this->optionList;
  }

  /**
   * {@inheritdoc}
   */
  public function getServices(bool $enabled = FALSE): array {
    $enabledServices = $this->config->get('services');

    $services = [];
    foreach ($this->getServicesOptionList() as $currentServiceId => $currentServiceLabel) {
      if ($enabled && empty($enabledServices[$currentServiceId])) {
        continue;
      }

      $config = !empty($this->config->get('services_settings')[$currentServiceId]) ?
        $this->config->get('services_settings')[$currentServiceId] :
        [];
      $services[$currentServiceId] = $this->createInstance($currentServiceId, [
        'enabled' => !empty($enabledServices[$currentServiceId]),
        'settings' => $config,
      ]);
    }

    return $services;
  }

  /**
   * {@inheritdoc}
   */
  public function isServiceEnabled(string $serviceId): bool {
    $enabledServices = $this->config->get('services');

    return !empty($enabledServices[$serviceId]);
  }

  /**
   * {@inheritdoc}
   */
  public function isNeeded(): bool {
    return !$this->currentUser->hasPermission('bypass tarte au citron');
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc