DomainLoader.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Drupal\domain;
  3. use Drupal\Core\Config\ConfigFactoryInterface;
  4. use Drupal\Core\Config\TypedConfigManagerInterface;
  5. /**
  6. * Loads Domain records.
  7. *
  8. * @deprecated
  9. * This class will be removed before the 8.1.0 release.
  10. * Use DomainStorage instead, loaded through the EntityTypeManager.
  11. */
  12. class DomainLoader implements DomainLoaderInterface {
  13. /**
  14. * The typed config handler.
  15. *
  16. * @var \Drupal\Core\Config\TypedConfigManagerInterface
  17. */
  18. protected $typedConfig;
  19. /**
  20. * The config factory.
  21. *
  22. * @var \Drupal\Core\Config\ConfigFactoryInterface
  23. */
  24. protected $configFactory;
  25. /**
  26. * Constructs a DomainLoader object.
  27. *
  28. * Trying to inject the storage manager throws an exception.
  29. *
  30. * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
  31. * The typed config handler.
  32. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  33. * The config factory.
  34. *
  35. * @see getStorage()
  36. */
  37. public function __construct(TypedConfigManagerInterface $typed_config, ConfigFactoryInterface $config_factory) {
  38. $this->typedConfig = $typed_config;
  39. $this->configFactory = $config_factory;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function loadSchema() {
  45. $fields = $this->typedConfig->getDefinition('domain.record.*');
  46. return isset($fields['mapping']) ? $fields['mapping'] : [];
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function load($id, $reset = FALSE) {
  52. $controller = $this->getStorage();
  53. if ($reset) {
  54. $controller->resetCache([$id]);
  55. }
  56. return $controller->load($id);
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function loadDefaultId() {
  62. $result = $this->loadDefaultDomain();
  63. if (!empty($result)) {
  64. return $result->id();
  65. }
  66. return NULL;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function loadDefaultDomain() {
  72. $result = $this->getStorage()->loadByProperties(['is_default' => TRUE]);
  73. if (!empty($result)) {
  74. return current($result);
  75. }
  76. return NULL;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function loadMultiple(array $ids = NULL, $reset = FALSE) {
  82. $controller = $this->getStorage();
  83. if ($reset) {
  84. $controller->resetCache($ids);
  85. }
  86. return $controller->loadMultiple($ids);
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function loadMultipleSorted(array $ids = NULL) {
  92. $domains = $this->loadMultiple($ids);
  93. uasort($domains, [$this, 'sort']);
  94. return $domains;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function loadByHostname($hostname) {
  100. $hostname = $this->prepareHostname($hostname);
  101. $result = $this->getStorage()->loadByProperties(['hostname' => $hostname]);
  102. if (empty($result)) {
  103. return NULL;
  104. }
  105. return current($result);
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function loadOptionsList() {
  111. $list = [];
  112. foreach ($this->loadMultipleSorted() as $id => $domain) {
  113. $list[$id] = $domain->label();
  114. }
  115. return $list;
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function sort(DomainInterface $a, DomainInterface $b) {
  121. return $a->getWeight() > $b->getWeight();
  122. }
  123. /**
  124. * Loads the storage controller.
  125. *
  126. * We use the loader very early in the request cycle. As a result, if we try
  127. * to inject the storage container, we hit a circular dependency. Using this
  128. * method at least keeps our code easier to update.
  129. */
  130. protected function getStorage() {
  131. $storage = \Drupal::entityTypeManager()->getStorage('domain');
  132. return $storage;
  133. }
  134. /**
  135. * {@inheritdoc}
  136. */
  137. public function prepareHostname($hostname) {
  138. // Strip www. prefix off the hostname.
  139. $ignore_www = $this->configFactory->get('domain.settings')->get('www_prefix');
  140. if ($ignore_www && substr($hostname, 0, 4) == 'www.') {
  141. $hostname = substr($hostname, 4);
  142. }
  143. return $hostname;
  144. }
  145. }