Settings.php 2.55 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Site\Settings.
6 7
 */

8
namespace Drupal\Core\Site;
9

10 11
/**
 * Read only settings that are initialized with the class.
12 13
 *
 * @ingroup utility
14 15
 */
final class Settings {
16 17

  /**
18 19
   * Array with the settings.
   *
20 21
   * @var array
   */
22
  private $storage = array();
23 24

  /**
25 26
   * Singleton instance.
   *
27
   * @var \Drupal\Core\Site\Settings
28
   */
29
  private static $instance;
30 31

  /**
32 33 34 35 36
   * Returns the settings instance.
   *
   * A singleton is used because this class is used before the container is
   * available.
   *
37
   * @return \Drupal\Core\Site\Settings
38
   */
39
  public static function getInstance() {
40 41 42
    return self::$instance;
  }

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  /**
   * Returns a setting.
   *
   * Settings can be set in settings.php in the $settings array and requested
   * by this function. Settings should be used over configuration for read-only,
   * possibly low bootstrap configuration that is environment specific.
   *
   * @param string $name
   *   The name of the setting to return.
   * @param mixed $default
   *   (optional) The default value to use if this setting is not set.
   *
   * @return mixed
   *   The value of the setting, the provided default if not set.
   */
  public static function get($name, $default = NULL) {
    return self::$instance->getSetting($name, $default);
  }

  /**
   * Returns all the settings. This is only used for testing purposes.
   *
   * @return array
   *   All the settings.
   */
  public static function getAll() {
    return self::$instance->getAllSettings();
  }

72 73 74
  /**
   * Constructor.
   *
75
   * @param array $settings
76
   *   Array with the settings.
77 78 79
   */
  function __construct(array $settings) {
    $this->storage = $settings;
80
    self::$instance = $this;
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  }

  /**
   * Returns a setting.
   *
   * Settings can be set in settings.php in the $settings array and requested
   * by this function. Settings should be used over configuration for read-only,
   * possibly low bootstrap configuration that is environment specific.
   *
   * @param string $name
   *   The name of the setting to return.
   * @param mixed $default
   *   (optional) The default value to use if this setting is not set.
   *
   * @return mixed
   *   The value of the setting, the provided default if not set.
   */
98
  public function getSetting($name, $default = NULL) {
99 100 101 102 103 104 105 106 107
    return isset($this->storage[$name]) ? $this->storage[$name] : $default;
  }

  /**
   * Returns all the settings. This is only used for testing purposes.
   *
   * @return array
   *   All the settings.
   */
108
  public function getAllSettings() {
109 110 111 112
    return $this->storage;
  }

}