3 namespace Drupal\Core\KeyValueStore;
5 use Symfony\Component\DependencyInjection\ContainerInterface;
8 * Defines the key/value store factory.
10 class KeyValueFactory implements KeyValueFactoryInterface {
13 * The specific setting name prefix.
15 * The collection name will be prefixed with this constant and used as a
16 * setting name. The setting value will be the id of a service.
18 const SPECIFIC_PREFIX = 'keyvalue_service_';
21 * The default setting name.
23 * This is a setting name that will be used if the specific setting does not
24 * exist. The setting value will be the id of a service.
26 const DEFAULT_SETTING = 'default';
29 * The default service id.
31 * If the default setting does not exist, this is the default service id.
33 const DEFAULT_SERVICE = 'keyvalue.database';
36 * Instantiated stores, keyed by collection name.
40 protected $stores = [];
43 * @var \Symfony\Component\DependencyInjection\ContainerInterface
48 * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
49 * The service container.
50 * @param array $options
51 * (optional) Collection-specific storage override options.
53 public function __construct(ContainerInterface $container, array $options = []) {
54 $this->container = $container;
55 $this->options = $options;
61 public function get($collection) {
62 if (!isset($this->stores[$collection])) {
63 if (isset($this->options[$collection])) {
64 $service_id = $this->options[$collection];
66 elseif (isset($this->options[static::DEFAULT_SETTING])) {
67 $service_id = $this->options[static::DEFAULT_SETTING];
70 $service_id = static::DEFAULT_SERVICE;
72 $this->stores[$collection] = $this->container->get($service_id)->get($collection);
74 return $this->stores[$collection];