mirror of
https://github.com/laravel-ls/go-php
synced 2026-06-18 04:50:04 +02:00
Initial commit
This commit is contained in:
commit
4cbf7e62b7
19 changed files with 1295 additions and 0 deletions
55
context.go
Normal file
55
context.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue