2015-10-04 18:28:44 +00:00
|
|
|
// Copyright 2015 Alexander Palaistras. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|
|
|
|
|
|
|
|
// Package engine provides methods allowing for the initialization and teardown
|
|
|
|
// of PHP engine bindings, off which execution contexts can be launched.
|
2015-10-01 23:48:33 +00:00
|
|
|
package engine
|
2015-09-17 21:22:07 +00:00
|
|
|
|
|
|
|
// #cgo CFLAGS: -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM
|
2015-10-01 23:48:33 +00:00
|
|
|
// #cgo CFLAGS: -I/usr/include/php/Zend -I../context
|
|
|
|
// #cgo LDFLAGS: -L${SRCDIR}/context -lphp5
|
2015-09-17 21:22:07 +00:00
|
|
|
//
|
|
|
|
// #include "context.h"
|
2015-10-01 23:48:33 +00:00
|
|
|
// #include "engine.h"
|
2015-09-17 21:22:07 +00:00
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-10-16 13:41:13 +00:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/deuill/go-php/context"
|
2015-09-17 21:22:07 +00:00
|
|
|
)
|
|
|
|
|
2015-10-04 18:28:44 +00:00
|
|
|
// Engine represents the core PHP engine bindings.
|
2015-09-17 21:22:07 +00:00
|
|
|
type Engine struct {
|
2015-10-16 13:41:13 +00:00
|
|
|
engine *C.struct__php_engine
|
|
|
|
contexts []*context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewContext creates a new execution context on which scripts can be executed
|
|
|
|
// and variables can be binded. It corresponds to PHP's RINIT (request init)
|
|
|
|
// phase.
|
|
|
|
func (e *Engine) NewContext(w io.Writer) (*context.Context, error) {
|
|
|
|
c, err := context.New(w)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
e.contexts = append(e.contexts, c)
|
|
|
|
|
|
|
|
return c, nil
|
2015-09-17 21:22:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 13:41:13 +00:00
|
|
|
// Destroy shuts down and frees any resources related to the PHP engine bindings.
|
2015-09-17 21:22:07 +00:00
|
|
|
func (e *Engine) Destroy() {
|
2015-10-16 13:41:13 +00:00
|
|
|
for _, c := range e.contexts {
|
|
|
|
c.Destroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.engine != nil {
|
|
|
|
C.engine_shutdown(e.engine)
|
|
|
|
e.engine = nil
|
|
|
|
}
|
2015-09-17 21:22:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-04 18:28:44 +00:00
|
|
|
// New initializes a PHP engine instance on which contexts can be executed. It
|
|
|
|
// corresponds to PHP's MINIT (module init) phase.
|
2015-09-17 21:22:07 +00:00
|
|
|
func New() (*Engine, error) {
|
|
|
|
ptr, err := C.engine_init()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("PHP engine failed to initialize")
|
|
|
|
}
|
|
|
|
|
2015-10-16 13:41:13 +00:00
|
|
|
return &Engine{engine: ptr, contexts: make([]*context.Context, 0)}, nil
|
2015-09-17 21:22:07 +00:00
|
|
|
}
|