whitelabel-8.x-2.x-dev/src/WhiteLabelNegotiator.php

src/WhiteLabelNegotiator.php
<?php

namespace Drupal\whitelabel;

use Drupal\Component\Plugin\PluginManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * The default White label negotiator.
 */
class WhiteLabelNegotiator implements WhiteLabelNegotiatorInterface {

  /**
   * The white label provider.
   *
   * @var \Drupal\whitelabel\WhiteLabelProviderInterface
   */
  protected $whiteLabelProvider;

  /**
   * The white label negotiation method plugin manager.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $negotiatorMethodManager;

  /**
   * The request stack object.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Local cache for white label negotiation method instances.
   *
   * @var \Drupal\whitelabel\WhiteLabelNegotiationMethodInterface[]
   */
  protected $methods;

  /**
   * Construct a WhiteLabelNegotiator.
   */
  public function __construct(WhiteLabelProviderInterface $whitelabel_provider, PluginManagerInterface $negotiator_manager, RequestStack $requestStack) {
    $this->whiteLabelProvider = $whitelabel_provider;
    $this->negotiatorMethodManager = $negotiator_manager;
    $this->requestStack = $requestStack;
  }

  /**
   * {@inheritdoc}
   */
  public function initWhiteLabelManager() {
    $this->whiteLabelProvider->setNegotiator($this);
  }

  /**
   * {@inheritdoc}
   */
  public function reset() {
    $this->methods = [];
  }

  /**
   * {@inheritdoc}
   */
  public function init() {
    $white_label = NULL;

    // Iterate over the negotiators until one returns a white label.
    foreach ($this->getNegotiationMethods() as $method_id => $type) {
      if (!isset($white_label)) {
        $white_label = $this->negotiateWhiteLabel($method_id);
      }
      if ($white_label) {
        return $white_label;
      }
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function negotiateWhiteLabel($method_id) {
    $white_label = $this->getNegotiationMethodInstance($method_id)->getWhiteLabel($this->requestStack->getCurrentRequest());
    return $white_label ?: NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getNegotiationMethods() {
    $definitions = $this->negotiatorMethodManager->getDefinitions();

    $config = \Drupal::config('whitelabel.negotiation');
    foreach ($definitions as $method_id => $definition) {
      $enabled = (bool) $config->get("negotiator_settings.$method_id.enabled");
      if (!$enabled) {
        unset($definitions[$method_id]);
      }
    }

    return $definitions;
  }

  /**
   * {@inheritdoc}
   */
  public function getNegotiationMethodInstance($method_id) {
    $config = \Drupal::config('whitelabel.negotiation');
    $plugin_config = $config->get("negotiator_settings.$method_id.settings") ?? [];

    if (!isset($this->methods[$method_id])) {
      $instance = $this->negotiatorMethodManager->createInstance($method_id, $plugin_config);
      $instance->setWhiteLabelNegotiationManager($this->negotiatorMethodManager);
      $this->methods[$method_id] = $instance;
    }
    return $this->methods[$method_id];
  }

}

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

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