You are here

private function DomainsReceive::readSites in DRD Agent 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Agent/Action/DomainsReceive.php \Drupal\drd_agent\Agent\Action\DomainsReceive::readSites()

Determines all available sites/domains in the current Drupal installation.

Return value

array An array with key/value pairs where key is the domain name and value the shortname of a directory in DRUPAL_ROOT/sites/ for where to find the settings.php file for that domain.

1 call to DomainsReceive::readSites()
DomainsReceive::execute in src/Agent/Action/DomainsReceive.php
Execute an action.

File

src/Agent/Action/DomainsReceive.php, line 46

Class

DomainsReceive
Provides a 'DomainsReceive' code.

Namespace

Drupal\drd_agent\Agent\Action

Code

private function readSites() : array {
  $sites = [];
  if (file_exists(DRUPAL_ROOT . '/sites/sites.php')) {
    try {

      /** @noinspection PhpIncludeInspection */
      include DRUPAL_ROOT . '/sites/sites.php';
    } catch (Exception $e) {

      // Ignore.
    }
  }
  if (empty($sites)) {
    foreach (scandir(DRUPAL_ROOT . '/sites') as $shortname) {
      if (is_dir(DRUPAL_ROOT . '/sites/' . $shortname) && !in_array($shortname, [
        '.',
        '..',
        'all',
      ])) {
        $file = DRUPAL_ROOT . '/sites/' . $shortname . '/settings.php';
        if (file_exists($file)) {
          list($base_url, ) = $this
            ->readSettings($shortname, $file);
          if (empty($base_url)) {
            $this
              ->watchdog('Reading Sites - Failed as url is empty: @shortname', [
              '@shortname' => $shortname,
            ], LogLevel::ERROR);
            continue;
          }
          $pos = strpos($base_url, '://');
          if ($pos > 0) {
            $base_url = substr($base_url, $pos + 3);
          }
          $sites[$base_url] = $shortname;
        }
      }
    }
  }
  if (empty($sites)) {
    $base_url = $GLOBALS['base_url'];
    $sites[$base_url] = 'default';
  }
  $this
    ->watchdog('Reading Sites - Found @n entries: <pre>@list</pre>', [
    '@n' => count($sites),
    '@list' => print_r($sites, TRUE),
  ]);
  return $sites;
}