mirror of
https://github.com/deuill/go-php.git
synced 2024-09-21 00:40:45 +00:00
47 lines
871 B
Go
47 lines
871 B
Go
package php
|
|
|
|
// #cgo CFLAGS: -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM
|
|
// #cgo CFLAGS: -I/usr/include/php/Zend
|
|
// #cgo LDFLAGS: -lphp5
|
|
//
|
|
// #include "engine.h"
|
|
// #include "context.h"
|
|
import "C"
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"unsafe"
|
|
)
|
|
|
|
type Engine struct {
|
|
engine *C.struct__php_engine
|
|
}
|
|
|
|
func (e *Engine) NewContext(w io.Writer) (*Context, error) {
|
|
ctx := &Context{writer: w, values: make(map[string]*Value)}
|
|
|
|
ptr, err := C.context_new(e.engine, unsafe.Pointer(ctx))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Failed to initialize context for PHP engine")
|
|
}
|
|
|
|
ctx.context = ptr
|
|
|
|
return ctx, nil
|
|
}
|
|
|
|
func (e *Engine) Destroy() {
|
|
C.engine_shutdown(e.engine)
|
|
e = nil
|
|
}
|
|
|
|
func New() (*Engine, error) {
|
|
ptr, err := C.engine_init()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("PHP engine failed to initialize")
|
|
}
|
|
|
|
return &Engine{engine: ptr}, nil
|
|
}
|