Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core_cache;
18
 
19
use core\exception\coding_exception;
20
use core_cache\exception\cache_exception;
21
use cachestore_static;
22
use cachestore_session;
23
use cachestore_file;
24
use core_component;
25
use ReflectionClass;
26
 
27
/**
28
 * Cache configuration writer.
29
 *
30
 * This class should only be used when you need to write to the config, all read operations exist within the cache config.
31
 *
32
 * @package    core_cache
33
 * @category   cache
34
 * @copyright  2012 Sam Hemelryk
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class config_writer extends config {
38
    /**
39
     * Switch that gets set to true when ever a config_writer instance is saving the cache configuration file.
40
     * If this is set to true when save is next called we must avoid the trying to save and instead return the
41
     * generated config so that is may be used instead of the file.
42
     * @var bool
43
     */
44
    protected static $creatingconfig = false;
45
 
46
    /**
47
     * Returns an instance of the configuration writer.
48
     *
49
     * @return config_writer
50
     */
51
    public static function instance() {
52
        $factory = factory::instance();
53
        return $factory->create_config_instance(true);
54
    }
55
 
56
    /**
57
     * Saves the current configuration.
58
     *
59
     * Exceptions within this function are tolerated but must be of type cache_exception.
60
     * They are caught during initialisation and written to the error log. This is required in order to avoid
61
     * infinite loop situations caused by the cache throwing exceptions during its initialisation.
62
     */
63
    protected function config_save() {
64
        global $CFG;
65
        static $confighash = '';
66
        $cachefile = static::get_config_file_path();
67
        $directory = dirname($cachefile);
68
        if ($directory !== $CFG->dataroot && !file_exists($directory)) {
69
            $result = make_writable_directory($directory, false);
70
            if (!$result) {
71
                throw new cache_exception('ex_configcannotsave', 'cache', '', null, 'Cannot create config directory. Check the permissions on your moodledata directory.');
72
            }
73
        }
74
        if (!file_exists($directory) || !is_writable($directory)) {
75
            throw new cache_exception('ex_configcannotsave', 'cache', '', null, 'Config directory is not writable. Check the permissions on the moodledata/muc directory.');
76
        }
77
 
78
        // Prepare a configuration array to store.
79
        $configuration = $this->generate_configuration_array();
80
 
81
        // Prepare the file content.
82
        $content = "<?php defined('MOODLE_INTERNAL') || die();\n \$configuration = " . var_export($configuration, true) . ";";
83
 
84
        // Do both file content and hash based detection because this might be called
85
        // many times within a single request.
86
        $hash = sha1($content);
87
        if (($hash === $confighash) || (file_exists($cachefile) && $content === file_get_contents($cachefile))) {
88
            // Config is unchanged so don't bother locking and writing.
89
            $confighash = $hash;
90
            return;
91
        }
92
 
93
        // We need to create a temporary cache lock instance for use here. Remember we are generating the config file
94
        // it doesn't exist and thus we can't use the normal API for this (it'll just try to use config).
95
        $lockconf = reset($this->configlocks);
96
        if ($lockconf === false) {
97
            debugging('Your cache configuration file is out of date and needs to be refreshed.', DEBUG_DEVELOPER);
98
            // Use the default
99
            $lockconf = [
100
                'name' => 'cachelock_file_default',
101
                'type' => 'cachelock_file',
102
                'dir' => 'filelocks',
103
                'default' => true,
104
            ];
105
        }
106
        $factory = factory::instance();
107
        $locking = $factory->create_lock_instance($lockconf);
108
        if ($locking->lock('configwrite', 'config', true)) {
109
            $tempcachefile = "{$cachefile}.tmp";
110
            // Its safe to use w mode here because we have already acquired the lock.
111
            $handle = fopen($tempcachefile, 'w');
112
            fwrite($handle, $content);
113
            fflush($handle);
114
            fclose($handle);
115
            $locking->unlock('configwrite', 'config');
116
            @chmod($tempcachefile, $CFG->filepermissions);
117
            rename($tempcachefile, $cachefile);
118
            // Tell PHP to recompile the script.
119
            core_component::invalidate_opcode_php_cache($cachefile);
120
        } else {
121
            throw new cache_exception('ex_configcannotsave', 'cache', '', null, 'Unable to open the cache config file.');
122
        }
123
    }
