2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-04-02 14:37:46 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gcfg
|
|
|
|
|
2020-02-24 21:09:19 +08:00
|
|
|
import (
|
|
|
|
"github.com/gogf/gf/container/gmap"
|
|
|
|
"github.com/gogf/gf/internal/intlog"
|
|
|
|
)
|
2019-04-02 14:37:46 +08:00
|
|
|
|
|
|
|
var (
|
2019-06-19 09:06:52 +08:00
|
|
|
// Customized configuration content.
|
2019-07-23 23:20:27 +08:00
|
|
|
configs = gmap.NewStrStrMap(true)
|
2019-04-02 14:37:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// SetContent sets customized configuration content for specified <file>.
|
2020-12-14 13:02:08 +08:00
|
|
|
// The <file> is unnecessary param, default is DefaultConfigFile.
|
2019-04-03 23:39:31 +08:00
|
|
|
func SetContent(content string, file ...string) {
|
2020-12-14 13:02:08 +08:00
|
|
|
name := DefaultConfigFile
|
2019-06-19 09:06:52 +08:00
|
|
|
if len(file) > 0 {
|
|
|
|
name = file[0]
|
|
|
|
}
|
|
|
|
// Clear file cache for instances which cached <name>.
|
|
|
|
instances.LockFunc(func(m map[string]interface{}) {
|
|
|
|
if configs.Contains(name) {
|
|
|
|
for _, v := range m {
|
2021-01-27 00:55:45 +08:00
|
|
|
v.(*Config).jsonMap.Remove(name)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
configs.Set(name, content)
|
|
|
|
})
|
2019-04-02 14:37:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetContent returns customized configuration content for specified <file>.
|
2020-12-14 13:02:08 +08:00
|
|
|
// The <file> is unnecessary param, default is DefaultConfigFile.
|
2019-04-03 23:39:31 +08:00
|
|
|
func GetContent(file ...string) string {
|
2020-12-14 13:02:08 +08:00
|
|
|
name := DefaultConfigFile
|
2019-06-19 09:06:52 +08:00
|
|
|
if len(file) > 0 {
|
|
|
|
name = file[0]
|
|
|
|
}
|
|
|
|
return configs.Get(name)
|
2019-04-02 16:08:46 +08:00
|
|
|
}
|
|
|
|
|
2020-02-24 21:09:19 +08:00
|
|
|
// RemoveContent removes the global configuration with specified <file>.
|
2019-04-03 23:39:31 +08:00
|
|
|
// If <name> is not passed, it removes configuration of the default group name.
|
2020-02-24 21:09:19 +08:00
|
|
|
func RemoveContent(file ...string) {
|
2020-12-14 13:02:08 +08:00
|
|
|
name := DefaultConfigFile
|
2019-04-03 23:39:31 +08:00
|
|
|
if len(file) > 0 {
|
|
|
|
name = file[0]
|
|
|
|
}
|
|
|
|
// Clear file cache for instances which cached <name>.
|
|
|
|
instances.LockFunc(func(m map[string]interface{}) {
|
|
|
|
if configs.Contains(name) {
|
|
|
|
for _, v := range m {
|
2021-01-27 00:55:45 +08:00
|
|
|
v.(*Config).jsonMap.Remove(name)
|
2019-04-03 23:39:31 +08:00
|
|
|
}
|
|
|
|
configs.Remove(name)
|
|
|
|
}
|
|
|
|
})
|
2020-02-24 21:09:19 +08:00
|
|
|
|
|
|
|
intlog.Printf(`RemoveContent: %s`, name)
|
2019-04-03 23:39:31 +08:00
|
|
|
}
|
|
|
|
|
2019-04-02 16:08:46 +08:00
|
|
|
// ClearContent removes all global configuration contents.
|
|
|
|
func ClearContent() {
|
2019-06-19 09:06:52 +08:00
|
|
|
configs.Clear()
|
2019-04-03 23:39:31 +08:00
|
|
|
// Clear cache for all instances.
|
|
|
|
instances.LockFunc(func(m map[string]interface{}) {
|
|
|
|
for _, v := range m {
|
2021-01-27 00:55:45 +08:00
|
|
|
v.(*Config).jsonMap.Clear()
|
2019-04-03 23:39:31 +08:00
|
|
|
}
|
|
|
|
})
|
2020-02-24 21:09:19 +08:00
|
|
|
|
|
|
|
intlog.Print(`RemoveConfig`)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|