You are here

class DomainNegotiator in Domain Access 8

Hierarchy

Expanded class hierarchy of DomainNegotiator

2 files declare their use of DomainNegotiator
Domain.php in domain/src/Plugin/Condition/Domain.php
Domain.php in domain/src/Plugin/views/argument_default/Domain.php
1 string reference to 'DomainNegotiator'
domain.services.yml in domain/domain.services.yml
domain/domain.services.yml
1 service uses DomainNegotiator
domain.negotiator in domain/domain.services.yml
Drupal\domain\DomainNegotiator

File

domain/src/DomainNegotiator.php, line 13

Namespace

Drupal\domain
View source
class DomainNegotiator implements DomainNegotiatorInterface {

  /**
   * Defines record matching types when dealing with request alteration.
   *
   * @see hook_domain_request_alter().
   *
   * @deprecated These constant will be replaced in the final release by
   * Drupal\domain\DomainNegotiatorInterface. Note that the new versions have
   * been renamed to maintain beta compatibility.
   */
  const DOMAIN_MATCH_NONE = 0;
  const DOMAIN_MATCH_EXACT = 1;
  const DOMAIN_MATCH_ALIAS = 2;

  /**
   * The HTTP_HOST value of the request.
   *
   * @var string
   */
  protected $httpHost;

  /**
   * The domain record returned by the lookup request.
   *
   * @var \Drupal\domain\DomainInterface
   */
  protected $domain;

  /**
   * The domain storage class.
   *
   * @var \Drupal\domain\DomainStorageInterface|null
   */
  protected $domainStorage;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

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

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

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

