You are here

public function DomainCommands::generate in Domain Access 8

Generate domains for testing.

@option count The count of extra domains to generate. Default is 15. @option empty Pass empty=1 to truncate the {domain} table before creating records. @option scheme Options are http | https | variable @usage drush domain-generate example.com @usage drush domain-generate example.com --count=25 @usage drush domain-generate example.com --count=25 --empty=1 @usage drush domain-generate example.com --count=25 --empty=1 --scheme=https @usage drush gend @usage drush gend --count=25 @usage drush gend --count=25 --empty=1 @usage drush gend --count=25 --empty=1 --scheme=https

@command domain:generate @aliases gend,domgen,domain-generate

Parameters

string $primary: The primary domain to use. This will be created and used for *.example.com hostnames.

array $options: An associative array of options whose values come from cli, aliases, config, etc.

Return value

string The message to print.

Throws

\Drupal\domain\Commands\DomainCommandException

File

domain/src/Commands/DomainCommands.php, line 860

Class

DomainCommands
Drush commands for the domain module.

Namespace

Drupal\domain\Commands

Code

public function generate($primary = 'example.com', array $options = [
  'count' => NULL,
  'empty' => NULL,
  'scheme' => 'http',
]) {

  // Check the number of domains to create.
  $count = $options['count'];
  if (is_null($count)) {
    $count = 15;
  }
  $domains = $this
    ->domainStorage()
    ->loadMultiple(NULL);
  if (!empty($options['empty'])) {
    $this
      ->domainStorage()
      ->delete($domains);
    $domains = $this
      ->domainStorage()
      ->loadMultiple(NULL);
  }

  // Ensure we don't duplicate any domains.
  $existing = [];
  if (!empty($domains)) {
    foreach ($domains as $domain) {
      $existing[] = $domain
        ->getHostname();
    }
  }

  // Set up one.* and so on.
  $names = [
    'one',
    'two',
    'three',
    'four',
    'five',
    'six',
    'seven',
    'eight',
    'nine',
    'ten',
    'foo',
    'bar',
    'baz',
  ];

  // Set the creation array.
  $new = [
    $primary,
  ];
  foreach ($names as $name) {
    $new[] = $name . '.' . $primary;
  }

  // Include a non hostname.
  $new[] = 'my' . $primary;

  // Filter against existing so we can count correctly.
  $prepared = [];
  foreach ($new as $key => $value) {
    if (!in_array($value, $existing, TRUE)) {
      $prepared[] = $value;
    }
  }

  // Add any test domains that have numeric prefixes. We don't expect these
  // URLs to work, and mainly use them for testing the user interface.
  $start = 1;
  foreach ($existing as $exists) {
    $name = explode('.', $exists);
    if (substr_count($name[0], 'test') > 0) {
      $num = (int) str_replace('test', '', $name[0]) + 1;
      if ($num > $start) {
        $start = $num;
      }
    }
  }
  $needed = $count - count($prepared) + $start;
  for ($i = $start; $i <= $needed; $i++) {
    $prepared[] = 'test' . $i . '.' . $primary;
  }

  // Get the initial item weight for sorting.
  $start_weight = count($domains);
  $prepared = array_slice($prepared, 0, $count);
  $list = [];

  // Create the domains.
  foreach ($prepared as $key => $item) {
    $hostname = mb_strtolower($item);
    $values = [
      'name' => $item != $primary ? ucwords(str_replace(".{$primary}", '', $item)) : \Drupal::config('system.site')
        ->get('name'),
      'hostname' => $hostname,
      'scheme' => $options['scheme'],
      'status' => 1,
      'weight' => $item != $primary ? $key + $start_weight + 1 : -1,
      'is_default' => 0,
      'id' => $this
        ->domainStorage()
        ->createMachineName($hostname),
    ];
    $domain = $this
      ->domainStorage()
      ->create($values);
    $domain
      ->save();
    $list[] = dt('Created @domain.', [
      '@domain' => $domain
        ->getHostname(),
    ]);
  }

  // If nothing created, say so.
  if (empty($prepared)) {
    return dt('No new domains were created.');
  }
  else {
    return dt("Created @count new domains:\n@list", [
      '@count' => count($prepared),
      '@list' => implode("\n", $list),
    ]);
  }
}