View source
<?php
namespace Drupal\domain\Entity;
use Drupal\Core\Config\ConfigValueException;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\domain\DomainInterface;
use Drupal\domain\DomainNegotiatorInterface;
class Domain extends ConfigEntityBase implements DomainInterface {
use StringTranslationTrait;
protected $id;
protected $domain_id;
protected $name;
protected $hostname;
protected $weight;
protected $is_default = FALSE;
protected $scheme;
protected $path;
protected $url;
protected $response = NULL;
protected $redirect = NULL;
protected $matchType;
protected $canonical;
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$domain_storage = \Drupal::entityTypeManager()
->getStorage('domain');
$default = $domain_storage
->loadDefaultId();
$count = $storage_controller
->getQuery()
->count()
->execute();
$values += [
'scheme' => empty($GLOBALS['is_https']) ? 'http' : 'https',
'status' => 1,
'weight' => $count + 1,
'is_default' => (int) empty($default),
];
}
public function isActive() {
$negotiator = \Drupal::service('domain.negotiator');
$domain = $negotiator
->getActiveDomain();
if (empty($domain)) {
return FALSE;
}
return $this
->id() == $domain
->id();
}
public function addProperty($name, $value) {
if (!isset($this->{$name})) {
$this->{$name} = $value;
}
}
public function isDefault() {
return (bool) $this->is_default;
}
public function isHttps() {
return (bool) ($this
->getScheme(FALSE) == 'https');
}
public function saveDefault() {
if (!$this
->isDefault()) {
if ($default = \Drupal::entityTypeManager()
->getStorage('domain')
->loadDefaultDomain()) {
$default->is_default = FALSE;
$default
->setHostname($default
->getCanonical());
$default
->save();
}
$this->is_default = TRUE;
$this
->setHostname($this
->getCanonical());
$this
->save();
}
else {
\Drupal::messenger()
->addMessage($this
->t('The selected domain is already the default.'), 'warning');
}
}
public function enable() {
$this
->setStatus(TRUE);
$this
->setHostname($this
->getCanonical());
$this
->save();
}
public function disable() {
if (!$this
->isDefault()) {
$this
->setStatus(FALSE);
$this
->setHostname($this
->getCanonical());
$this
->save();
}
else {
\Drupal::messenger()
->addMessage($this
->t('The default domain cannot be disabled.'), 'warning');
}
}
public function saveProperty($name, $value) {
if (isset($this->{$name})) {
$this->{$name} = $value;
$this
->setHostname($this
->getCanonical());
$this
->save();
\Drupal::messenger()
->addMessage($this
->t('The @key attribute was set to @value for domain @hostname.', [
'@key' => $name,
'@value' => $value,
'@hostname' => $this->hostname,
]));
}
else {
\Drupal::messenger()
->addMessage($this
->t('The @key attribute does not exist.', [
'@key' => $name,
]));
}
}
public function setPath() {
global $base_path;
$this->path = $this
->getScheme() . $this
->getHostname() . ($base_path ?: '/');
}
public function setUrl() {
$request = \Drupal::request();
$uri = $request ? $request
->getRequestUri() : '/';
$this->url = $this
->getScheme() . $this
->getHostname() . $uri;
}
public function getPath() {
if (!isset($this->path)) {
$this
->setPath();
}
return $this->path;
}
public function getRawPath() {
return $this
->getScheme() . $this
->getHostname();
}
public function buildUrl($path) {
return $this
->getRawPath() . $path;
}
public function getUrl() {
if (!isset($this->url)) {
$this
->setUrl();
}
return $this->url;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
$default = $storage
->loadDefaultDomain();
if (!$default) {
$this->is_default = TRUE;
}
elseif ($this->is_default && $default
->getDomainId() != $this
->getDomainId()) {
$default->is_default = FALSE;
$default
->save();
}
if ($this
->isNew() && empty($this
->getDomainId())) {
$this
->createDomainId();
}
$hostname = $this
->getHostname();
$existing = $storage
->loadByProperties([
'hostname' => $hostname,
]);
$existing = reset($existing);
if ($existing && $this
->getDomainId() != $existing
->getDomainId()) {
throw new ConfigValueException("The hostname ({$hostname}) is already registered.");
}
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
\Drupal::service('cache_tags.invalidator')
->invalidateTags([
'rendered',
'url.site',
]);
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
foreach ($entities as $entity) {
$actions = $storage
->loadMultiple([
'domain_default_action.' . $entity
->id(),
'domain_delete_action.' . $entity
->id(),
'domain_disable_action.' . $entity
->id(),
'domain_enable_action.' . $entity
->id(),
]);
foreach ($actions as $action) {
$action
->delete();
}
}
\Drupal::service('cache_tags.invalidator')
->invalidateTags([
'rendered',
'url.site',
]);
}
public function createDomainId() {
$id = hash('crc32', $this
->id());
$id = abs(hexdec(substr($id, 0, -2)));
$this
->createNumericId($id);
}
public function createNumericId($id) {
$storage = \Drupal::entityTypeManager()
->getStorage('domain');
$result = $storage
->loadByProperties([
'domain_id' => $id,
]);
if (empty($result)) {
$this->domain_id = $id;
}
else {
$id++;
$this
->createNumericId($id);
}
}
public function getScheme($add_suffix = TRUE) {
$scheme = $this->scheme;
if ($scheme == 'variable') {
$scheme = \Drupal::entityTypeManager()
->getStorage('domain')
->getDefaultScheme();
}
elseif ($scheme != 'https') {
$scheme = 'http';
}
$scheme .= $add_suffix ? '://' : '';
return $scheme;
}
public function getRawScheme() {
return $this->scheme;
}
public function getResponse() {
if (empty($this->response)) {
$validator = \Drupal::service('domain.validator');
$validator
->checkResponse($this);
}
return $this->response;
}
public function setResponse($response) {
$this->response = $response;
}
public function getLink($current_path = TRUE) {
$options = [
'absolute' => TRUE,
'https' => $this
->isHttps(),
];
if ($current_path) {
$url = Url::fromUri($this
->getUrl(), $options);
}
else {
$url = Url::fromUri($this
->getPath(), $options);
}
return Link::fromTextAndUrl($this
->getCanonical(), $url)
->toString();
}
public function getRedirect() {
return $this->redirect;
}
public function setRedirect($code = 302) {
$this->redirect = $code;
}
public function getHostname() {
return $this->hostname;
}
public function setHostname($hostname) {
$this->hostname = $hostname;
}
public function getDomainId() {
return $this->domain_id;
}
public function getWeight() {
return $this->weight;
}
public function setMatchType($match_type = DomainNegotiatorInterface::DOMAIN_MATCHED_EXACT) {
$this->matchType = $match_type;
}
public function getMatchType() {
return $this->matchType;
}
public function getPort() {
$ports = explode(':', $this
->getHostname());
if (isset($ports[1])) {
return ':' . $ports[1];
}
return '';
}
public function setCanonical($hostname = NULL) {
if (is_null($hostname)) {
$this->canonical = $this
->getHostname();
}
else {
$this->canonical = $hostname;
}
}
public function getCanonical() {
if (empty($this->canonical)) {
$this
->setCanonical();
}
return $this->canonical;
}
public function toString() {
return $this->name;
}
}