3 namespace Drupal\Core\Site;
5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Core\Database\Database;
9 * Read only settings that are initialized with the class.
13 final class Settings {
16 * Array with the settings.
20 private $storage = [];
25 * @var \Drupal\Core\Site\Settings
27 private static $instance = NULL;
32 * @param array $settings
33 * Array with the settings.
35 public function __construct(array $settings) {
36 $this->storage = $settings;
37 self::$instance = $this;
41 * Returns the settings instance.
43 * A singleton is used because this class is used before the container is
46 * @return \Drupal\Core\Site\Settings
48 * @throws \BadMethodCallException
49 * Thrown when the settings instance has not been initialized yet.
51 public static function getInstance() {
52 if (self::$instance === NULL) {
53 throw new \BadMethodCallException('Settings::$instance is not initialized yet. Whatever you are trying to do, it might be too early for that. You could call Settings::initialize(), but it is probably better to wait until it is called in the regular way. Also check for recursions.');
55 return self::$instance;
59 * Protects creating with clone.
61 private function __clone() {
65 * Prevents settings from being serialized.
67 public function __sleep() {
68 throw new \LogicException('Settings can not be serialized. This probably means you are serializing an object that has an indirect reference to the Settings object. Adjust your code so that is not necessary.');
74 * Settings can be set in settings.php in the $settings array and requested
75 * by this function. Settings should be used over configuration for read-only,
76 * possibly low bootstrap configuration that is environment specific.
79 * The name of the setting to return.
80 * @param mixed $default
81 * (optional) The default value to use if this setting is not set.
84 * The value of the setting, the provided default if not set.
86 public static function get($name, $default = NULL) {
87 if ($name === 'install_profile' && isset(self::$instance->storage[$name])) {
88 @trigger_error('To access the install profile in Drupal 8 use \Drupal::installProfile() or inject the install_profile container parameter into your service. See https://www.drupal.org/node/2538996', E_USER_DEPRECATED);
90 return isset(self::$instance->storage[$name]) ? self::$instance->storage[$name] : $default;
94 * Returns all the settings. This is only used for testing purposes.
99 public static function getAll() {
100 return self::$instance->storage;
104 * Bootstraps settings.php and the Settings singleton.
106 * @param string $app_root
108 * @param string $site_path
109 * The current site path.
110 * @param \Composer\Autoload\ClassLoader $class_loader
111 * The class loader that is used for this request. Passed by reference and
112 * exposed to the local scope of settings.php, so as to allow it to be
113 * decorated with Symfony's ApcClassLoader, for example.
115 * @see default.settings.php
117 public static function initialize($app_root, $site_path, &$class_loader) {
118 // Export these settings.php variables to the global namespace.
119 global $config_directories, $config;
124 if (is_readable($app_root . '/' . $site_path . '/settings.php')) {
125 require $app_root . '/' . $site_path . '/settings.php';
128 // Initialize Database.
129 Database::setMultipleConnectionInfo($databases);
131 // Initialize Settings.
132 new Settings($settings);
136 * Gets a salt useful for hardening against SQL injection.
139 * A salt based on information in settings.php, not in the database.
141 * @throws \RuntimeException
143 public static function getHashSalt() {
144 $hash_salt = self::$instance->get('hash_salt');
145 // This should never happen, as it breaks user logins and many other
146 // services. Therefore, explicitly notify the user (developer) by throwing
148 if (empty($hash_salt)) {
149 throw new \RuntimeException('Missing $settings[\'hash_salt\'] in settings.php.');
156 * Generates a prefix for APCu user cache keys.
158 * A standardized prefix is useful to allow visual inspection of an APCu user
159 * cache. By default, this method will produce a unique prefix per site using
160 * the hash salt. If the setting 'apcu_ensure_unique_prefix' is set to FALSE
161 * then if the caller does not provide a $site_path only the Drupal root will
162 * be used. This allows tests to use the same prefix ensuring that the number
163 * of APCu items created during a full test run is kept to a minimum.
164 * Additionally, if a multi site implementation does not use site specific
165 * module directories setting apcu_ensure_unique_prefix would allow the sites
166 * to share APCu cache items.
169 * An identifier for the prefix. For example, 'class_loader' or
173 * The prefix for APCu user cache keys.
175 * @see https://www.drupal.org/project/drupal/issues/2926309
177 public static function getApcuPrefix($identifier, $root, $site_path = '') {
178 if (static::get('apcu_ensure_unique_prefix', TRUE)) {
179 return 'drupal.' . $identifier . '.' . \Drupal::VERSION . '.' . static::get('deployment_identifier') . '.' . hash_hmac('sha256', $identifier, static::get('hash_salt') . '.' . $root . '/' . $site_path);
181 return 'drupal.' . $identifier . '.' . \Drupal::VERSION . '.' . static::get('deployment_identifier') . '.' . Crypt::hashBase64($root . '/' . $site_path);