  /**
   * Constructs a DomainNegotiator object.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   *   The request stack object.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(RequestStack $requestStack, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory) {
    $this->requestStack = $requestStack;
    $this->moduleHandler = $module_handler;
    $this->entityTypeManager = $entity_type_manager;
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function setRequestDomain($httpHost, $reset = FALSE) {

    // @TODO: Investigate caching methods.
    $this
      ->setHttpHost($httpHost);

    // Try to load a direct match.
    if ($domain = $this
      ->domainStorage()
      ->loadByHostname($httpHost)) {

      // If the load worked, set an exact match flag for the hook.
      $domain
        ->setMatchType(DomainNegotiatorInterface::DOMAIN_MATCHED_EXACT);
    }
    else {
      $values = [
        'hostname' => $httpHost,
      ];

      /** @var \Drupal\domain\DomainInterface $domain */
      $domain = $this
        ->domainStorage()
        ->create($values);
      $domain
        ->setMatchType(DomainNegotiatorInterface::DOMAIN_MATCHED_NONE);
    }

    // Now check with modules (like Domain Alias) that register alternate
    // lookup systems with the main module.
    $this->moduleHandler
      ->alter('domain_request', $domain);

    // We must have registered a valid id, else the request made no match.
    if (!empty($domain
      ->id())) {
      $this
        ->setActiveDomain($domain);
    }
    elseif ($domain = $this
      ->domainStorage()
      ->loadDefaultDomain()) {
      $this->moduleHandler
        ->alter('domain_request', $domain);
      $domain
        ->setMatchType(DomainNegotiatorInterface::DOMAIN_MATCHED_NONE);
      if (!empty($domain
        ->id())) {
        $this
          ->setActiveDomain($domain);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setActiveDomain(DomainInterface $domain) {

    // @TODO: caching
    $this->domain = $domain;
  }

  /**
   * Determine the active domain.
   */
  protected function negotiateActiveDomain() {
    $httpHost = $this
      ->negotiateActiveHostname();
    $this
      ->setRequestDomain($httpHost);
    return $this->domain;
  }

  /**
   * {@inheritdoc}
   */
  public function getActiveDomain($reset = FALSE) {
    if ($reset) {
      $this
        ->negotiateActiveDomain();
    }
    return $this->domain;
  }

  /**
   * {@inheritdoc}
   */
  public function getActiveId() {
    return $this->domain
      ->id();
  }

  /**
   * {@inheritdoc}
   */
  public function negotiateActiveHostname() {
    if ($request = $this->requestStack
      ->getCurrentRequest()) {
      $httpHost = $request
        ->getHttpHost();
    }
    else {
      $httpHost = $_SERVER['HTTP_HOST'] ?? NULL;
    }
    $hostname = !empty($httpHost) ? $httpHost : 'localhost';
    return $this
      ->domainStorage()
      ->prepareHostname($hostname);
  }

  /**
   * {@inheritdoc}
   */
  public function setHttpHost($httpHost) {
    $this->httpHost = $httpHost;
  }

  /**
   * {@inheritdoc}
   */
  public function getHttpHost() {
    return $this->httpHost;
  }

  /**
   * {@inheritdoc}
   */
  public function isRegisteredDomain($hostname) {

    // Direct hostname match always passes.
    if ($domain = $this
      ->domainStorage()
      ->loadByHostname($hostname)) {
      return TRUE;
    }

    // Check for registered alias matches.
    $values = [
      'hostname' => $hostname,
    ];

    /** @var \Drupal\domain\DomainInterface $domain */
    $domain = $this
      ->domainStorage()
      ->create($values);
    $domain
      ->setMatchType(DomainNegotiatorInterface::DOMAIN_MATCHED_NONE);

    // Now check with modules (like Domain Alias) that register alternate
    // lookup systems with the main module.
    $this->moduleHandler
      ->alter('domain_request', $domain);

    // We must have registered a valid id, else the request made no match.
    if (!empty($domain
      ->id())) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Retrieves the domain storage handler.
   *
   * @return \Drupal\domain\DomainStorageInterface
   *   The domain storage handler.
   */
  protected function domainStorage() {
    if (!$this->domainStorage) {
      $this->domainStorage = $this->entityTypeManager
        ->getStorage('domain');
    }
    return $this->domainStorage;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DomainNegotiator::$configFactory protected property The config factory.
DomainNegotiator::$domain protected property The domain record returned by the lookup request.
DomainNegotiator::$domainStorage protected property The domain storage class.
DomainNegotiator::$entityTypeManager protected property The entity type manager.
DomainNegotiator::$httpHost protected property The HTTP_HOST value of the request.
DomainNegotiator::$moduleHandler protected property The module handler.
DomainNegotiator::$requestStack protected property The request stack object.
DomainNegotiator::domainStorage protected function Retrieves the domain storage handler.
DomainNegotiator::DOMAIN_MATCH_ALIAS constant
DomainNegotiator::DOMAIN_MATCH_EXACT constant
DomainNegotiator::DOMAIN_MATCH_NONE Deprecated constant Defines record matching types when dealing with request alteration.
DomainNegotiator::getActiveDomain public function Gets the active domain. Overrides DomainNegotiatorInterface::getActiveDomain
DomainNegotiator::getActiveId public function Gets the id of the active domain. Overrides DomainNegotiatorInterface::getActiveId
DomainNegotiator::getHttpHost public function Gets the inbound httpHost request. Overrides DomainNegotiatorInterface::getHttpHost
DomainNegotiator::isRegisteredDomain public function Checks that a URL's hostname is registered as a valid domain or alias. Overrides DomainNegotiatorInterface::isRegisteredDomain
DomainNegotiator::negotiateActiveDomain protected function Determine the active domain.
DomainNegotiator::negotiateActiveHostname public function Sets the hostname of the active request. Overrides DomainNegotiatorInterface::negotiateActiveHostname
DomainNegotiator::setActiveDomain public function Sets the active domain. Overrides DomainNegotiatorInterface::setActiveDomain
DomainNegotiator::setHttpHost public function Stores the inbound httpHost request. Overrides DomainNegotiatorInterface::setHttpHost
DomainNegotiator::setRequestDomain public function Determines the active domain request. Overrides DomainNegotiatorInterface::setRequestDomain
DomainNegotiator::__construct public function Constructs a DomainNegotiator object.
DomainNegotiatorInterface::DOMAIN_MATCHED_ALIAS constant An alias pattern match found.
DomainNegotiatorInterface::DOMAIN_MATCHED_EXACT constant An exact domain record string match found.
DomainNegotiatorInterface::DOMAIN_MATCHED_NONE constant Defines record matching types when dealing with request alteration.