You are here

public function DomainCommands::listDomains in Domain Access 8

List active domains for the site.

@option inactive Show only the domains that are inactive/disabled. @option active Show only the domains that are active/enabled. @usage drush domain:list List active domains for the site. @usage drush domains List active domains for the site.

@command domain:list @aliases domains,domain-list

@field-labels weight: Weight name: Name hostname: Hostname response: HTTP Response scheme: Scheme status: Status is_default: Default domain_id: Domain Id id: Machine name @default-fields id,name,hostname,scheme,status,is_default,response

Parameters

array $options: The options passed to the command.

Return value

\Consolidation\OutputFormatters\StructuredData\RowsOfFields Table output.

Throws

\Drupal\domain\Commands\DomainCommandException

File

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

Class

DomainCommands
Drush commands for the domain module.

Namespace

Drupal\domain\Commands

Code

public function listDomains(array $options) {

  // Load all domains:
  $domains = $this
    ->domainStorage()
    ->loadMultipleSorted();
  if (empty($domains)) {
    $this
      ->logger()
      ->warning(dt('No domains have been created. Use "drush domain:add" to create one.'));
    return new RowsOfFields([]);
  }
  $keys = [
    'weight',
    'name',
    'hostname',
    'response',
    'scheme',
    'status',
    'is_default',
    'domain_id',
    'id',
  ];
  $rows = [];

  /** @var \Drupal\domain\DomainInterface $domain */
  foreach ($domains as $domain) {
    $row = [];
    foreach ($keys as $key) {
      switch ($key) {
        case 'response':
          try {
            $v = $this
              ->checkDomain($domain);
          } catch (TransferException $ex) {
            $v = dt('500 - Failed');
          } catch (Exception $ex) {
            $v = dt('500 - Exception');
          }
          if ($v >= 200 && $v <= 299) {
            $v = dt('200 - OK');
          }
          elseif ($v == 500) {
            $v = dt('500 - No server');
          }
          break;
        case 'status':
          $v = $domain
            ->get($key);
          if ($options['inactive'] && $v || $options['active'] && !$v) {
            continue 3;
          }
          $v = !empty($v) ? dt('Active') : dt('Inactive');
          break;
        case 'is_default':
          $v = $domain
            ->get($key);
          $v = !empty($v) ? dt('Default') : '';
          break;
        default:
          $v = $domain
            ->get($key);
          break;
      }
      $row[$key] = Html::escape($v);
    }
    $rows[] = $row;
  }
  return new RowsOfFields($rows);
}