View source
<?php
namespace Drupal\domain_alias;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\domain\DomainInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class DomainAliasStorage extends ConfigEntityStorage implements DomainAliasStorageInterface {
protected $typedConfig;
protected $requestStack;
protected function setTypedConfigManager(TypedConfigManagerInterface $typed_config) {
$this->typedConfig = $typed_config;
}
protected function setRequestStack(RequestStack $request_stack) {
$this->requestStack = $request_stack;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
$instance = parent::createInstance($container, $entity_type);
$instance
->setTypedConfigManager($container
->get('config.typed'));
$instance
->setRequestStack($container
->get('request_stack'));
return $instance;
}
public function loadSchema() {
$fields = $this->typedConfig
->getDefinition('domain_alias.alias.*');
return isset($fields['mapping']) ? $fields['mapping'] : [];
}
public function loadByHostname($hostname) {
$patterns = $this
->getPatterns($hostname);
foreach ($patterns as $pattern) {
if ($alias = $this
->loadByPattern($pattern)) {
return $alias;
}
}
return NULL;
}
public function loadByPattern($pattern) {
$result = $this
->loadByProperties([
'pattern' => $pattern,
]);
if (empty($result)) {
return NULL;
}
return current($result);
}
public function loadByEnvironment($environment) {
$result = $this
->loadByProperties([
'environment' => $environment,
]);
if (empty($result)) {
return NULL;
}
return $result;
}
public function loadByEnvironmentMatch(DomainInterface $domain, $environment) {
$result = $this
->loadByProperties([
'domain_id' => $domain
->id(),
'environment' => $environment,
]);
if (empty($result)) {
return [];
}
return $result;
}
public function sort($a, $b) {
if (substr_count($a, '*') > substr_count($b, '*') || strlen($a) < strlen($b)) {
return 1;
}
return 0;
}
public function getPatterns($hostname) {
$parts = explode('.', $hostname);
$count = count($parts);
$port = NULL;
if (substr_count($hostname, ':') > 0) {
$ports = explode(':', $parts[$count - 1]);
$parts[$count - 1] = preg_replace('/:(\\d+)/', '', $parts[$count - 1]);
$port = $ports[1];
}
$patterns = $this
->buildPatterns($parts);
uasort($patterns, [
$this,
'sort',
]);
array_unshift($patterns, implode('.', $parts));
$patterns = $this
->buildPortPatterns($patterns, $hostname, $port);
return array_unique($patterns);
}
private function buildPatterns(array $parts) {
$count = count($parts);
for ($i = 0; $i < $count; $i++) {
$temp = $parts;
$temp[$i] = '*';
$patterns[] = implode('.', $temp);
if (count($temp) > 2 && $i < $count - 1) {
$temp[$i + 1] = '*';
$patterns[] = implode('.', $temp);
}
if ($count > 3 && $i < $count - 2) {
$temp[$i + 2] = '*';
$patterns[] = implode('.', $temp);
}
if ($count > 3 && $i < 2) {
$temp = $parts;
$temp[$i] = '*';
$temp[$i + 2] = '*';
$patterns[] = implode('.', $temp);
}
if ($count > 2) {
$temp = array_fill(0, $count, '*');
$temp[$i] = $parts[$i];
$patterns[] = implode('.', $temp);
}
}
return $patterns;
}
private function buildPortPatterns(array $patterns, $hostname, $port = NULL) {
if (empty($port) && !empty($this->requestStack
->getCurrentRequest())) {
$port = $this->requestStack
->getCurrentRequest()
->getPort();
}
$new_patterns = [];
foreach ($patterns as $index => $pattern) {
$new_patterns[] = $pattern . ':*';
if (empty($port) || $port == 80 || $port == 443) {
$new_patterns[] = $pattern;
}
if (!empty($port)) {
$new_patterns[] = $pattern . ':' . $port;
}
}
return $new_patterns;
}
}