2017-02-27 18:53:04 +03:00
|
|
|
// It's important to keep the requires absolutely minimal here,
|
|
|
|
// As this cache is used in SO many other areas, we may open ourselves to
|
|
|
|
// circular dependency bugs.
|
2017-08-15 14:29:27 +03:00
|
|
|
var debug = require('ghost-ignition').debug('settings:cache'),
|
2017-03-03 01:00:01 +03:00
|
|
|
_ = require('lodash'),
|
2017-02-27 18:53:04 +03:00
|
|
|
events = require('../events'),
|
|
|
|
/**
|
|
|
|
* ## Cache
|
|
|
|
* Holds cached settings
|
2017-03-03 01:00:01 +03:00
|
|
|
* Keyed by setting.key
|
|
|
|
* Contains the JSON version of the model
|
|
|
|
* @type {{}} - object of objects
|
2017-02-27 18:53:04 +03:00
|
|
|
*/
|
|
|
|
settingsCache = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* IMPORTANT:
|
|
|
|
* We store settings with a type and a key in the database.
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* type: core
|
2017-04-24 20:41:00 +03:00
|
|
|
* key: db_hash
|
2017-02-27 18:53:04 +03:00
|
|
|
* value: ...
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* But the settings cache does not allow requesting a value by type, only by key.
|
2017-04-24 20:41:00 +03:00
|
|
|
* e.g. settingsCache.get('db_hash')
|
2017-02-27 18:53:04 +03:00
|
|
|
*/
|
|
|
|
module.exports = {
|
2017-03-03 01:00:01 +03:00
|
|
|
/**
|
|
|
|
* Get a key from the settingsCache
|
|
|
|
* Will resolve to the value, including parsing JSON, unless {resolve: false} is passed in as an option
|
|
|
|
* In which case the full JSON version of the model will be resolved
|
|
|
|
*
|
|
|
|
* @param {string} key
|
|
|
|
* @param {object} options
|
|
|
|
* @return {*}
|
|
|
|
*/
|
2017-02-27 18:53:04 +03:00
|
|
|
get: function get(key, options) {
|
|
|
|
if (!settingsCache[key]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't try to resolve to the value of the setting
|
|
|
|
if (options && options.resolve === false) {
|
|
|
|
return settingsCache[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default behaviour is to try to resolve the value and return that
|
|
|
|
try {
|
2017-06-04 13:52:22 +03:00
|
|
|
// CASE: if a string contains a number e.g. "1", JSON.parse will auto convert into integer
|
|
|
|
if (settingsCache[key].value.match(/^\d+$/)) {
|
|
|
|
return settingsCache[key].value;
|
|
|
|
}
|
|
|
|
|
2017-02-27 18:53:04 +03:00
|
|
|
return JSON.parse(settingsCache[key].value);
|
|
|
|
} catch (err) {
|
|
|
|
return settingsCache[key].value;
|
|
|
|
}
|
|
|
|
},
|
2017-03-03 01:00:01 +03:00
|
|
|
/**
|
|
|
|
* Set a key on the cache
|
|
|
|
* The only way to get an object into the cache
|
|
|
|
* Uses clone to prevent modifications from being reflected
|
|
|
|
* @param {string} key
|
|
|
|
* @param {object} value json version of settings model
|
|
|
|
*/
|
2017-02-27 18:53:04 +03:00
|
|
|
set: function set(key, value) {
|
2017-03-03 01:00:01 +03:00
|
|
|
settingsCache[key] = _.cloneDeep(value);
|
2017-02-27 18:53:04 +03:00
|
|
|
},
|
2017-03-03 01:00:01 +03:00
|
|
|
/**
|
|
|
|
* Get the entire cache object
|
|
|
|
* Uses clone to prevent modifications from being reflected
|
|
|
|
* @return {{}} cache
|
|
|
|
*/
|
2017-02-27 18:53:04 +03:00
|
|
|
getAll: function getAll() {
|
2017-03-03 01:00:01 +03:00
|
|
|
return _.cloneDeep(settingsCache);
|
2017-02-27 18:53:04 +03:00
|
|
|
},
|
2017-03-03 01:00:01 +03:00
|
|
|
/**
|
|
|
|
* Initialise the cache
|
|
|
|
*
|
|
|
|
* Optionally takes a collection of settings & can populate the cache with these.
|
|
|
|
*
|
|
|
|
* @param {Bookshelf.Collection<Settings>} [settingsCollection]
|
|
|
|
* @return {{}}
|
|
|
|
*/
|
|
|
|
init: function init(settingsCollection) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// Local function, only ever used for initialising
|
|
|
|
// We deliberately call "set" on each model so that set is a consistent interface
|
|
|
|
function updateSettingFromModel(settingModel) {
|
|
|
|
debug('Auto updating', settingModel.get('key'));
|
|
|
|
self.set(settingModel.get('key'), settingModel.toJSON());
|
|
|
|
}
|
|
|
|
|
2017-02-27 18:53:04 +03:00
|
|
|
// First, reset the cache
|
|
|
|
settingsCache = {};
|
|
|
|
|
2017-03-03 01:00:01 +03:00
|
|
|
// // if we have been passed a collection of settings, use this to populate the cache
|
|
|
|
if (settingsCollection && settingsCollection.models) {
|
|
|
|
_.each(settingsCollection.models, updateSettingFromModel);
|
|
|
|
}
|
|
|
|
|
2017-02-27 18:53:04 +03:00
|
|
|
// Bind to events to automatically keep up-to-date
|
|
|
|
events.on('settings.edited', updateSettingFromModel);
|
|
|
|
events.on('settings.added', updateSettingFromModel);
|
|
|
|
events.on('settings.deleted', updateSettingFromModel);
|
|
|
|
|
|
|
|
return settingsCache;
|
|
|
|
}
|
|
|
|
};
|