forked from backdrop/backdrop
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.inc
1442 lines (1341 loc) · 41.3 KB
/
config.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* This is the API for configuration storage.
*/
/**
* Retrieves a configuration object.
*
* This is the main entry point to the configuration API. Calling
* @code config(book.admin) @endcode will return a configuration object in which
* the book module can store its administrative settings.
*
* @param string $config_file
* The name of the configuration object to retrieve. The name corresponds to
* an JSON configuration file. For @code config(book.admin) @endcode, the
* config object returned will contain the contents of book.admin.json.
* @param string $type
* (optional) The type of config directory to return. Backdrop core provides
* 'active' and 'staging'. Defaults to 'active'.
*
* @return Config
* A Config object containing the specified configuration settings.
*
*/
function config($config_file, $type = 'active') {
// Use the advanced backdrop_static() pattern, since this is called very often.
static $backdrop_static_fast;
if (!isset($backdrop_static_fast)) {
$backdrop_static_fast['loaded_configs'] = &backdrop_static(__FUNCTION__);
}
$loaded_configs = &$backdrop_static_fast['loaded_configs'];
if (!isset($loaded_configs[$type][$config_file])) {
$storage = config_get_config_storage($type);
$config = new Config($config_file, $storage);
$config->load();
$cache = $config->get('_config_static');
if ($cache) {
$loaded_configs[$type][$config_file] = $config;
}
}
else {
$config = $loaded_configs[$type][$config_file];
}
return $config;
}
/**
* A shortcut function to delete a single value from a config file.
*
* Note that this function immediately writes the config file to disk and clears
* associated caches related to the new config. If deleting a number of options
* in the same configuration file, it is better to create a config object
* directly, delete all the necessary values, and then save the config file
* minus new options all at once.
*
* @param string $config_file
* The name of the configuration object to retrieve. The name corresponds to
* an JSON configuration file. For @code config(book.admin) @endcode, the
* config object returned will contain the contents of book.admin.json.
* @param string $option
* The name of the config option within the file to delete. The config option
* may contain periods to indicate levels within the config file.
*
* @since 1.7.0
*/
function config_clear($config_file, $option) {
$config = config($config_file);
$config->clear($option);
$config->save();
}
/**
* A shortcut function to check if a value is overridden within a config file.
*
* @param string $config_file
* The name of the configuration object to check. The name corresponds to
* an JSON configuration file. For @code config(book.admin) @endcode, the
* config object returned will contain the contents of book.admin.json.
* @param string $option
* The name of the config option within the file to check. The config option
* may contain periods to indicate levels within the config file.
*
* @return bool
* TRUE if the config option is overridden. FALSE otherwise.
*
* @see Config::isOverridden()
*/
function config_is_overridden($config_file, $option) {
return config($config_file)->isOverridden($option);
}
/**
* A shortcut function to load and retrieve a single value from a config file.
*
* @param string $config_file
* The name of the configuration object to retrieve. The name corresponds to
* an JSON configuration file. For @code config(book.admin) @endcode, the
* config object returned will contain the contents of book.admin.json.
* @param string $option
* The name of the config option within the file to read. The config option
* may contain periods to indicate levels within the config file. If NULL is
* passed in, the entire contents of the config file will be returned.
*
* @return mixed
* The contents of the requested config option. Returns NULL if the specified
* option was not found in the file at all.
*/
function config_get($config_file, $option = NULL) {
$config = config($config_file);
return $config->get($option);
}
/**
* A shortcut function to load and retrieve a single translated value from a config file.
*
* @param string $config_file
* The name of the configuration object to retrieve. The name corresponds to
* an JSON configuration file. For @code config('book.admin') @endcode, the
* config object returned will contain the contents of book.admin.json.
* @param string $option
* The name of the config option within the file to read. The config option
* may contain periods to indicate levels within the config file. If NULL is
* passed in, the entire contents of the config file will be returned.
* @param array $args
* An associative array of replacements to make. Replacements are made in the
* same way as the t() function.
* @param array $options
* An associative array of additional options, with the following elements:
* - 'langcode' (defaults to the current language): The language code to
* translate to a language other than what is used to display the page.
* - 'context' (defaults to the empty context): The context the source string
* belongs to.
*
* @return string
* The translated contents of the requested config option. Returns default if no
* translation is found. Returns NULL if the specified option was not found in
* the file at all or if not a string.
*
* @see t()
* @see config_get()
*/
function config_get_translated($config_file, $option = NULL, $args = array(), $options = array()) {
$config = config($config_file);
return $config->getTranslated($option, $args, $options);
}
/**
* A shortcut function to set and save a single value in a config file.
*
* Note that this function immediately writes the config file to disk and clears
* associated caches related to the new config. If writing a number of options
* to the same configuration file, it is better to create a config object
* directly, set all the new values, and then save the config file with all the
* new options all at once.
*
* @param string $config_file
* The name of the configuration object to retrieve. The name corresponds to
* an JSON configuration file. For @code config(book.admin) @endcode, the
* config object returned will contain the contents of book.admin.json.
* @param string $option
* The name of the config option within the file to set. The config option
* may contain periods to indicate levels within the config file.
* @param mixed $value
* The value to save into the config file.
*/
function config_set($config_file, $option, $value) {
$config = config($config_file);
$config->set($option, $value);
$config->save();
}
/**
* A shortcut function to set and save multiple values in a config file.
*
* Note that this function immediately writes the config file to disk and clears
* associated caches related to the new config. Unlike config_set() which would
* need to be called once per value, this function updates all the values and
* then saves the object.
*
* @param string $config_file
* The name of the configuration object to retrieve. The name corresponds to
* an JSON configuration file. For @code config(book.admin) @endcode, the
* config object returned will contain the contents of book.admin.json.
* @param array $options
* A keyed array of configuration option names mapping their values.
*/
function config_set_multiple($config_file, $options) {
$config = config($config_file);
$config->setMultiple($options);
$config->save();
}
/**
* Returns the path of a configuration directory.
*
* @param string $type
* (optional) The type of config directory to return. Backdrop core provides
* 'active' and 'staging'. Defaults to 'active'.
*
* @return string
* The configuration directory path.
*
* @throws ConfigException
*/
function config_get_config_directory($type = 'active') {
global $config_directories;
if ($test_prefix = backdrop_valid_test_ua()) {
// See BackdropWebTestBase::setUp().
$path = conf_path() . '/files/simpletest/' . substr($test_prefix, 10) . '/config_' . $type;
}
elseif (!empty($config_directories[$type])) {
$path = $config_directories[$type];
// If the path starts with a slash or dot, assume a normal path. If just
// a directory name is provided, make it relative to the settings.php file.
$first_character = substr($path, 0, 1);
if (!in_array($first_character, array('.', '/', '\\'))) {
$path = conf_path() . '/' . $path;
}
}
else {
throw new ConfigException(format_string('The configuration directory type "@type" does not exist.', array('@type' => $type)));
}
return $path;
}
/**
* Retrieves all configurations starting with a particular prefix.
*
* @param string $prefix
* The prefix of the configuration names to retrieve.
* @param string $type
* The configuration type, either "staging" or "active".
*
* @return array
* An array containing matching configuration object names.
*/
function config_get_names_with_prefix($prefix, $type = 'active') {
$storage = config_get_config_storage($type);
return $storage->listAll($prefix);
}
/**
* Loads configuration objects by name.
*
* @param array $names
* The list of configuration names to load.
* @param string $type
* The configuration type, either "staging" or "active".
*
* @return array
* An array containing matching configuration objects.
*/
function config_load_multiple($names, $type = 'active') {
$storage = config_get_config_storage($type);
return $storage->readMultiple($names);
}
/**
* Moves the default config supplied by a module to the live config directory.
*
* @param string $module
* The name of the module we are installing.
* @param string|NULL $config_name
* (optional) If wanting to copy just a single configuration file from the
* module, specify the configuration file name without the extension.
*/
function config_install_default_config($module, $config_name = NULL) {
$module_config_dir = backdrop_get_path('module', $module) . '/config';
if (is_dir($module_config_dir)) {
$storage = new ConfigFileStorage($module_config_dir);
$files = glob($module_config_dir . '/' . '*.json');
foreach ($files as $file) {
// Load config data into the active store and write it out to the
// file system in the Backdrop config directory. Note the config name
// needs to be the same as the file name WITHOUT the extension.
$parts = explode('/', $file);
$file = array_pop($parts);
$file_config_name = str_replace('.json', '', $file);
if (is_null($config_name) || $file_config_name === $config_name) {
$data = $storage->read($file_config_name);
$config = config($file_config_name);
// We only create new configs, and do not overwrite existing ones.
if ($config->isNew()) {
$config->setData($data);
$config->save();
}
}
}
}
}
/**
* Uninstall all the configuration provided by a module.
*/
function config_uninstall_config($module, $config_name = NULL) {
backdrop_load('module', $module);
if ($configs = module_invoke($module, 'config_info')) {
foreach ($configs as $config_name => $config_info) {
if (isset($config_info['name_key'])) {
$sub_names = config_get_names_with_prefix($config_name . '.');
foreach ($sub_names as $sub_name) {
config($sub_name)->delete();
}
}
else {
config($config_name)->delete();
}
}
}
}
/**
* Get the storage object for the specified configuration type
*
* @param string $type
* (optional) The type of config directory to return. Backdrop core provides
* 'active' and 'staging'. Defaults to 'active'.
*
* @return ConfigStorageInterface
* A ConfigStorageInterface object managing the specified configuration type.
*/
function config_get_config_storage($type = 'active') {
$directory = config_get_config_directory($type);
return new ConfigFileStorage($directory);
}
/**
* A base exception thrown in any configuration system operations.
*/
class ConfigException extends Exception {}
/**
* Exception thrown when a config object name is invalid.
*/
class ConfigNameException extends ConfigException {}
/**
* Exception thrown when a config object has a validation error before saving.
*
* Messages thrown using ConfigValidateException should be translated, as they
* are passed directly to end-users during form validations.
*/
class ConfigValidateException extends ConfigException {}
/**
* Exception thrown by classes implementing ConfigStorageInterface.
*/
class ConfigStorageException extends ConfigException {}
/**
* Exception thrown when attempting to read a config file fails.
*/
class ConfigStorageReadException extends ConfigStorageException {}
/**
* Defines the default configuration object.
*/
class Config {
/**
* The maximum length of a configuration object name.
*
* Many filesystems (including HFS, NTFS, and ext4) have a maximum file name
* length of 255 characters. To ensure that no configuration objects
* incompatible with this limitation are created, we enforce a maximum name
* length of 250 characters (leaving 5 characters for the file extension).
*
* @see http://en.wikipedia.org/wiki/Comparison_of_file_systems
*/
const MAX_NAME_LENGTH = 250;
/**
* The name of the configuration object.
*
* @var string
*/
protected $name;
/**
* Whether the configuration object is new or has been saved to the storage.
*
* @var bool
*/
protected $isNew = TRUE;
/**
* The data of the configuration object.
*
* @var array
*/
protected $data;
/**
* Any overrides specified for this configuration object.
*
* @var array
*/
protected $overrides;
/**
* The state of validation on this object.
*
* This value is set to TRUE by Config::validate() and is reset to FALSE after
* any change to the $data variable.
*/
protected $validated = FALSE;
/**
* The storage used to load and save this configuration object.
*
* @var ConfigStorageInterface
*/
protected $storage;
/**
* The configuration context used for this configuration object.
*
* @var ConfigStorageInterface
*/
protected $context;
/**
* Whether the configuration object has already been loaded.
*
* @var bool
*/
protected $isLoaded = FALSE;
/**
* Constructs a configuration object.
*
* @param string $name
* The name of the configuration object being constructed.
* @param ConfigStorageInterface $storage
* A storage controller object to use for reading and writing the
* configuration data.
*/
public function __construct($name, ConfigStorageInterface $storage) {
global $config;
$this->name = $name;
$this->storage = $storage;
$this->overrides = isset($config[$name]) ? $config[$name] : NULL;
}
/**
* Initializes a configuration object.
*
* @return Config
* The configuration object.
*/
public function init() {
$this->isLoaded = FALSE;
return $this;
}
/**
* Initializes a configuration object with pre-loaded data.
*
* @param array $data
* Array of loaded data for this configuration object.
*
* @return Config
* The configuration object.
*/
public function initWithData(array $data) {
$this->isLoaded = TRUE;
$this->isNew = FALSE;
$this->replaceData($data);
return $this;
}
/**
* Returns the name of this configuration object.
*
* @return string
* The name of the configuration object.
*/
public function getName() {
return $this->name;
}
/**
* Sets the name of this configuration object.
*
* @param string $name
* The name of the configuration object.
*
* @return Config
* The configuration object.
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* Validates the configuration object name.
*
* @param string $name
* The name of the configuration object.
*
* @throws ConfigNameException
*
* @see Config::MAX_NAME_LENGTH
*/
public static function validateName($name) {
// The name must be namespaced by owner.
if (strpos($name, '.') === FALSE) {
throw new ConfigNameException(format_string('Missing namespace in Config object name @name.', array(
'@name' => $name,
)));
}
// The name must be shorter than Config::MAX_NAME_LENGTH characters.
if (strlen($name) > self::MAX_NAME_LENGTH) {
throw new ConfigNameException(format_string('Config object name @name exceeds maximum allowed length of @length characters.', array(
'@name' => $name,
'@length' => self::MAX_NAME_LENGTH,
)));
}
// The name must not contain any of the following characters:
// : ? * < > " ' / \
if (preg_match('/[:?*<>"\'\/\\\\]/', $name)) {
throw new ConfigNameException(format_string('Invalid character in Config object name @name.', array(
'@name' => $name,
)));
}
}
/**
* Validate the full contents of the configuration data.
*
* This method is not automatically called when Config::setData() is called.
* Because validation is a potentially expensive operation, you should call
* this only when expecting potential problems in the provided data, such as
* when validating user-provided imports.
*
* @throws ConfigValidateException
*/
public function validateData() {
if (!$this->validated) {
$config_info = config_get_info($this->getName());
module_invoke_all('config_data_validate', $this, $config_info);
$this->validated = TRUE;
}
}
/**
* Returns whether this configuration object is new.
*
* @return bool
* TRUE if this configuration object does not exist in storage.
*/
public function isNew() {
if (!$this->isLoaded) {
$this->load();
}
return $this->isNew;
}
/**
* Check if a particular config key is overridden.
*
* @param $key
* A string that maps to a key within the configuration data.
*
* @return bool
* TRUE if the $key is overridden. FALSE otherwise.
*/
public function isOverridden($key) {
return $this->getOverride($key) !== NULL;
}
/**
* Gets data from this configuration object.
*
* @param string $key
* A string that maps to a key within the configuration data.
* For instance in the following configuration array:
* @code
* array(
* 'foo' => array(
* 'bar' => 'baz',
* ),
* );
* @endcode
* A key of 'foo.bar' would return the string 'baz'. However, a key of 'foo'
* would return array('bar' => 'baz').
* If no key is specified, then the entire data array is returned.
*
* @return mixed
* The data that was requested.
*/
public function get($key = '') {
$value = $this->getOverride($key);
if (!isset($value)) {
$value = $this->getOriginal($key);
}
return $value;
}
/**
* Gets the current config value as specified in the written config storage.
*
* In most cases, Config::get() should be used to pull the config value and
* also include any overrides to apply. This method should be used only when
* explicitly wanting the currently saved value (as stored in a config file)
* rather than what may be specified in an override (as provided in
* settings.php).
*
* @param string $key
* The string that maps to a key with the configuration data. See
* Config::get() for full examples.
*
* @return mixed
* The data that was requested.
*
* @see Config::get()
*/
public function getOriginal($key = '') {
if (!$this->isLoaded) {
$this->load();
}
if (empty($key)) {
return $this->data;
}
else {
$parts = explode('.', $key);
if (count($parts) == 1) {
return isset($this->data[$key]) ? $this->data[$key] : NULL;
}
else {
$value = $this->data;
$key_exists = FALSE;
foreach ($parts as $part) {
if (is_array($value) && array_key_exists($part, $value)) {
$value = $value[$part];
$key_exists = TRUE;
}
else {
$key_exists = FALSE;
break;
}
}
return $key_exists ? $value : NULL;
}
}
}
/**
* Checks if a config value has an override specified.
*
* @param string $key
* The string that maps to a key with the configuration data.
*
* A key may be overridden at any level, for example if
* $key === 'foo.bar.key1', the matched value could be provided by an override
* from 'foo.bar.key1', 'foo.bar', or 'foo'. In settings.php any of the
* following would provide an overridden value for 'foo.bar.key1'.
*
* @code
* // Exact match.
* $config['example.settings']['foo.bar.key1'] = 'value1';
*
* // One level up.
* $config['example.settings']['foo.bar'] = array(
* 'key1' => 'value1',
* );
*
* // A root level override.
* $config['example.settings']['foo'] = array(
* 'bar' => array(
* 'key1' => 'value1',
* 'key2' => 'value2',
* ),
* 'baz' => 'other value',
* );
* @endcode
*
* @see Config::get()
* @see settings.php
*/
public function getOverride($key) {
$value = NULL;
$parts = explode('.', $key);
$popped_parts = array();
while ($parts) {
$assembled_key = implode('.', $parts);
if (isset($this->overrides[$assembled_key])) {
// An override is matched at some level.
$value = $this->overrides[$assembled_key];
// Drill down back into the override value to get the requested key.
foreach ($popped_parts as $popped_part) {
if (isset($value[$popped_part])) {
$value = $value[$popped_part];
}
else {
$value = NULL;
$popped_parts = array();
}
}
}
// If an override was not found at this key, go up a level in the key
// and continue until there are no more $parts left.
array_unshift($popped_parts, array_pop($parts));
}
return $value;
}
/**
* Gets translated data from this configuration object.
*
* @param string $key
* A string that maps to a key within the configuration data.
* For instance in the following configuration array:
* @code
* array(
* 'foo' => array(
* 'bar' => 'baz',
* ),
* );
* @endcode
* A key of 'foo.bar' would return the string 'baz'. However, a key of 'foo'
* would return array('bar' => 'baz').
* If no key is specified, then an empty string is returned.
* If the key is not 'translatable' key then the original string is returned.
* If the key's value is not a string then an empty string is returned.
*
* @param array $args
* An associative array of replacements to make. Replacements are made in
* the same way as the t() function.
* @param array $options
* An associative array of additional options, with the following elements:
* - 'langcode' (defaults to the current language): The language code to
* translate to a language other than what is used to display the page.
* - 'context' (defaults to the empty context): The context the source string
* belongs to.
*
* @return string|NULL
* The translated data that was requested.
*
* @see t()
* @see get()
*/
public function getTranslated($key = '', $args = array(), $options = array()) {
if (empty($key)) {
return NULL;
}
$value = $this->get($key);
if (!is_string($value)) {
return NULL;
}
if (!is_array($this->get('_config_translatables'))) {
return $value;
}
// Ensure that it's a translatable key.
if (!in_array($key, $this->get('_config_translatables'))) {
return $value;
}
// Set a default context so we can differentiate between config strings.
// Rather than by using isset() or empty(), checking by key existence
// allows a NULL context to be used.
if (!array_key_exists('context', $options)) {
$options['context'] = 'config:' . $this->getName() . ':' . $key;
}
return t($value, $args, $options);
}
/**
* Replaces the data of this configuration object.
*
* @param array $data
* The new configuration data.
*
* @return Config
* The configuration object.
*/
public function setData(array $data) {
$this->replaceData($data);
// A load would destroy the data just set (for example on import).
$this->isLoaded = TRUE;
return $this;
}
/**
* Replaces the data of this configuration object.
*
* This function is separate from setData() to avoid load() state tracking.
* A load() would destroy the replaced data (for example on import). Do not
* call set() when inside load().
*
* @param array $data
* The new configuration data.
*
* @return Config
* The configuration object.
*/
protected function replaceData(array $data) {
$this->data = $data;
$this->validated = FALSE;
return $this;
}
/**
* Sets a value in this configuration object.
*
* Note that this will save a NULL value. If wanting to unset a key from the
* configuration, use Config::clear($key).
*
* @param string $key
* Identifier to store value in configuration.
* @param mixed $value
* Value to associate with identifier.
* @param bool $include_overridden_value
* Set to TRUE to write the config value even if this key has been
* overridden (usually through settings.php). Overridden keys are ignored
* by default to prevent accidentally writing values that may be
* environment-specific or contain sensitive information that should not
* be written to config.
*
* @return Config
* The configuration object.
*/
public function set($key, $value, $include_overridden_value = FALSE) {
// If setting a value that matches an override-provided one, don't actually
// write it to config.
$override_value = $this->getOverride($key);
if (isset($override_value) && ($override_value === $value) && !$include_overridden_value) {
return $this;
}
if (!$this->isLoaded) {
$this->load();
}
// The dot/period is a reserved character; it may appear between keys, but
// not within keys.
$parts = explode('.', $key);
if (count($parts) == 1) {
$this->data[$key] = $value;
}
else {
$data = &$this->data;
$last_key = array_pop($parts);
foreach ($parts as $part) {
if (!isset($data)) {
$data[$part] = array();
}
$data = &$data[$part];
}
$data[$last_key] = $value;
}
$this->validated = FALSE;
return $this;
}
/**
* Sets multiple values in this configuration object.
*
* @param array $options
* A keyed array of identifiers mapping to the associated values.
*
* @return Config
* The configuration object.
*/
public function setMultiple($options) {
foreach ($options as $key => $value) {
$this->set($key, $value);
}
return $this;
}
/**
* Sets overrides for this configuration object.
*
* @param array $overrides
* A list of overrides keyed by strings.
*
* @see Config::getOverride()
*/
public function setOverrides($overrides) {
$this->overrides = $overrides;
}
/**
* Unsets a value in this configuration object.
*
* @param string $key
* Name of the key whose value should be unset.
*
* @return Config
* The configuration object.
*/
public function clear($key) {
if (!$this->isLoaded) {
$this->load();
}
$parts = explode('.', $key);
if (count($parts) == 1) {
unset($this->data[$key]);
}
else {
$data = &$this->data;
$last_key = array_pop($parts);
foreach ($parts as $part) {
$data = &$data[$part];
}
unset($data[$last_key]);
}
$this->validated = FALSE;
return $this;
}
/**
* Loads configuration data into this object.
*
* @return Config
* The configuration object.
*/
public function load() {
$this->isLoaded = FALSE;
$data = $this->storage->read($this->name);
if ($data === FALSE) {
$this->isNew = TRUE;
$this->replaceData(array());
}
else {
$this->isNew = FALSE;
$this->replaceData($data);
}
$this->isLoaded = TRUE;
return $this;
}
/**
* Saves the configuration object.
*
* @return Config
* The configuration object.
*/
public function save() {
// Validate the configuration object name before saving.
static::validateName($this->name);
if (!$this->isLoaded) {
$this->load();
}
// Ensure config name is saved in the result.
$this->data = array_merge(array('_config_name' => $this->name), $this->data);
$this->storage->write($this->name, $this->data);
$this->isNew = FALSE;
// Empty static caches of this config file.
if ($static = &backdrop_static('config')) {
foreach ($static as $type => $configs) {
if (array_key_exists($this->name, $configs)) {
unset($static[$type][$this->name]);
}
}
}
return $this;
}
/**
* Deletes the configuration object.
*
* @return Config
* The configuration object.
*/
public function delete() {
$this->data = array();
$this->storage->delete($this->name);
$this->isNew = TRUE;
return $this;
}
/**
* Retrieves the storage used to load and save this configuration object.
*
* @return ConfigStorageInterface
* The configuration storage object.
*/
public function getStorage() {
return $this->storage;
}
}
/**
* Defines an interface for configuration storage controllers.
*
* Classes implementing this interface allow reading and writing configuration
* data from and to the storage.