mirror of
https://github.com/laravel-ls/go-php
synced 2026-06-16 03:54:55 +02:00
55 lines
972 B
Go
55 lines
972 B
Go
package php
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"unsafe"
|
|
)
|
|
|
|
// #include "runtime.h"
|
|
// #include "zval.h"
|
|
// void context_bind(char *name, zval *value);
|
|
import "C"
|
|
|
|
// Context represents an individual execution context.
|
|
type Context struct {
|
|
ioContext
|
|
Path string
|
|
}
|
|
|
|
// DefaultContext returns a context with standard out and error.
|
|
func DefaultContext() *Context {
|
|
return &Context{
|
|
ioContext: ioContext{
|
|
Output: os.Stdout,
|
|
Error: os.Stderr,
|
|
},
|
|
}
|
|
}
|
|
|
|
func Output(output io.Writer) *Context {
|
|
ctx := DefaultContext()
|
|
ctx.Output = output
|
|
return ctx
|
|
}
|
|
|
|
func (ctx *Context) SetPath(path string) *Context {
|
|
ctx.Path = path
|
|
return ctx
|
|
}
|
|
|
|
func (ctx *Context) Bind(name string, value any) *Context {
|
|
n := C.CString(name)
|
|
defer C.free(unsafe.Pointer(n))
|
|
|
|
zval := go_to_zval(value)
|
|
defer C.zval_destroy(zval)
|
|
|
|
C.context_bind(n, zval)
|
|
return ctx
|
|
}
|
|
|
|
// Exec - Execute a program in the given context.
|
|
func (ctx Context) Exec(prog *program) Value {
|
|
return Exec(&ctx, prog)
|
|
}
|