3 namespace Drupal\Core\Config\Entity\Query;
5 use Drupal\Core\Config\Config;
6 use Drupal\Core\Config\ConfigCrudEvent;
7 use Drupal\Core\Config\ConfigEvents;
8 use Drupal\Core\Config\ConfigFactoryInterface;
9 use Drupal\Core\Config\ConfigManagerInterface;
10 use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
11 use Drupal\Core\Entity\EntityTypeInterface;
12 use Drupal\Core\Entity\Query\QueryBase;
13 use Drupal\Core\Entity\Query\QueryException;
14 use Drupal\Core\Entity\Query\QueryFactoryInterface;
15 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
16 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19 * Provides a factory for creating entity query objects for the config backend.
21 class QueryFactory implements QueryFactoryInterface, EventSubscriberInterface {
24 * The prefix for the key value collection for fast lookups.
26 const CONFIG_LOOKUP_PREFIX = 'config.entity.key_store.';
29 * The config factory used by the config entity query.
31 * @var \Drupal\Core\Config\ConfigFactoryInterface;
33 protected $configFactory;
36 * The namespace of this class, the parent class etc.
40 protected $namespaces;
43 * Constructs a QueryFactory object.
45 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
46 * The config storage used by the config entity query.
47 * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value
48 * The key value factory.
49 * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
50 * The configuration manager.
52 public function __construct(ConfigFactoryInterface $config_factory, KeyValueFactoryInterface $key_value, ConfigManagerInterface $config_manager) {
53 $this->configFactory = $config_factory;
54 $this->keyValueFactory = $key_value;
55 $this->configManager = $config_manager;
56 $this->namespaces = QueryBase::getNamespaces($this);
62 public function get(EntityTypeInterface $entity_type, $conjunction) {
63 return new Query($entity_type, $conjunction, $this->configFactory, $this->keyValueFactory, $this->namespaces);
69 public function getAggregate(EntityTypeInterface $entity_type, $conjunction) {
70 throw new QueryException('Aggregation over configuration entities is not supported');
74 * Gets the key value store used to store fast lookups.
76 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
79 * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
80 * The key value store used to store fast lookups.
82 protected function getConfigKeyStore(EntityTypeInterface $entity_type) {
83 return $this->keyValueFactory->get(static::CONFIG_LOOKUP_PREFIX . $entity_type->id());
87 * Updates or adds lookup data.
89 * @param \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type
91 * @param \Drupal\Core\Config\Config $config
92 * The configuration object that is being saved.
94 protected function updateConfigKeyStore(ConfigEntityTypeInterface $entity_type, Config $config) {
95 $config_key_store = $this->getConfigKeyStore($entity_type);
96 foreach ($entity_type->getLookupKeys() as $lookup_key) {
97 foreach ($this->getKeys($config, $lookup_key, 'get', $entity_type) as $key) {
98 $values = $config_key_store->get($key, []);
99 if (!in_array($config->getName(), $values, TRUE)) {
100 $values[] = $config->getName();
101 $config_key_store->set($key, $values);
108 * Deletes lookup data.
110 * @param \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type
112 * @param \Drupal\Core\Config\Config $config
113 * The configuration object that is being deleted.
115 protected function deleteConfigKeyStore(ConfigEntityTypeInterface $entity_type, Config $config) {
116 $config_key_store = $this->getConfigKeyStore($entity_type);
117 foreach ($entity_type->getLookupKeys() as $lookup_key) {
118 foreach ($this->getKeys($config, $lookup_key, 'getOriginal', $entity_type) as $key) {
119 $values = $config_key_store->get($key, []);
120 $pos = array_search($config->getName(), $values, TRUE);
121 if ($pos !== FALSE) {
122 unset($values[$pos]);
124 if (empty($values)) {
125 $config_key_store->delete($key);
128 $config_key_store->set($key, $values);
135 * Creates lookup keys for configuration data.
137 * @param \Drupal\Core\Config\Config $config
138 * The configuration object.
140 * The configuration key to look for.
141 * @param string $get_method
142 * Which method on the config object to call to get the value. Either 'get'
144 * @param \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type
145 * The configuration entity type.
148 * An array of lookup keys concatenated to the configuration values.
150 * @throws \Drupal\Core\Config\Entity\Query\InvalidLookupKeyException
151 * The provided $key cannot end with a wildcard. This makes no sense since
152 * you cannot do fast lookups against this.
154 protected function getKeys(Config $config, $key, $get_method, ConfigEntityTypeInterface $entity_type) {
155 if (substr($key, -1) == '*') {
156 throw new InvalidLookupKeyException(strtr('%entity_type lookup key %key ends with a wildcard this can not be used as a lookup', ['%entity_type' => $entity_type->id(), '%key' => $key]));
158 $parts = explode('.*', $key);
159 // Remove leading dots.
160 array_walk($parts, function (&$value) {
161 $value = trim($value, '.');
164 $values = (array) $this->getValues($config, $parts[0], $get_method, $parts);
167 // Flatten the array to a single dimension and add the key to all the
169 array_walk_recursive($values, function ($current) use (&$output, $key) {
170 if (is_scalar($current)) {
171 $current = $key . ':' . $current;
173 $output[] = $current;
179 * Finds all the values for a configuration key in a configuration object.
181 * @param \Drupal\Core\Config\Config $config
182 * The configuration object.
184 * The current key being checked.
185 * @param string $get_method
186 * Which method on the config object to call to get the value.
187 * @param array $parts
188 * All the parts of a configuration key we are checking.
190 * Which position of $parts we are processing. Defaults to 0.
193 * The array of configuration values the match the provided key. NULL if
194 * the configuration object does not have a value that corresponds to the
197 protected function getValues(Config $config, $key, $get_method, array $parts, $start = 0) {
198 $value = $config->$get_method($key);
199 if (is_array($value)) {
202 if (!isset($parts[$start])) {
203 // The configuration object does not have a value that corresponds to
207 foreach (array_keys($value) as $key_bit) {
208 $new_key = $key . '.' . $key_bit;
209 if (!empty($parts[$start])) {
210 $new_key .= '.' . $parts[$start];
212 $new_value[] = $this->getValues($config, $new_key, $get_method, $parts, $start);
220 * Updates configuration entity in the key store.
222 * @param \Drupal\Core\Config\ConfigCrudEvent $event
223 * The configuration event.
225 public function onConfigSave(ConfigCrudEvent $event) {
226 $saved_config = $event->getConfig();
227 $entity_type_id = $this->configManager->getEntityTypeIdByName($saved_config->getName());
228 if ($entity_type_id) {
229 $entity_type = $this->configManager->getEntityManager()->getDefinition($entity_type_id);
230 $this->updateConfigKeyStore($entity_type, $saved_config);
235 * Removes configuration entity from key store.
237 * @param \Drupal\Core\Config\ConfigCrudEvent $event
238 * The configuration event.
240 public function onConfigDelete(ConfigCrudEvent $event) {
241 $saved_config = $event->getConfig();
242 $entity_type_id = $this->configManager->getEntityTypeIdByName($saved_config->getName());
243 if ($entity_type_id) {
244 $entity_type = $this->configManager->getEntityManager()->getDefinition($entity_type_id);
245 $this->deleteConfigKeyStore($entity_type, $saved_config);
252 public static function getSubscribedEvents() {
253 $events[ConfigEvents::SAVE][] = ['onConfigSave', 128];
254 $events[ConfigEvents::DELETE][] = ['onConfigDelete', 128];