-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapplication.go
190 lines (144 loc) · 3.69 KB
/
application.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package larago
import (
"reflect"
"strings"
"github.com/lara-go/larago/container"
)
// Application struct.
type Application struct {
*container.Container
Version string
Name string
Description string
HomeDirectory string
DateTimeFormat string
bootstrapped bool
booted bool
toBoot []reflect.Value
providers []ServiceProvider
config *ConfigRepository
configLoader ConfigLoader
commands []ConsoleCommand
}
// New constructor of the application.
func New() *Application {
instance := &Application{
Container: container.New(),
HomeDirectory: HomeDirectory,
DateTimeFormat: DateTimeFormat,
}
instance.Container.Instance(instance)
instance.Container.SetTagsResolver(instance)
return instance
}
// SetConfig callback that loads config while bootstrapping.
func (app *Application) SetConfig(loader ConfigLoader) *Application {
app.configLoader = loader
return app
}
// ImportConfig of the application.
func (app *Application) ImportConfig() *Application {
// Resolve config repository.
app.config = &ConfigRepository{
config: app.configLoader(),
}
// Save it to container.
app.Instance(app.config, "config", (*Config)(nil))
return app
}
// Config getter.
func (app *Application) Config() *ConfigRepository {
return app.config
}
// Env checks if application works in this environment.
func (app *Application) Env(name string) bool {
return app.config.Env() == name
}
// Register service.
func (app *Application) Register(providers ...ServiceProvider) {
for _, provider := range providers {
provider.Register(app)
if method := reflect.ValueOf(provider).MethodByName("Boot"); method.IsValid() {
if app.booted {
app.bootProvider(method)
} else {
app.toBoot = append(app.toBoot, method)
}
}
}
}
// Facade registers application facades.
func (app *Application) Facade(wrappers ...*Facade) {
for _, wrapper := range wrappers {
wrapper.Application = app
}
}
// Commands registers commands.
func (app *Application) Commands(commands ...ConsoleCommand) {
for _, command := range commands {
app.commands = append(app.commands, command)
}
}
// GetCommands retrieve registered console commands.
func (app *Application) GetCommands() []ConsoleCommand {
return app.commands
}
// Boot Application.
func (app *Application) Boot() error {
if app.booted {
return nil
}
for _, bootable := range app.toBoot {
if err := app.bootProvider(bootable); err != nil {
return err
}
}
app.booted = true
return nil
}
// Boot provider.
func (app *Application) bootProvider(boot reflect.Value) error {
_, err := app.Call(boot)
return err
}
// BootstrapWith runs bootstrappers one by one to populate and prepare application.
func (app *Application) BootstrapWith(boostrappers ...Bootstrapper) error {
if app.bootstrapped {
return nil
}
for _, bootstrapper := range boostrappers {
if err := bootstrapper(app); err != nil {
return err
}
}
app.bootstrapped = true
return nil
}
// ResolveTag resolves custom tags.
func (app *Application) ResolveTag(tag string, container *container.Container) interface{} {
var resolved interface{}
if strings.HasPrefix(tag, "Config.") {
resolved = app.config.Get(strings.TrimPrefix(tag, "Config."))
}
return resolved
}
// Run application.
func (app *Application) Run() {
app.catchSignals()
defer app.exitHandler()
// Get Kernel from container and handle request.
var kernel Kernel
app.Assign(&kernel)
kernel.Handle()
}
func (app *Application) exitHandler() {
var exitHandler ExitHandler
app.Assign(&exitHandler)
// Set defer panic handle.
exitHandler.Defer()
}
func (app *Application) catchSignals() {
var signalsHandler SignalsHandler
app.Assign(&signalsHandler)
signalsHandler.CatchInterrupt()
}