final class Settings

You are here

Read only settings that are initialized with the class.

Namespace

Drupal\Component\Utility

Hierarchy

Expanded class hierarchy of Settings

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
Settings::$instance private static property Singleton instance.
Settings::$storage private property Array with the settings.
Settings::get public static function Returns a setting.
Settings::getAll public static function Returns all the settings. This is only used for testing purposes.
Settings::getAllSettings public function Returns all the settings. This is only used for testing purposes.
Settings::getInstance public static function Returns the settings instance.
Settings::getSetting public function Returns a setting.
Settings::__construct function Constructor.

File

core/lib/Drupal/Component/Utility/Settings.php, line 15
Contains \Drupal\Component\Utility\Settings.

View source
final class Settings {

  /**
   * Array with the settings.
   *
   * @var array
   */
  private $storage = array();

  /**
   * Singleton instance.
   *
   * @var \Drupal\Component\Utility\Settings
   */
  private static $instance;

  /**
   * Returns the settings instance.
   *
   * A singleton is used because this class is used before the container is
   * available.
   *
   * @return \Drupal\Component\Utility\Settings
   */
  public static function getInstance() {
    return self::$instance;
  }

  /**
   * 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();
  }

  /**
   * Constructor.
   *
   * @param array $settings
   *   Array with the settings.
   */
  function __construct(array $settings) {
    $this->storage = $settings;
    self::$instance = $this;
  }

  /**
   * 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 function getSetting($name, $default = NULL) {
    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.
   */
  public function getAllSettings() {
    return $this->storage;
  }

}

Related topics

48 files declare their use of Settings
authorize.php in core/authorize.php
Administrative script for running authorized file operations.
bootstrap.inc in core/includes/bootstrap.inc
Functions that need to be loaded on every Drupal request.
BootstrapConfigStorageFactory.php in core/lib/Drupal/Core/Config/BootstrapConfigStorageFactory.php
Contains Drupal\Core\Config\BootstrapConfigStorageFactory.
CacheFactory.php in core/lib/Drupal/Core/Cache/CacheFactory.php
Contains \Drupal\Core\Cache\CacheFactory.
CacheFactoryTest.php in core/tests/Drupal/Tests/Core/Cache/CacheFactoryTest.php
Contains \Drupal\Tests\Core\Cache\CacheFactoryTest.

... See full list

42 string references to 'Settings'
aggregator.local_tasks.yml in core/modules/aggregator/aggregator.local_tasks.yml
core/modules/aggregator/aggregator.local_tasks.yml
aggregator.routing.yml in core/modules/aggregator/aggregator.routing.yml
core/modules/aggregator/aggregator.routing.yml
AggregatorConfigurationTest::testSettingsPage in core/modules/aggregator/lib/Drupal/aggregator/Tests/AggregatorConfigurationTest.php
Tests the settings form to ensure the correct default values are used.
book.local_tasks.yml in core/modules/book/book.local_tasks.yml
core/modules/book/book.local_tasks.yml
comment.schema.yml in core/modules/comment/config/schema/comment.schema.yml
core/modules/comment/config/schema/comment.schema.yml

... See full list