124
 
125
    /**
126
     * Generates a configuration array suitable to be written to the config file.
127
     * @return array
128
     */
129
    protected function generate_configuration_array() {
130
        $configuration = [];
131
        $configuration['siteidentifier'] = $this->siteidentifier;
132
        $configuration['stores'] = $this->configstores;
133
        $configuration['modemappings'] = $this->configmodemappings;
134
        $configuration['definitions'] = $this->configdefinitions;
135
        $configuration['definitionmappings'] = $this->configdefinitionmappings;
136
        $configuration['locks'] = $this->configlocks;
137
        return $configuration;
138
    }
139
 
140
    /**
141
     * Adds a plugin instance.
142
     *
143
     * This function also calls save so you should redirect immediately, or at least very shortly after
144
     * calling this method.
145
     *
146
     * @param string $name The name for the instance (must be unique)
147
     * @param string $plugin The name of the plugin.
148
     * @param array $configuration The configuration data for the plugin instance.
149
     * @return bool
150
     * @throws cache_exception
151
     */
152
    public function add_store_instance($name, $plugin, array $configuration = []) {
153
        if (array_key_exists($name, $this->configstores)) {
154
            throw new cache_exception('Duplicate name specificed for cache plugin instance. You must provide a unique name.');
155
        }
156
        $class = 'cachestore_' . $plugin;
157
        if (!class_exists($class)) {
158
            $plugins = core_component::get_plugin_list_with_file('cachestore', 'lib.php');
159
            if (!array_key_exists($plugin, $plugins)) {
160
                throw new cache_exception('Invalid plugin name specified. The plugin does not exist or is not valid.');
161
            }
162
            $file = $plugins[$plugin];
163
            if (file_exists($file)) {
164
                require_once($file);
165
            }
166
            if (!class_exists($class)) {
167
                throw new cache_exception('Invalid cache plugin specified. The plugin does not contain the required class.');
168
            }
169
        }
170
        $reflection = new ReflectionClass($class);
171
        if (!$reflection->isSubclassOf(store::class)) {
172
            throw new cache_exception('Invalid cache plugin specified. The plugin does not extend the required class.');
173
        }
174
        if (!$class::are_requirements_met()) {
175
            throw new cache_exception('Unable to add new cache plugin instance. The requested plugin type is not supported.');
176
        }
177
        $this->configstores[$name] = [
178
            'name' => $name,
179
            'plugin' => $plugin,
180
            'configuration' => $configuration,
181
            'features' => $class::get_supported_features($configuration),
182
            'modes' => $class::get_supported_modes($configuration),
183
            'mappingsonly' => !empty($configuration['mappingsonly']),
184
            'class' => $class,
185
            'default' => false,
186
        ];
187
        if (array_key_exists('lock', $configuration)) {
188
            $this->configstores[$name]['lock'] = $configuration['lock'];
189
            unset($this->configstores[$name]['configuration']['lock']);
190
        }
191
        // Call instance_created()
192
        $store = new $class($name, $this->configstores[$name]['configuration']);
193
        $store->instance_created();
194
 
195
        $this->config_save();
196
        return true;
197
    }
198
 
199
    /**
200
     * Adds a new lock instance to the config file.
201
     *
202
     * @param string $name The name the user gave the instance. PARAM_ALHPANUMEXT
203
     * @param string $plugin The plugin we are creating an instance of.
204
     * @param string $configuration Configuration data from the config instance.
205
     * @throws cache_exception
206
     */
207
    public function add_lock_instance($name, $plugin, $configuration = []) {
208
        if (array_key_exists($name, $this->configlocks)) {
209
            throw new cache_exception('Duplicate name specificed for cache lock instance. You must provide a unique name.');
210
        }
211
        $class = 'cachelock_' . $plugin;
212
        if (!class_exists($class)) {
213
            $plugins = core_component::get_plugin_list_with_file('cachelock', 'lib.php');
214
            if (!array_key_exists($plugin, $plugins)) {
215
                throw new cache_exception('Invalid lock name specified. The plugin does not exist or is not valid.');
216
            }
217
            $file = $plugins[$plugin];
218
            if (file_exists($file)) {
219
                require_once($file);
220
            }
221
            if (!class_exists($class)) {
222
                throw new cache_exception('Invalid lock plugin specified. The plugin does not contain the required class.');
223
            }
224
        }
225
        $reflection = new ReflectionClass($class);
226
        if (!$reflection->implementsInterface(lockable_cache_interface::class)) {
227
            throw new cache_exception('Invalid lock plugin specified. The plugin does not implement the required interface.');
228
        }
229
        $this->configlocks[$name] = array_merge($configuration, [
230
            'name' => $name,
231
            'type' => 'cachelock_' . $plugin,
232
            'default' => false,
233
        ]);
234
        $this->config_save();
235
    }
