-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.go
128 lines (99 loc) · 2.76 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package service
import (
"errors"
"sort"
)
// Manager for Service
type Manager struct {
AllowOverride bool
ShareByDefault bool //Whether or not to share by default
factories FactoriesMap
services ServicesMap
shared SharedMap
aliases AliasesMap
initializers Initializers
}
//Has Check for exists service
func (sm *Manager) Has(name string) (string, bool) {
if sm.factories.Has(name) {
return "factories", true
}
if sm.aliases.Has(name) {
return "aliases", true
}
return "", false
}
//UnRegister service
func (sm *Manager) UnRegister(name string) {
sm.factories.Remove(name)
sm.aliases.Remove(name)
sm.shared.Remove(name)
sm.services.Remove(name)
}
//SetFacgtory Register service as factory
func (sm *Manager) SetFacgtory(name string, fn func(*Manager) interface{}) error {
if _, find := sm.Has(name); find {
if sm.AllowOverride == false {
return errors.New("A service by the name " + name + " already exists and cannot be overridden, please use an alternate name")
}
sm.UnRegister(name)
}
sm.factories.Set(name, fn)
sm.shared.Set(name, sm.ShareByDefault)
return nil
}
//SetAlias Register new alias to another service
func (sm *Manager) SetAlias(name string, target string) error {
if _, find := sm.Has(name); find {
if sm.AllowOverride == false {
return errors.New("A service by the name " + name + " already exists and cannot be overridden, please use an alternate name")
}
sm.UnRegister(name)
}
sm.aliases.Set(name, target)
return nil
}
//AddInitializer add new initializer
func (sm *Manager) AddInitializer(fn func(interface{}), order float32) {
sm.initializers = append(sm.initializers, Initializer{
fn: fn,
order: order,
})
sort.Sort(sm.initializers)
}
//Get service
func (sm *Manager) Get(name string) (service interface{}, err error) {
if se, found := sm.services.Get(name); found {
service = se
return
}
if factory, found := sm.factories.Get(name); found {
service = factory(sm)
} else if alias, found := sm.aliases.Get(name); found {
if service, err = sm.Get(alias); err != nil {
return
}
} else {
err = errors.New("unable to fetch or create an instance for " + name)
return
}
// apply initializers
for _, init := range sm.initializers {
init.fn(service)
}
if sm.shared.Get(name) == true {
sm.services.Set(name, service)
}
return
}
//NewManager Create New Manager Struct
func NewManager(shareByDefault bool) *Manager {
return &Manager{
ShareByDefault: shareByDefault,
shared: SharedMap{items: make(map[string]bool)},
aliases: AliasesMap{items: make(map[string]string)},
factories: FactoriesMap{items: make(map[string]func(*Manager) interface{})},
services: ServicesMap{items: make(map[string]interface{})},
initializers: Initializers{},
}
}