2017-12-04 04:08:05 +01:00
|
|
|
package mvc2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"reflect"
|
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
"github.com/kataras/golog"
|
2017-12-04 04:08:05 +01:00
|
|
|
"github.com/kataras/iris/context"
|
|
|
|
"github.com/kataras/iris/core/router"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errNil = errors.New("nil")
|
|
|
|
errBad = errors.New("bad")
|
|
|
|
errAlreadyExists = errors.New("already exists")
|
|
|
|
)
|
|
|
|
|
|
|
|
type Engine struct {
|
|
|
|
Input []reflect.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() *Engine {
|
|
|
|
return new(Engine)
|
|
|
|
}
|
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
func (e *Engine) Bind(values ...interface{}) *Engine {
|
|
|
|
for _, val := range values {
|
|
|
|
if v := reflect.ValueOf(val); goodVal(v) {
|
|
|
|
e.Input = append(e.Input, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-12-04 04:08:05 +01:00
|
|
|
func (e *Engine) Child() *Engine {
|
|
|
|
child := New()
|
|
|
|
|
|
|
|
// copy the current parent's ctx func binders and services to this new child.
|
|
|
|
if l := len(e.Input); l > 0 {
|
|
|
|
input := make([]reflect.Value, l, l)
|
|
|
|
copy(input, e.Input)
|
|
|
|
child.Input = input
|
|
|
|
}
|
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
return child
|
2017-12-04 04:08:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Engine) Handler(handler interface{}) context.Handler {
|
2017-12-10 06:00:51 +01:00
|
|
|
h, err := MakeHandler(handler, e.Input...)
|
|
|
|
if err != nil {
|
|
|
|
golog.Errorf("mvc handler: %v", err)
|
|
|
|
}
|
2017-12-04 04:08:05 +01:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
func (e *Engine) Controller(router router.Party, controller BaseController, onActivate ...func(*ControllerActivator)) {
|
|
|
|
ca := newControllerActivator(router, controller, e.Input...)
|
2017-12-04 04:08:05 +01:00
|
|
|
|
2017-12-10 06:00:51 +01:00
|
|
|
// give a priority to the "onActivate"
|
|
|
|
// callbacks, if any.
|
|
|
|
for _, cb := range onActivate {
|
|
|
|
cb(ca)
|
2017-12-04 04:08:05 +01:00
|
|
|
}
|
2017-12-10 06:00:51 +01:00
|
|
|
|
|
|
|
// check if controller has an "OnActivate" function
|
|
|
|
// which accepts the controller activator and call it.
|
|
|
|
if activateListener, ok := controller.(interface {
|
|
|
|
OnActivate(*ControllerActivator)
|
|
|
|
}); ok {
|
|
|
|
activateListener.OnActivate(ca)
|
|
|
|
}
|
|
|
|
|
|
|
|
ca.activate()
|
2017-12-04 04:08:05 +01:00
|
|
|
}
|