236
 
237
    /**
238
     * Deletes a lock instance given its name.
239
     *
240
     * @param string $name The name of the plugin, PARAM_ALPHANUMEXT.
241
     * @return bool
242
     * @throws cache_exception
243
     */
244
    public function delete_lock_instance($name) {
245
        if (!array_key_exists($name, $this->configlocks)) {
246
            throw new cache_exception('The requested store does not exist.');
247
        }
248
        if ($this->configlocks[$name]['default']) {
249
            throw new cache_exception('You can not delete the default lock.');
250
        }
251
        foreach ($this->configstores as $store) {
252
            if (isset($store['lock']) && $store['lock'] === $name) {
253
                throw new cache_exception('You cannot delete a cache lock that is being used by a store.');
254
            }
255
        }
256
        unset($this->configlocks[$name]);
257
        $this->config_save();
258
        return true;
259
    }
260
 
261
    /**
262
     * Sets the mode mappings.
263
     *
264
     * These determine the default caches for the different modes.
265
     * This function also calls save so you should redirect immediately, or at least very shortly after
266
     * calling this method.
267
     *
268
     * @param array $modemappings
269
     * @return bool
270
     * @throws cache_exception
271
     */
272
    public function set_mode_mappings(array $modemappings) {
273
        $mappings = [
274
            store::MODE_APPLICATION => [],
275
            store::MODE_SESSION => [],
276
            store::MODE_REQUEST => [],
277
        ];
278
        foreach ($modemappings as $mode => $stores) {
279
            if (!array_key_exists($mode, $mappings)) {
280
                throw new cache_exception('The cache mode for the new mapping does not exist');
281
            }
282
            $sort = 0;
283
            foreach ($stores as $store) {
284
                if (!array_key_exists($store, $this->configstores)) {
285
                    throw new cache_exception('The instance name for the new mapping does not exist');
286
                }
287
                if (array_key_exists($store, $mappings[$mode])) {
288
                    throw new cache_exception('This cache mapping already exists');
289
                }
290
                $mappings[$mode][] = [
291
                    'store' => $store,
292
                    'mode' => $mode,
293
                    'sort' => $sort++,
294
                ];
295
            }
296
        }
297
        $this->configmodemappings = array_merge(
298
            $mappings[store::MODE_APPLICATION],
299
            $mappings[store::MODE_SESSION],
300
            $mappings[store::MODE_REQUEST]
301
        );
302
 
303
        $this->config_save();
304
        return true;
305
    }
306
 
307
    /**
308
     * Edits a give plugin instance.
309
     *
310
     * The plugin instance is determined by its name, hence you cannot rename plugins.
311
     * This function also calls save so you should redirect immediately, or at least very shortly after
312
     * calling this method.
313
     *
314
     * @param string $name
315
     * @param string $plugin
316
     * @param array $configuration
317
     * @return bool
318
     * @throws cache_exception
319
     */
