github.com/gogf/gf@v1.16.9/frame/gins/gins_view.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). 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  
     7  package gins
     8  
     9  import (
    10  	"fmt"
    11  	"github.com/gogf/gf/os/gview"
    12  	"github.com/gogf/gf/util/gutil"
    13  )
    14  
    15  const (
    16  	frameCoreComponentNameViewer = "gf.core.component.viewer"
    17  	configNodeNameViewer         = "viewer"
    18  )
    19  
    20  // View returns an instance of View with default settings.
    21  // The parameter <name> is the name for the instance.
    22  func View(name ...string) *gview.View {
    23  	instanceName := gview.DefaultName
    24  	if len(name) > 0 && name[0] != "" {
    25  		instanceName = name[0]
    26  	}
    27  	instanceKey := fmt.Sprintf("%s.%s", frameCoreComponentNameViewer, instanceName)
    28  	return instances.GetOrSetFuncLock(instanceKey, func() interface{} {
    29  		return getViewInstance(instanceName)
    30  	}).(*gview.View)
    31  }
    32  
    33  func getViewInstance(name ...string) *gview.View {
    34  	instanceName := gview.DefaultName
    35  	if len(name) > 0 && name[0] != "" {
    36  		instanceName = name[0]
    37  	}
    38  	view := gview.Instance(instanceName)
    39  	// To avoid file no found error while it's not necessary.
    40  	if Config().Available() {
    41  		var m map[string]interface{}
    42  		nodeKey, _ := gutil.MapPossibleItemByKey(Config().GetMap("."), configNodeNameViewer)
    43  		if nodeKey == "" {
    44  			nodeKey = configNodeNameViewer
    45  		}
    46  		m = Config().GetMap(fmt.Sprintf(`%s.%s`, nodeKey, instanceName))
    47  		if len(m) == 0 {
    48  			m = Config().GetMap(nodeKey)
    49  		}
    50  		if len(m) > 0 {
    51  			if err := view.SetConfigWithMap(m); err != nil {
    52  				panic(err)
    53  			}
    54  		}
    55  	}
    56  	return view
    57  }