Community Documentation

conf_path

5 bootstrap.inc conf_path()
6 bootstrap.inc conf_path($require_settings = TRUE, $reset = FALSE)
7 bootstrap.inc conf_path($require_settings = TRUE, $reset = FALSE)
8 bootstrap.inc conf_path($require_settings = TRUE, $reset = FALSE)

Finds the appropriate configuration directory.

Finds a matching configuration directory by stripping the website's hostname from left to right and pathname from right to left. The first configuration file found will be used and the remaining ones will be ignored. If no configuration file is found, return a default value '$confdir/default'.

With a site located at http://www.example.com:8080/mysite/test/, the file, settings.php, is searched for in the following directories:

  • $confdir/8080.www.example.com.mysite.test
  • $confdir/www.example.com.mysite.test
  • $confdir/example.com.mysite.test
  • $confdir/com.mysite.test
  • $confdir/8080.www.example.com.mysite
  • $confdir/www.example.com.mysite
  • $confdir/example.com.mysite
  • $confdir/com.mysite
  • $confdir/8080.www.example.com
  • $confdir/www.example.com
  • $confdir/example.com
  • $confdir/com
  • $confdir/default

If a file named sites.php is present in the $confdir, it will be loaded prior to scanning for directories. It should define an associative array named $sites, which maps domains to directories. It should be in the form of:

<?php
$sites = array(
  'The url to alias' => 'A directory within the sites directory'
);
?>

For example:

<?php
$sites = array(
  'devexample.com' => 'example.com',
  'localhost.example' => 'example.com',
);
?>

The above array will cause Drupal to look for a directory named "example.com" in the sites directory whenever a request comes from "example.com", "devexample.com", or "localhost/example". That is useful on development servers, where the domain name may not be the same as the domain of the live server. Since Drupal stores file paths into the database (files, system table, etc.) this will ensure the paths are correct while accessed on development servers.

Parameters

bool $require_settings: Only configuration 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.

bool $reset: Force a full search for matching directories even if one had been found previously. Defaults to FALSE.

Return value

The path of the matching directory.

▾ 24 functions call conf_path()

DatabaseTasks_sqlite::getFormOptions in includes/database/sqlite/install.inc
Return driver specific configuration options.
DrupalPublicStreamWrapper::getDirectoryPath in includes/stream_wrappers.inc
Implements abstract public function getDirectoryPath()
DrupalTestCase::run in modules/simpletest/drupal_web_test_case.php
Run all tests in this class.
DrupalUnitTestCase::setUp in modules/simpletest/drupal_web_test_case.php
Sets up unit test environment.
DrupalWebTestCase::drupalGetTestFiles in modules/simpletest/drupal_web_test_case.php
Get a list files that can be used in tests.
DrupalWebTestCase::setUp in modules/simpletest/drupal_web_test_case.php
Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix.…
drupal_rewrite_settings in includes/install.inc
Replace values in settings.php with values in the submitted array.
drupal_settings_initialize in includes/bootstrap.inc
Sets the base URL, cookie domain, and session name from configuration.
drupal_system_listing in includes/common.inc
Returns information about system object files (modules, themes, etc.).
FileTranferTest::testJail in modules/simpletest/tests/filetransfer.test
file_directory_temp in includes/file.inc
Get the path of system-appropriate temporary directory.
install_check_requirements in includes/install.core.inc
Checks installation requirements and reports any errors.
install_configure_form in includes/install.core.inc
Installation task; configure settings for the new site.
install_settings_form in includes/install.core.inc
Installation task; define a form to configure and rewrite settings.php.
install_verify_settings in includes/install.core.inc
Verifies the existing settings in settings.php.
system_file_system_settings in modules/system/system.admin.inc
Form builder; Configure the site file handling.
system_requirements in modules/system/system.install
Test and report Drupal installation requirements.
system_update_7061 in modules/system/system.install
Migrate upload.module data to the newly created file field.
update_fix_d7_requirements in includes/update.inc
Perform Drupal 6.x to 7.x updates that are required for update.php to function properly.
update_manager_install_form_submit in modules/update/update.manager.inc
Handle form submission when installing new projects via the update manager.
update_manager_local_transfers_allowed in modules/update/update.manager.inc
Determines if file transfers will be performed locally.
update_manager_update_ready_form_submit in modules/update/update.manager.inc
Submit handler for the form to confirm that an update should continue.
update_prepare_d7_bootstrap in includes/update.inc
Performs extra steps required to bootstrap when using a Drupal 6 database.
UpgradePathTestCase::setUp in modules/simpletest/tests/upgrade/upgrade.test
Override of DrupalWebTestCase::setUp() specialized for upgrade testing.

File

includes/bootstrap.inc, line 564
Functions that need to be loaded on every Drupal request.

Code

<?php
function conf_path($require_settings = TRUE, $reset = FALSE) {
  $conf = &drupal_static(__FUNCTION__, '');

  if ($conf && !$reset) {
    return $conf;
  }

  $confdir = 'sites';

  $sites = array();
  if (file_exists(DRUPAL_ROOT . '/' . $confdir . '/sites.php')) {
    // This will overwrite $sites with the desired mappings.
    include(DRUPAL_ROOT . '/' . $confdir . '/sites.php');
  }

  $uri = explode('/', $_SERVER['SCRIPT_NAME'] ? $_SERVER['SCRIPT_NAME'] : $_SERVER['SCRIPT_FILENAME']);
  $server = explode('.', implode('.', array_reverse(explode(':', rtrim($_SERVER['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 . '/' . $confdir . '/' . $sites[$dir])) {
        $dir = $sites[$dir];
      }
      if (file_exists(DRUPAL_ROOT . '/' . $confdir . '/' . $dir . '/settings.php') || (!$require_settings && file_exists(DRUPAL_ROOT . '/' . $confdir . '/' . $dir))) {
        $conf = "$confdir/$dir";
        return $conf;
      }
    }
  }
  $conf = "$confdir/default";
  return $conf;
}
?>

Comments

speed up 500x!

Instead of

<?php
 
for ($i = count($uri) - 1; $i > 0; $i--) {
  for (
$j = count($server); $j > 0; $j--) {
?>

it shoulda been
<?php
  $countUri
= count($uri) - 1; // or even sizeof($uri) - 1
 
for ($i = $countUri; $i > 0; $i--) {
   
$countServer = count($server); // or even sizeof($server) - 1
   
for ($j = $countServer; $j > 0; $j--) {
?>

May speed up the loop up to 500 times! Look at the benchmarks in the "Counting Loops" section (all the way on the bottom)
http://www.phpbench.com/

I also noticed this in other functions, but this is maybe not the full list.
- _locale_import_one_string
- _locale_import_parse_arithmetic
- drupal_html_to_text
- xmlrpc_value

Try to submit a patch ...

@ultrabutter
please, try to submit a patch to the HEAD.
http://drupal.org/node/add/project-issue/drupal

The patch can be tested and reviewed by the community and hopefully ... committed to the core.

False conclusion

There is no advantage to pre-calculating the initial value of the loop variable, as that is evaluated only once.

The advantage is in pre-calculating the final value used in the test expression, as that is evaluated during each iteration of the loop.

To illustrate:

  1. for ($i = complex_function(); $i > 0; $i--) {

  2. $start = complex_function();
    for ($i = $start; $i > 0; $i--) {

#1 and #2 should run at nearly identical speed. If anything, #1 should be (very) slightly faster as it contains one less assignment.

  1. for ($i = 0; $i < complex_function(); $i++){

  2. $finish = complex_function();
    for ($i = 0; $i < $finish; $i++) {

#1 should be much slower, as it evaluates complex_expression() on every iteration of the loop, whereas #2 only evaluates it once.

Login or register to post comments