320
    public function edit_store_instance($name, $plugin, $configuration) {
321
        if (!array_key_exists($name, $this->configstores)) {
322
            throw new cache_exception('The requested instance does not exist.');
323
        }
324
        $plugins = core_component::get_plugin_list_with_file('cachestore', 'lib.php');
325
        if (!array_key_exists($plugin, $plugins)) {
326
            throw new cache_exception('Invalid plugin name specified. The plugin either does not exist or is not valid.');
327
        }
328
        $class = 'cachestore_' . $plugin;
329
        $file = $plugins[$plugin];
330
        if (!class_exists($class)) {
331
            if (file_exists($file)) {
332
                require_once($file);
333
            }
334
            if (!class_exists($class)) {
335
                throw new cache_exception(
336
                    "Invalid cache plugin specified. The plugin does not contain the required class {$class}",
337
                );
338
            }
339
        }
340
        $this->configstores[$name] = [
341
            'name' => $name,
342
            'plugin' => $plugin,
343
            'configuration' => $configuration,
344
            'features' => $class::get_supported_features($configuration),
345
            'modes' => $class::get_supported_modes($configuration),
346
            'mappingsonly' => !empty($configuration['mappingsonly']),
347
            'class' => $class,
348
            'default' => $this->configstores[$name]['default'], // Can't change the default.
349
        ];
350
        if (array_key_exists('lock', $configuration)) {
351
            $this->configstores[$name]['lock'] = $configuration['lock'];
352
            unset($this->configstores[$name]['configuration']['lock']);
353
        }
354
        $this->config_save();
355
        return true;
356
    }
357
 
358
    /**
359
     * Deletes a store instance.
360
     *
361
     * This function also calls save so you should redirect immediately, or at least very shortly after
362
     * calling this method.
363
     *
364
     * @param string $name The name of the instance to delete.
365
     * @return bool
366
     * @throws cache_exception
367
     */
368
    public function delete_store_instance($name) {
369
        if (!array_key_exists($name, $this->configstores)) {
370
            throw new cache_exception('The requested store does not exist.');
371
        }
372
        if ($this->configstores[$name]['default']) {
373
            throw new cache_exception('The can not delete the default stores.');
374
        }
375
        foreach ($this->configmodemappings as $mapping) {
376
            if ($mapping['store'] === $name) {
377
                throw new cache_exception('You cannot delete a cache store that has mode mappings.');
378
            }
379
        }
380
        foreach ($this->configdefinitionmappings as $mapping) {
381
            if ($mapping['store'] === $name) {
382
                throw new cache_exception('You cannot delete a cache store that has definition mappings.');
383
            }
384
        }
385
 
386
        // Call instance_deleted()
387
        $class = 'cachestore_' . $this->configstores[$name]['plugin'];
388
        $store = new $class($name, $this->configstores[$name]['configuration']);
389
        $store->instance_deleted();
390
 
391
        unset($this->configstores[$name]);
392
        $this->config_save();
393
        return true;
394
    }
395
 
396
    /**
397
     * Creates the default configuration and saves it.
398
     *
399
     * This function calls config_save, however it is safe to continue using it afterwards as this function should only ever
400
     * be called when there is no configuration file already.
401
     *
402
     * @param bool $forcesave If set to true then we will forcefully save the default configuration file.
403
     * @return true|array Returns true if the default configuration was successfully created.
404
     *     Returns a configuration array if it could not be saved. This is a bad situation. Check your error logs.
405
     */
406
    public static function create_default_configuration($forcesave = false) {
407
        // HACK ALERT.
408
        // We probably need to come up with a better way to create the default stores, or at least ensure 100% that the
409
        // default store plugins are protected from deletion.
410
        $writer = new self();
411
        $writer->configstores = self::get_default_stores();
412
        $writer->configdefinitions = self::locate_definitions();
413
        $writer->configmodemappings = [
414
            [
415
                'mode' => store::MODE_APPLICATION,
416
                'store' => 'default_application',
417
                'sort' => -1,
418
            ],
419
            [
420
                'mode' => store::MODE_SESSION,
421
                'store' => 'default_session',
422
                'sort' => -1,
423
            ],
424
            [
425
                'mode' => store::MODE_REQUEST,
426
                'store' => 'default_request',
427
                'sort' => -1,
428
            ],
429
        ];
430
        $writer->configlocks = [
431
            'default_file_lock' => [
432
                'name' => 'cachelock_file_default',
433
                'type' => 'cachelock_file',
434
                'dir' => 'filelocks',
435
                'default' => true,
436
            ],
437
        ];
438
 
439
        $factory = factory::instance();
440
        // We expect the cache to be initialising presently. If its not then something has gone wrong and likely
441
        // we are now in a loop.
442
        if (!$forcesave && $factory->get_state() !== factory::STATE_INITIALISING) {
443
            return $writer->generate_configuration_array();
444
        }
445
        $factory->set_state(factory::STATE_SAVING);
446
        $writer->config_save();
447
        return true;
448
    }
449
 
