Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / lib / Drupal / Core / Site / Settings.php
1 <?php
2
3 namespace Drupal\Core\Site;
4
5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Core\Database\Database;
7
8 /**
9  * Read only settings that are initialized with the class.
10  *
11  * @ingroup utility
12  */
13 final class Settings {
14
15   /**
16    * Array with the settings.
17    *
18    * @var array
19    */
20   private $storage = [];
21
22   /**
23    * Singleton instance.
24    *
25    * @var \Drupal\Core\Site\Settings
26    */
27   private static $instance = NULL;
28
29   /**
30    * Constructor.
31    *
32    * @param array $settings
33    *   Array with the settings.
34    */
35   public function __construct(array $settings) {
36     $this->storage = $settings;
37     self::$instance = $this;
38   }
39
40   /**
41    * Returns the settings instance.
42    *
43    * A singleton is used because this class is used before the container is
44    * available.
45    *
46    * @return \Drupal\Core\Site\Settings
47    *
48    * @throws \BadMethodCallException
49    *   Thrown when the settings instance has not been initialized yet.
50    */
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.');
54     }
55     return self::$instance;
56   }
57
58   /**
59    * Protects creating with clone.
60    */
61   private function __clone() {
62   }
63
64   /**
65    * Prevents settings from being serialized.
66    */
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.');
69   }
70
71   /**
72    * Returns a setting.
73    *
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.
77    *
78    * @param string $name
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.
82    *
83    * @return mixed
84    *   The value of the setting, the provided default if not set.
85    */
86   public static function get($name, $default = NULL) {
87     return isset(self::$instance->storage[$name]) ? self::$instance->storage[$name] : $default;
88   }
89
90   /**
91    * Returns all the settings. This is only used for testing purposes.
92    *
93    * @return array
94    *   All the settings.
95    */
96   public static function getAll() {
97     return self::$instance->storage;
98   }
99
100   /**
101    * Bootstraps settings.php and the Settings singleton.
102    *
103    * @param string $app_root
104    *   The app root.
105    * @param string $site_path
106    *   The current site path.
107    * @param \Composer\Autoload\ClassLoader $class_loader
108    *   The class loader that is used for this request. Passed by reference and
109    *   exposed to the local scope of settings.php, so as to allow it to be
110    *   decorated with Symfony's ApcClassLoader, for example.
111    *
112    * @see default.settings.php
113    */
114   public static function initialize($app_root, $site_path, &$class_loader) {
115     // Export these settings.php variables to the global namespace.
116     global $config_directories, $config;
117     $settings = [];
118     $config = [];
119     $databases = [];
120
121     if (is_readable($app_root . '/' . $site_path . '/settings.php')) {
122       require $app_root . '/' . $site_path . '/settings.php';
123     }
124
125     // Initialize Database.
126     Database::setMultipleConnectionInfo($databases);
127
128     // Initialize Settings.
129     new Settings($settings);
130   }
131
132   /**
133    * Gets a salt useful for hardening against SQL injection.
134    *
135    * @return string
136    *   A salt based on information in settings.php, not in the database.
137    *
138    * @throws \RuntimeException
139    */
140   public static function getHashSalt() {
141     $hash_salt = self::$instance->get('hash_salt');
142     // This should never happen, as it breaks user logins and many other
143     // services. Therefore, explicitly notify the user (developer) by throwing
144     // an exception.
145     if (empty($hash_salt)) {
146       throw new \RuntimeException('Missing $settings[\'hash_salt\'] in settings.php.');
147     }
148
149     return $hash_salt;
150   }
151
152   /**
153    * Generates a prefix for APCu user cache keys.
154    *
155    * A standardized prefix is useful to allow visual inspection of an APCu user
156    * cache. By default, this method will produce a unique prefix per site using
157    * the hash salt. If the setting 'apcu_ensure_unique_prefix' is set to FALSE
158    * then if the caller does not provide a $site_path only the Drupal root will
159    * be used. This allows tests to use the same prefix ensuring that the number
160    * of APCu items created during a full test run is kept to a minimum.
161    * Additionally, if a multi site implementation does not use site specific
162    * module directories setting apcu_ensure_unique_prefix would allow the sites
163    * to share APCu cache items.
164    *
165    * @param $identifier
166    *   An identifier for the prefix. For example, 'class_loader' or
167    *   'cache_backend'.
168    *
169    * @return string
170    *   The prefix for APCu user cache keys.
171    *
172    * @see https://www.drupal.org/project/drupal/issues/2926309
173    */
174   public static function getApcuPrefix($identifier, $root, $site_path = '') {
175     if (static::get('apcu_ensure_unique_prefix', TRUE)) {
176       return 'drupal.' . $identifier . '.' . \Drupal::VERSION . '.' . static::get('deployment_identifier') . '.' . hash_hmac('sha256', $identifier, static::get('hash_salt') . '.' . $root . '/' . $site_path);
177     }
178     return 'drupal.' . $identifier . '.' . \Drupal::VERSION . '.' . static::get('deployment_identifier') . '.' . Crypt::hashBase64($root . '/' . $site_path);
179   }
180
181 }