You are here

public static function DrupalKernel::findSitePath in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/DrupalKernel.php \Drupal\Core\DrupalKernel::findSitePath()

Returns the appropriate site directory for a request.

Once the kernel has been created DrupalKernelInterface::getSitePath() is preferred since it gets the statically cached result of this method.

Site directories contain all site specific code. This includes settings.php for bootstrap level configuration, file configuration stores, public file storage and site specific modules and themes.

Finds a matching site directory file by stripping the website's hostname from left to right and pathname from right to left. By default, the directory must contain a 'settings.php' file for it to match. If the parameter $require_settings is set to FALSE, then a directory without a 'settings.php' file will match as well. The first configuration file found will be used and the remaining ones will be ignored. If no configuration file is found, returns a default value 'sites/default'. See default.settings.php for examples on how the URL is converted to a directory.

If a file named sites.php is present in the sites directory, it will be loaded prior to scanning for directories. That file can define aliases in an associative array named $sites. The array is written in the format '<port>.<domain>.<path>' => 'directory'. As an example, to create a directory alias for https://www.drupal.org:8080/mysite/test whose configuration file is in sites/example.com, the array should be defined as:

$sites = array(
  '8080.www.drupal.org.mysite.test' => 'example.com',
);

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request.

bool $require_settings: Only directories with an existing settings.php file will be recognized. Defaults to TRUE. During initial installation, this is set to FALSE so that Drupal can detect a matching directory, then create a new settings.php file in it.

Return value

string The path of the matching directory.

Throws

\Symfony\Component\HttpKernel\Exception\BadRequestHttpException In case the host name in the request is invalid.

See also

\Drupal\Core\DrupalKernelInterface::getSitePath()

\Drupal\Core\DrupalKernelInterface::setSitePath()

default.settings.php

example.sites.php

17 calls to DrupalKernel::findSitePath()
db-tools.php in core/scripts/db-tools.php
DrupalKernel::initializeSettings in core/lib/Drupal/Core/DrupalKernel.php
Locate site path and initialize settings singleton.
DrupalKernelTest::testFindSitePath in core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php
Tests site path finding.
drupal_rebuild in core/includes/utility.inc
Rebuilds all caches even when Drupal itself does not work.
dump-database-d8-mysql.php in core/scripts/dump-database-d8-mysql.php

... See full list

File

core/lib/Drupal/Core/DrupalKernel.php, line 339
Contains \Drupal\Core\DrupalKernel.

Class

DrupalKernel
The DrupalKernel class is the core of Drupal itself.

Namespace

Drupal\Core

Code

public static function findSitePath(Request $request, $require_settings = TRUE) {
  if (static::validateHostname($request) === FALSE) {
    throw new BadRequestHttpException();
  }

  // Check for a simpletest override.
  if ($test_prefix = drupal_valid_test_ua()) {
    return 'sites/simpletest/' . substr($test_prefix, 10);
  }

  // Determine whether multi-site functionality is enabled.
  if (!file_exists(DRUPAL_ROOT . '/sites/sites.php')) {
    return 'sites/default';
  }

  // Otherwise, use find the site path using the request.
  $script_name = $request->server
    ->get('SCRIPT_NAME');
  if (!$script_name) {
    $script_name = $request->server
      ->get('SCRIPT_FILENAME');
  }
  $http_host = $request
    ->getHttpHost();
  $sites = array();
  include DRUPAL_ROOT . '/sites/sites.php';
  $uri = explode('/', $script_name);
  $server = explode('.', implode('.', array_reverse(explode(':', rtrim($http_host, '.')))));
  for ($i = count($uri) - 1; $i > 0; $i--) {
    for ($j = count($server); $j > 0; $j--) {
      $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
      if (isset($sites[$dir]) && file_exists(DRUPAL_ROOT . '/sites/' . $sites[$dir])) {
        $dir = $sites[$dir];
      }
      if (file_exists(DRUPAL_ROOT . '/sites/' . $dir . '/settings.php') || !$require_settings && file_exists(DRUPAL_ROOT . '/sites/' . $dir)) {
        return "sites/{$dir}";
      }
    }
  }
  return 'sites/default';
}