450
    /**
451
     * Returns an array of default stores for use.
452
     *
453
     * @return array
454
     */
455
    protected static function get_default_stores() {
456
        global $CFG;
457
 
458
        require_once($CFG->dirroot . '/cache/stores/file/lib.php');
459
        require_once($CFG->dirroot . '/cache/stores/session/lib.php');
460
        require_once($CFG->dirroot . '/cache/stores/static/lib.php');
461
 
462
        return [
463
            'default_application' => [
464
                'name' => 'default_application',
465
                'plugin' => 'file',
466
                'configuration' => [],
467
                'features' => cachestore_file::get_supported_features(),
468
                'modes' => cachestore_file::get_supported_modes(),
469
                'default' => true,
470
            ],
471
            'default_session' => [
472
                'name' => 'default_session',
473
                'plugin' => 'session',
474
                'configuration' => [],
475
                'features' => cachestore_session::get_supported_features(),
476
                'modes' => cachestore_session::get_supported_modes(),
477
                'default' => true,
478
            ],
479
            'default_request' => [
480
                'name' => 'default_request',
481
                'plugin' => 'static',
482
                'configuration' => [],
483
                'features' => cachestore_static::get_supported_features(),
484
                'modes' => cachestore_static::get_supported_modes(),
485
                'default' => true,
486
            ],
487
        ];
488
    }
489
 
490
    /**
491
     * Updates the default stores within the MUC config file.
492
     */
493
    public static function update_default_config_stores() {
494
        $factory = factory::instance();
495
        $factory->updating_started();
496
        $config = $factory->create_config_instance(true);
497
        $config->configstores = array_merge($config->configstores, self::get_default_stores());
498
        $config->config_save();
499
        $factory->updating_finished();
500
    }
501
 
502
    /**
503
     * Updates the definition in the configuration from those found in the cache files.
504
     *
505
     * Calls config_save further down, you should redirect immediately or asap after calling this method.
506
     *
507
     * @param bool $coreonly If set to true only core definitions will be updated.
508
     */
509
    public static function update_definitions($coreonly = false) {
510
        $factory = factory::instance();
511
        $factory->updating_started();
512
        $config = $factory->create_config_instance(true);
513
        $config->write_definitions_to_cache(self::locate_definitions($coreonly));
514
        $factory->updating_finished();
515
    }
516
 
517
    /**
518
     * Locates all of the definition files.
519
     *
520
     * @param bool $coreonly If set to true only core definitions will be updated.
521
     * @return array
522
     */
523
    protected static function locate_definitions($coreonly = false) {
524
        global $CFG;
525
 
526
        $files = [];
527
        if (file_exists($CFG->dirroot . '/lib/db/caches.php')) {
528
            $files['core'] = $CFG->dirroot . '/lib/db/caches.php';
529
        }
530
 
531
        if (!$coreonly) {
532
            $plugintypes = core_component::get_plugin_types();
533
            foreach ($plugintypes as $type => $location) {
534
                $plugins = core_component::get_plugin_list_with_file($type, 'db/caches.php');
535
                foreach ($plugins as $plugin => $filepath) {
536
                    $component = clean_param($type . '_' . $plugin, PARAM_COMPONENT); // Standardised plugin name.
537
                    $files[$component] = $filepath;
538
                }
539
            }
540
        }
541
 
542
        $definitions = [];
543
        foreach ($files as $component => $file) {
544
            $filedefs = self::load_caches_file($file);
545
            foreach ($filedefs as $area => $definition) {
546
                $area = clean_param($area, PARAM_AREA);
547
                $id = $component . '/' . $area;
548
                $definition['component'] = $component;
549
                $definition['area'] = $area;
550
                if (array_key_exists($id, $definitions)) {
551
                    debugging('Error: duplicate cache definition found with id: ' . $id, DEBUG_DEVELOPER);
552
                    continue;
553
                }
554
                $definitions[$id] = $definition;
555
            }
556
        }
557
 
558
        return $definitions;
559
    }
560
 
561
    /**
562
     * Writes the updated definitions for the config file.
563
     * @param array $definitions
564
     */
