Newer
Older
<?php
namespace Drupal\Core\Config\Entity\Query;
catch
committed
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
Alex Pott
committed
use Drupal\Core\Config\ConfigFactoryInterface;
catch
committed
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Query\QueryBase;
use Drupal\Core\Entity\Query\QueryException;
Alex Pott
committed
use Drupal\Core\Entity\Query\QueryFactoryInterface;
catch
committed
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Provides a factory for creating entity query objects for the config backend.
*/
catch
committed
class QueryFactory implements QueryFactoryInterface, EventSubscriberInterface {
/**
* The prefix for the key value collection for fast lookups.
*/
const CONFIG_LOOKUP_PREFIX = 'config.entity.key_store.';
/**
* The config factory used by the config entity query.
*
Alex Pott
committed
* @var \Drupal\Core\Config\ConfigFactoryInterface;
*/
protected $configFactory;
/**
* The namespace of this class, the parent class etc.
*
* @var array
*/
protected $namespaces;
/**
* Constructs a QueryFactory object.
*
Alex Pott
committed
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config storage used by the config entity query.
catch
committed
* @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value
* The key value factory.
* @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
* The configuration manager.
*/
catch
committed
public function __construct(ConfigFactoryInterface $config_factory, KeyValueFactoryInterface $key_value, ConfigManagerInterface $config_manager) {
$this->configFactory = $config_factory;
catch
committed
$this->keyValueFactory = $key_value;
$this->configManager = $config_manager;
$this->namespaces = QueryBase::getNamespaces($this);
}
/**
Alex Pott
committed
* {@inheritdoc}
*/
public function get(EntityTypeInterface $entity_type, $conjunction) {
catch
committed
return new Query($entity_type, $conjunction, $this->configFactory, $this->keyValueFactory, $this->namespaces);
}
/**
* {@inheritdoc}
*/
public function getAggregate(EntityTypeInterface $entity_type, $conjunction) {
Jennifer Hodgdon
committed
throw new QueryException('Aggregation over configuration entities is not supported');
}
catch
committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* Gets the key value store used to store fast lookups.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
* The key value store used to store fast lookups.
*/
protected function getConfigKeyStore(EntityTypeInterface $entity_type) {
return $this->keyValueFactory->get(static::CONFIG_LOOKUP_PREFIX . $entity_type->id());
}
/**
* Updates or adds lookup data.
*
* @param \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type
* The entity type.
* @param \Drupal\Core\Config\Config $config
* The configuration object that is being saved.
*/
protected function updateConfigKeyStore(ConfigEntityTypeInterface $entity_type, Config $config) {
$config_key_store = $this->getConfigKeyStore($entity_type);
foreach ($entity_type->getLookupKeys() as $lookup_key) {
foreach ($this->getKeys($config, $lookup_key, 'get', $entity_type) as $key) {
$values = $config_key_store->get($key, []);
if (!in_array($config->getName(), $values, TRUE)) {
$values[] = $config->getName();
$config_key_store->set($key, $values);
}
}
}
}
/**
* Deletes lookup data.
*
* @param \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type
* The entity type.
* @param \Drupal\Core\Config\Config $config
* The configuration object that is being deleted.
*/
protected function deleteConfigKeyStore(ConfigEntityTypeInterface $entity_type, Config $config) {
$config_key_store = $this->getConfigKeyStore($entity_type);
foreach ($entity_type->getLookupKeys() as $lookup_key) {
foreach ($this->getKeys($config, $lookup_key, 'getOriginal', $entity_type) as $key) {
$values = $config_key_store->get($key, []);
$pos = array_search($config->getName(), $values, TRUE);
if ($pos !== FALSE) {
unset($values[$pos]);
}
if (empty($values)) {
$config_key_store->delete($key);
}
else {
$config_key_store->set($key, $values);
}
}
}
}
/**
* Creates lookup keys for configuration data.
*
* @param \Drupal\Core\Config\Config $config
* The configuration object.
* @param string $key
* The configuration key to look for.
* @param string $get_method
* Which method on the config object to call to get the value. Either 'get'
* or 'getOriginal'.
* @param \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type
* The configuration entity type.
*
* @return array
* An array of lookup keys concatenated to the configuration values.
*
* @throws \Drupal\Core\Config\Entity\Query\InvalidLookupKeyException
* The provided $key cannot end with a wildcard. This makes no sense since
* you cannot do fast lookups against this.
*/
protected function getKeys(Config $config, $key, $get_method, ConfigEntityTypeInterface $entity_type) {
if (substr($key, -1) == '*') {
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]));
}
$parts = explode('.*', $key);
// Remove leading dots.
array_walk($parts, function (&$value) {
$value = trim($value, '.');
});
$values = (array) $this->getValues($config, $parts[0], $get_method, $parts);
$output = array();
// Flatten the array to a single dimension and add the key to all the
// values.
array_walk_recursive($values, function ($current) use (&$output, $key) {
if (is_scalar($current)) {
$current = $key . ':' . $current;
}
$output[] = $current;
});
return $output;
}
/**
* Finds all the values for a configuration key in a configuration object.
*
* @param \Drupal\Core\Config\Config $config
* The configuration object.
* @param string $key
* The current key being checked.
* @param string $get_method
* Which method on the config object to call to get the value.
* @param array $parts
* All the parts of a configuration key we are checking.
* @param int $start
* Which position of $parts we are processing. Defaults to 0.
*
* @return array|NULL
* The array of configuration values the match the provided key. NULL if
* the configuration object does not have a value that corresponds to the
* key.
*/
protected function getValues(Config $config, $key, $get_method, array $parts, $start = 0) {
$value = $config->$get_method($key);
if (is_array($value)) {
$new_value = [];
$start++;
if (!isset($parts[$start])) {
// The configuration object does not have a value that corresponds to
// the key.
return NULL;
}
foreach (array_keys($value) as $key_bit) {
$new_key = $key . '.' . $key_bit;
if (!empty($parts[$start])) {
$new_key .= '.' . $parts[$start];
}
$new_value[] = $this->getValues($config, $new_key, $get_method, $parts, $start);
}
$value = $new_value;
}
return $value;
}
/**
* Updates configuration entity in the key store.
*
* @param ConfigCrudEvent $event
* The configuration event.
*/
public function onConfigSave(ConfigCrudEvent $event) {
$saved_config = $event->getConfig();
$entity_type_id = $this->configManager->getEntityTypeIdByName($saved_config->getName());
if ($entity_type_id) {
$entity_type = $this->configManager->getEntityManager()->getDefinition($entity_type_id);
$this->updateConfigKeyStore($entity_type, $saved_config);
}
}
/**
* Removes configuration entity from key store.
*
* @param \Drupal\Core\Config\ConfigCrudEvent $event
* The configuration event.
*/
public function onConfigDelete(ConfigCrudEvent $event) {
$saved_config = $event->getConfig();
$entity_type_id = $this->configManager->getEntityTypeIdByName($saved_config->getName());
if ($entity_type_id) {
$entity_type = $this->configManager->getEntityManager()->getDefinition($entity_type_id);
$this->deleteConfigKeyStore($entity_type, $saved_config);
}
}
/**
* {@inheritdoc}
*/
static function getSubscribedEvents() {
$events[ConfigEvents::SAVE][] = array('onConfigSave', 128);
$events[ConfigEvents::DELETE][] = array('onConfigDelete', 128);
return $events;
}