DomainStorage.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace Drupal\domain;
  3. use Drupal\Component\Uuid\UuidInterface;
  4. use Drupal\Core\Config\Entity\ConfigEntityStorage;
  5. use Drupal\Core\Entity\EntityTypeInterface;
  6. use Drupal\Core\Language\LanguageManagerInterface;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Drupal\Core\Config\ConfigFactoryInterface;
  9. use Drupal\Core\Config\TypedConfigManagerInterface;
  10. /**
  11. * Loads Domain records.
  12. */
  13. class DomainStorage extends ConfigEntityStorage implements DomainStorageInterface {
  14. /**
  15. * The typed config handler.
  16. *
  17. * @var \Drupal\Core\Config\TypedConfigManagerInterface
  18. */
  19. protected $typedConfig;
  20. /**
  21. * Constructs a DomainStorage object.
  22. *
  23. * Trying to inject the storage manager throws an exception.
  24. *
  25. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  26. * The entity type definition.
  27. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  28. * The config factory service.
  29. * @param \Drupal\Component\Uuid\UuidInterface $uuid_service
  30. * The UUID service.
  31. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
  32. * The language manager.
  33. * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
  34. * The typed config handler.
  35. */
  36. public function __construct(EntityTypeInterface $entity_type, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, TypedConfigManagerInterface $typed_config) {
  37. parent::__construct($entity_type, $config_factory, $uuid_service, $language_manager);
  38. $this->configFactory = $config_factory;
  39. $this->uuidService = $uuid_service;
  40. $this->languageManager = $language_manager;
  41. $this->typedConfig = $typed_config;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
  47. return new static(
  48. $entity_type,
  49. $container->get('config.factory'),
  50. $container->get('uuid'),
  51. $container->get('language_manager'),
  52. $container->get('config.typed')
  53. );
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function loadSchema() {
  59. $fields = $this->typedConfig->getDefinition('domain.record.*');
  60. return isset($fields['mapping']) ? $fields['mapping'] : [];
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function loadDefaultId() {
  66. $result = $this->loadDefaultDomain();
  67. if (!empty($result)) {
  68. return $result->id();
  69. }
  70. return NULL;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function loadDefaultDomain() {
  76. $result = $this->loadByProperties(['is_default' => TRUE]);
  77. if (!empty($result)) {
  78. return current($result);
  79. }
  80. return NULL;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function loadMultipleSorted(array $ids = NULL) {
  86. $domains = $this->loadMultiple($ids);
  87. uasort($domains, [$this, 'sort']);
  88. return $domains;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function loadByHostname($hostname) {
  94. $hostname = $this->prepareHostname($hostname);
  95. $result = $this->loadByProperties(['hostname' => $hostname]);
  96. if (empty($result)) {
  97. return NULL;
  98. }
  99. return current($result);
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function loadOptionsList() {
  105. $list = [];
  106. foreach ($this->loadMultipleSorted() as $id => $domain) {
  107. $list[$id] = $domain->label();
  108. }
  109. return $list;
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function sort(DomainInterface $a, DomainInterface $b) {
  115. return $a->getWeight() > $b->getWeight();
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function prepareHostname($hostname) {
  121. // Strip www. prefix off the hostname.
  122. $ignore_www = $this->configFactory->get('domain.settings')->get('www_prefix');
  123. if ($ignore_www && substr($hostname, 0, 4) == 'www.') {
  124. $hostname = substr($hostname, 4);
  125. }
  126. return $hostname;
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function create(array $values = []) {
  132. $default = $this->loadDefaultId();
  133. $domains = $this->loadMultiple();
  134. if (empty($values)) {
  135. $values['hostname'] = $this->createHostname();
  136. $values['name'] = \Drupal::config('system.site')->get('name');
  137. }
  138. $values += [
  139. 'scheme' => $this->getDefaultScheme(),
  140. 'status' => 1,
  141. 'weight' => count($domains) + 1,
  142. 'is_default' => (int) empty($default),
  143. ];
  144. $domain = parent::create($values);
  145. return $domain;
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function createHostname() {
  151. // We cannot inject the negotiator due to dependencies.
  152. return \Drupal::service('domain.negotiator')->negotiateActiveHostname();
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. public function createMachineName($hostname = NULL) {
  158. if (empty($hostname)) {
  159. $hostname = $this->createHostname();
  160. }
  161. return preg_replace('/[^a-z0-9_]/', '_', $hostname);
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function getDefaultScheme() {
  167. // Use the foundation request if possible.
  168. $request = \Drupal::request();
  169. if (!empty($request)) {
  170. $scheme = $request->getScheme();
  171. }
  172. // Else use the server variable.
  173. elseif (!empty($_SERVER['https'])) {
  174. $scheme = 'https';
  175. }
  176. // Else fall through to default.
  177. else {
  178. $scheme = 'http';
  179. }
  180. return $scheme;
  181. }
  182. }