565
    private function write_definitions_to_cache(array $definitions) {
566
 
567
        // Preserve the selected sharing option when updating the definitions.
568
        // This is set by the user and should never come from caches.php.
569
        foreach ($definitions as $key => $definition) {
570
            unset($definitions[$key]['selectedsharingoption']);
571
            unset($definitions[$key]['userinputsharingkey']);
572
            if (isset($this->configdefinitions[$key]) && isset($this->configdefinitions[$key]['selectedsharingoption'])) {
573
                $definitions[$key]['selectedsharingoption'] = $this->configdefinitions[$key]['selectedsharingoption'];
574
            }
575
            if (isset($this->configdefinitions[$key]) && isset($this->configdefinitions[$key]['userinputsharingkey'])) {
576
                $definitions[$key]['userinputsharingkey'] = $this->configdefinitions[$key]['userinputsharingkey'];
577
            }
578
        }
579
 
580
        $this->configdefinitions = $definitions;
581
        foreach ($this->configdefinitionmappings as $key => $mapping) {
582
            if (!array_key_exists($mapping['definition'], $definitions)) {
583
                unset($this->configdefinitionmappings[$key]);
584
            }
585
        }
586
        $this->config_save();
587
    }
588
 
589
    /**
590
     * Loads the caches file if it exists.
591
     * @param string $file Absolute path to the file.
592
     * @return array
593
     */
594
    private static function load_caches_file($file) {
595
        if (!file_exists($file)) {
596
            return [];
597
        }
598
        $definitions = [];
599
        include($file);
600
        return $definitions;
601
    }
602
 
603
    /**
604
     * Sets the mappings for a given definition.
605
     *
606
     * @param string $definition
607
     * @param array $mappings
608
     * @throws coding_exception
609
     */
610
    public function set_definition_mappings($definition, $mappings) {
611
        if (!array_key_exists($definition, $this->configdefinitions)) {
612
            throw new coding_exception('Invalid definition name passed when updating mappings.');
613
        }
614
        foreach ($mappings as $store) {
615
            if (!array_key_exists($store, $this->configstores)) {
616
                throw new coding_exception('Invalid store name passed when updating definition mappings.');
617
            }
618
        }
619
        foreach ($this->configdefinitionmappings as $key => $mapping) {
620
            if ($mapping['definition'] == $definition) {
621
                unset($this->configdefinitionmappings[$key]);
622
            }
623
        }
624
        $sort = count($mappings);
625
        foreach ($mappings as $store) {
626
            $this->configdefinitionmappings[] = [
627
                'store' => $store,
628
                'definition' => $definition,
629
                'sort' => $sort,
630
            ];
631
            $sort--;
632
        }
633
 
634
        $this->config_save();
635
    }
636
 
637
    /**
638
     * Update the site identifier stored by the cache API.
639
     *
640
     * @param string $siteidentifier
641
     * @return string The new site identifier.
642
     */
643
    public function update_site_identifier($siteidentifier) {
644
        $this->siteidentifier = md5((string)$siteidentifier);
645
        $this->config_save();
646
        return $this->siteidentifier;
647
    }
648
 
649
    /**
650
     * Sets the selected sharing options and key for a definition.
651
     *
652
     * @param string $definition The name of the definition to set for.
653
     * @param int $sharingoption The sharing option to set.
654
     * @param string|null $userinputsharingkey The user input key or null.
655
     * @throws coding_exception
656
     */
657
    public function set_definition_sharing($definition, $sharingoption, $userinputsharingkey = null) {
658
        if (!array_key_exists($definition, $this->configdefinitions)) {
659
            throw new coding_exception('Invalid definition name passed when updating sharing options.');
660
        }
661
        if (!($this->configdefinitions[$definition]['sharingoptions'] & $sharingoption)) {
662
            throw new coding_exception('Invalid sharing option passed when updating definition.');
663
        }
664
        $this->configdefinitions[$definition]['selectedsharingoption'] = (int)$sharingoption;
665
        if (!empty($userinputsharingkey)) {
666
            $this->configdefinitions[$definition]['userinputsharingkey'] = (string)$userinputsharingkey;
667
        }
668
        $this->config_save();
669
    }
670
}
671
 
672
// Alias this class to the old name.
673
// This file will be autoloaded by the legacyclasses autoload system.
674
// In future all uses of this class will be corrected and the legacy references will be removed.
675
class_alias(config_writer::class, \cache_config_writer::class);