DomainCreator.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Drupal\domain;
  3. /**
  4. * Creates new domain records.
  5. *
  6. * This class is a helper that replaces legacy procedural code.
  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 DomainCreator implements DomainCreatorInterface {
  13. /**
  14. * The Domain loader.
  15. *
  16. * @var \Drupal\domain\DomainLoaderInterface
  17. */
  18. protected $loader;
  19. /**
  20. * The Domain negotiator.
  21. *
  22. * @var \Drupal\domain\DomainNegotiatorInterface
  23. */
  24. protected $negotiator;
  25. /**
  26. * Constructs a DomainCreator object.
  27. *
  28. * @param \Drupal\domain\DomainLoaderInterface $loader
  29. * The domain loader.
  30. * @param \Drupal\domain\DomainNegotiatorInterface $negotiator
  31. * The domain negotiator.
  32. */
  33. public function __construct(DomainLoaderInterface $loader, DomainNegotiatorInterface $negotiator) {
  34. $this->loader = $loader;
  35. $this->negotiator = $negotiator;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function createDomain(array $values = []) {
  41. $default = $this->loader->loadDefaultId();
  42. $domains = $this->loader->loadMultiple();
  43. if (empty($values)) {
  44. $values['hostname'] = $this->createHostname();
  45. $values['name'] = \Drupal::config('system.site')->get('name');
  46. }
  47. $values += [
  48. 'scheme' => \Drupal::entityTypeManager()->getStorage('domain')->getDefaultScheme(),
  49. 'status' => 1,
  50. 'weight' => count($domains) + 1,
  51. 'is_default' => (int) empty($default),
  52. ];
  53. $domain = \Drupal::entityTypeManager()->getStorage('domain')->create($values);
  54. return $domain;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function createHostname() {
  60. return $this->negotiator->negotiateActiveHostname();
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function createMachineName($hostname = NULL) {
  66. if (empty($hostname)) {
  67. $hostname = $this->createHostname();
  68. }
  69. return preg_replace('/[^a-z0-9_]/', '_', $hostname);
  70. }
  71. }