eeaf510ea2ca5031b74705c1d9ac84cffa78106d
[yaffs-website] / web / core / lib / Drupal / Core / KeyValueStore / KeyValueFactory.php
1 <?php
2
3 namespace Drupal\Core\KeyValueStore;
4
5 use Symfony\Component\DependencyInjection\ContainerInterface;
6
7 /**
8  * Defines the key/value store factory.
9  */
10 class KeyValueFactory implements KeyValueFactoryInterface {
11
12   /**
13    * The specific setting name prefix.
14    *
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.
17    */
18   const SPECIFIC_PREFIX = 'keyvalue_service_';
19
20   /**
21    * The default setting name.
22    *
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.
25    */
26   const DEFAULT_SETTING = 'default';
27
28   /**
29    * The default service id.
30    *
31    * If the default setting does not exist, this is the default service id.
32    */
33   const DEFAULT_SERVICE = 'keyvalue.database';
34
35   /**
36    * Instantiated stores, keyed by collection name.
37    *
38    * @var array
39    */
40   protected $stores = [];
41
42   /**
43    * var \Symfony\Component\DependencyInjection\ContainerInterface
44    */
45   protected $container;
46
47   /**
48    * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
49    *   The service container.
50    * @param array $options
51    *   (optional) Collection-specific storage override options.
52    */
53   public function __construct(ContainerInterface $container, array $options = []) {
54     $this->container = $container;
55     $this->options = $options;
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function get($collection) {
62     if (!isset($this->stores[$collection])) {
63       if (isset($this->options[$collection])) {
64         $service_id = $this->options[$collection];
65       }
66       elseif (isset($this->options[static::DEFAULT_SETTING])) {
67         $service_id = $this->options[static::DEFAULT_SETTING];
68       }
69       else {
70         $service_id = static::DEFAULT_SERVICE;
71       }
72       $this->stores[$collection] = $this->container->get($service_id)->get($collection);
73     }
74     return $this->stores[$collection];
75   }
76
77 }