You are here

class KeyValueFactory in Service Container 7

Same name in this branch
  1. 7 src/KeyValueStore/KeyValueFactory.php \Drupal\service_container\KeyValueStore\KeyValueFactory
  2. 7 lib/Drupal/Core/KeyValueStore/KeyValueFactory.php \Drupal\Core\KeyValueStore\KeyValueFactory
Same name and namespace in other branches
  1. 7.2 lib/Drupal/Core/KeyValueStore/KeyValueFactory.php \Drupal\Core\KeyValueStore\KeyValueFactory

Defines the key/value store factory.

Hierarchy

Expanded class hierarchy of KeyValueFactory

3 files declare their use of KeyValueFactory
DatabaseStorageExpirableTest.php in lib/Drupal/service_container/Tests/KeyValue/DatabaseStorageExpirableTest.php
Contains Drupal\system\Tests\KeyValueStore\DatabaseStorageExpirableTest.
KeyValueFactory.php in src/KeyValueStore/KeyValueFactory.php
Contains \Drupal\service_container\KeyValueStore\KeyValueFactory.
MemoryStorageTest.php in lib/Drupal/service_container/Tests/KeyValue/MemoryStorageTest.php
Contains Drupal\system\Tests\KeyValueStore\MemoryStorageTest.

File

lib/Drupal/Core/KeyValueStore/KeyValueFactory.php, line 15
Contains \Drupal\Core\KeyValueStore\KeyValueFactory.

Namespace

Drupal\Core\KeyValueStore
View source
class KeyValueFactory implements KeyValueFactoryInterface {

  /**
   * The specific setting name prefix.
   *
   * The collection name will be prefixed with this constant and used as a
   * setting name. The setting value will be the id of a service.
   */
  const SPECIFIC_PREFIX = 'keyvalue_service_';

  /**
   * The default setting name.
   *
   * This is a setting name that will be used if the specific setting does not
   * exist. The setting value will be the id of a service.
   */
  const DEFAULT_SETTING = 'default';

  /**
   * The default service id.
   *
   * If the default setting does not exist, this is the default service id.
   */
  const DEFAULT_SERVICE = 'keyvalue.database';

  /**
   * Instantiated stores, keyed by collection name.
   *
   * @var array
   */
  protected $stores = array();

  /**
   * var \Symfony\Component\DependencyInjection\ContainerInterface
   */
  protected $container;

  /**
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The service container.
   * @param array $options
   *   (optional) Collection-specific storage override options.
   */
  function __construct(ContainerInterface $container, array $options = array()) {
    $this->container = $container;
    $this->options = $options;
  }

  /**
   * {@inheritdoc}
   */
  public function get($collection) {
    if (!isset($this->stores[$collection])) {
      if (isset($this->options[$collection])) {
        $service_id = $this->options[$collection];
      }
      elseif (isset($this->options[static::DEFAULT_SETTING])) {
        $service_id = $this->options[static::DEFAULT_SETTING];
      }
      else {
        $service_id = static::DEFAULT_SERVICE;
      }
      $this->stores[$collection] = $this->container
        ->get($service_id)
        ->get($collection);
    }
    return $this->stores[$collection];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
KeyValueFactory::$container protected property var \Symfony\Component\DependencyInjection\ContainerInterface
KeyValueFactory::$stores protected property Instantiated stores, keyed by collection name.
KeyValueFactory::DEFAULT_SERVICE constant The default service id. 2
KeyValueFactory::DEFAULT_SETTING constant The default setting name. 2
KeyValueFactory::get public function Constructs a new key/value store for a given collection name. Overrides KeyValueFactoryInterface::get
KeyValueFactory::SPECIFIC_PREFIX constant The specific setting name prefix. 2
KeyValueFactory::__construct function