View source
<?php
namespace Drupal\domain;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
class DomainStorage extends ConfigEntityStorage implements DomainStorageInterface {
protected $typedConfig;
public function __construct(EntityTypeInterface $entity_type, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache, TypedConfigManagerInterface $typed_config) {
parent::__construct($entity_type, $config_factory, $uuid_service, $language_manager, $memory_cache);
$this->typedConfig = $typed_config;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('config.factory'), $container
->get('uuid'), $container
->get('language_manager'), $container
->get('entity.memory_cache'), $container
->get('config.typed'));
}
public function loadSchema() {
$fields = $this->typedConfig
->getDefinition('domain.record.*');
return isset($fields['mapping']) ? $fields['mapping'] : [];
}
public function loadDefaultId() {
$result = $this
->loadDefaultDomain();
if (!empty($result)) {
return $result
->id();
}
return NULL;
}
public function loadDefaultDomain() {
$result = $this
->loadByProperties([
'is_default' => TRUE,
]);
if (!empty($result)) {
return current($result);
}
return NULL;
}
public function loadMultipleSorted(array $ids = NULL) {
$domains = $this
->loadMultiple($ids);
uasort($domains, [
$this,
'sort',
]);
return $domains;
}
public function loadByHostname($hostname) {
$hostname = $this
->prepareHostname($hostname);
$result = $this
->loadByProperties([
'hostname' => $hostname,
]);
if (empty($result)) {
return NULL;
}
return current($result);
}
public function loadOptionsList() {
$list = [];
foreach ($this
->loadMultipleSorted() as $id => $domain) {
$list[$id] = $domain
->label();
}
return $list;
}
public function sort(DomainInterface $a, DomainInterface $b) {
$weight_difference = $a
->getWeight() - $b
->getWeight();
if ($weight_difference !== 0) {
return $weight_difference;
}
return strcmp($a
->label(), $b
->label());
}
public function prepareHostname($hostname) {
$ignore_www = $this->configFactory
->get('domain.settings')
->get('www_prefix');
if ($ignore_www && substr($hostname, 0, 4) == 'www.') {
$hostname = substr($hostname, 4);
}
return $hostname;
}
public function create(array $values = []) {
$default = $this
->loadDefaultId();
$domains = $this
->loadMultiple();
if (empty($values)) {
$values['hostname'] = $this
->createHostname();
$values['name'] = \Drupal::config('system.site')
->get('name');
}
$values += [
'scheme' => $this
->getDefaultScheme(),
'status' => 1,
'weight' => count($domains) + 1,
'is_default' => (int) empty($default),
];
$domain = parent::create($values);
return $domain;
}
public function createHostname() {
return \Drupal::service('domain.negotiator')
->negotiateActiveHostname();
}
public function createMachineName($hostname = NULL) {
if (empty($hostname)) {
$hostname = $this
->createHostname();
}
return preg_replace('/[^a-z0-9_]/', '_', $hostname);
}
public function getDefaultScheme() {
$request = \Drupal::request();
if (!empty($request)) {
$scheme = $request
->getScheme();
}
elseif (!empty($_SERVER['https'])) {
$scheme = 'https';
}
else {
$scheme = 'http';
}
return $scheme;
}
}