gins_view.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gins
  7. import (
  8. "fmt"
  9. "github.com/gogf/gf/os/gview"
  10. "github.com/gogf/gf/util/gutil"
  11. )
  12. const (
  13. frameCoreComponentNameViewer = "gf.core.component.viewer"
  14. configNodeNameViewer = "viewer"
  15. )
  16. // View returns an instance of View with default settings.
  17. // The parameter <name> is the name for the instance.
  18. func View(name ...string) *gview.View {
  19. instanceName := gview.DefaultName
  20. if len(name) > 0 && name[0] != "" {
  21. instanceName = name[0]
  22. }
  23. instanceKey := fmt.Sprintf("%s.%s", frameCoreComponentNameViewer, instanceName)
  24. return instances.GetOrSetFuncLock(instanceKey, func() interface{} {
  25. return getViewInstance(instanceName)
  26. }).(*gview.View)
  27. }
  28. func getViewInstance(name ...string) *gview.View {
  29. instanceName := gview.DefaultName
  30. if len(name) > 0 && name[0] != "" {
  31. instanceName = name[0]
  32. }
  33. view := gview.Instance(instanceName)
  34. // To avoid file no found error while it's not necessary.
  35. if Config().Available() {
  36. var m map[string]interface{}
  37. nodeKey, _ := gutil.MapPossibleItemByKey(Config().GetMap("."), configNodeNameViewer)
  38. if nodeKey == "" {
  39. nodeKey = configNodeNameViewer
  40. }
  41. m = Config().GetMap(fmt.Sprintf(`%s.%s`, nodeKey, instanceName))
  42. if len(m) == 0 {
  43. m = Config().GetMap(nodeKey)
  44. }
  45. if len(m) > 0 {
  46. if err := view.SetConfigWithMap(m); err != nil {
  47. panic(err)
  48. }
  49. }
  50. }
  51. return view
  52. }