Files
gf/g/frame/gins/gins.go
2018-08-23 21:55:27 +08:00

95 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// 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://gitee.com/johng/gf.
// 单例对象管理.
// 框架内置了一些核心对象获取方法并且可以通过Set和Get方法实现IoC以及对内置核心对象的自定义替换
package gins
import (
"gitee.com/johng/gf/g/os/gcfg"
"gitee.com/johng/gf/g/os/gcmd"
"gitee.com/johng/gf/g/os/genv"
"gitee.com/johng/gf/g/os/gview"
"gitee.com/johng/gf/g/os/gfile"
"gitee.com/johng/gf/g/container/gmap"
)
const (
gFRAME_CORE_COMPONENT_NAME_VIEW = "gf.core.component.view"
gFRAME_CORE_COMPONENT_NAME_CONFIG = "gf.core.component.config"
)
// 单例对象存储器
var instances = gmap.NewStringInterfaceMap()
// 获取单例对象
func Get(k string) interface{} {
return instances.Get(k)
}
// 设置单例对象
func Set(k string, v interface{}) {
instances.Set(k, v)
}
// 核心对象View
func View() *gview.View {
result := Get(gFRAME_CORE_COMPONENT_NAME_VIEW)
if result != nil {
return result.(*gview.View)
} else {
path := gcmd.Option.Get("gf.viewpath")
if path == "" {
path = genv.Get("gf.viewpath")
if path == "" {
path = gfile.SelfDir()
}
}
view := gview.Get(path)
// 添加基于源码的搜索目录检索地址,常用于开发环境调试,只添加入口文件目录
if p := gfile.MainPkgPath(); gfile.Exists(p) {
view.AddPath(p)
}
// 框架内置函数
view.BindFunc("config", funcConfig)
Set(gFRAME_CORE_COMPONENT_NAME_VIEW, view)
return view
}
return nil
}
// 核心对象Config
// 配置文件目录查找依次为启动参数cfgpath、当前程序运行目录
func Config() *gcfg.Config {
result := Get(gFRAME_CORE_COMPONENT_NAME_CONFIG)
if result != nil {
return result.(*gcfg.Config)
} else {
path := gcmd.Option.Get("gf.cfgpath")
if path == "" {
path = genv.Get("gf.cfgpath")
if path == "" {
path = gfile.SelfDir()
}
}
config := gcfg.New(path)
// 添加基于源码的搜索目录检索地址,常用于开发环境调试,只添加入口文件目录
if p := gfile.MainPkgPath(); gfile.Exists(p) {
config.AddPath(p)
}
// 单例对象缓存控制
Set(gFRAME_CORE_COMPONENT_NAME_CONFIG, config)
return config
}
return nil
}
// 模板内置方法config
func funcConfig(pattern string, file...string) gview.HTML {
return gview.HTML(Config().GetString(pattern, file...))
}