Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions context.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ void context_bind(engine_context *context, char *name, void *value) {
_context_bind(name, v->internal);
}

void context_ini(engine_context *context, char *name, char *value) {
_context_ini(name, value);
}

void context_destroy(engine_context *context) {
php_request_shutdown(NULL);

Expand Down
12 changes: 12 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ func (c *Context) Bind(name string, val interface{}) error {
return nil
}

// Ini allows setting ini file values into the current execution context.
func (c *Context) Ini(name, value string) error {
n := C.CString(name)
defer C.free(unsafe.Pointer(n))
v := C.CString(value)
defer C.free(unsafe.Pointer(v))

_, err := C.context_ini(c.context, n, v)

return err
}

// Exec executes a PHP script pointed to by filename in the current execution
// context, and returns an error, if any. Output produced by the script is
// written to the context's pre-defined io.Writer instance.
Expand Down
18 changes: 18 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,24 @@ func TestContextBind(t *testing.T) {
c.Destroy()
}

func TestContextIni(t *testing.T) {
c, _ := e.NewContext()
defer c.Destroy()

path := "/path/set/by/go"
c.Ini("include_path", path)

val, err := c.Eval("return ini_get('include_path');")
if err != nil {
t.Errorf("Unexpected error while running script: %v", err)
return
}

if val.String() != path {
t.Errorf("Expected %s, have %s", path, val.String())
}
}

func TestContextDestroy(t *testing.T) {
c, _ := e.NewContext()
c.Destroy()
Expand Down
1 change: 1 addition & 0 deletions include/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ engine_context *context_new();
void context_exec(engine_context *context, char *filename);
void *context_eval(engine_context *context, char *script);
void context_bind(engine_context *context, char *name, void *value);
void context_ini(engine_context *context, char *name, char *value);
void context_destroy(engine_context *context);

#include "_context.h"
Expand Down
1 change: 1 addition & 0 deletions include/php5/_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define ___CONTEXT_H___

static void _context_bind(char *name, zval *value);
static void _context_ini(char *name, char *value);
static void _context_eval(zend_op_array *op, zval *ret);

#endif
1 change: 1 addition & 0 deletions include/php7/_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@

static void _context_bind(char *name, zval *value);
static void _context_eval(zend_op_array *op, zval *ret);
static void _context_ini(char *name, char *value);

#endif
6 changes: 6 additions & 0 deletions src/php5/_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ static void _context_bind(char *name, zval *value) {
ZEND_SET_SYMBOL(EG(active_symbol_table), name, value);
}

static void _context_ini(char *name, char *value) {
if (zend_alter_ini_entry_ex(name, strlen(name) + 1, value, strlen(value) + 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0) == FAILURE) {
errno = 1;
}
}

static void _context_eval(zend_op_array *op, zval *ret) {
zend_op_array *oparr = EG(active_op_array);
zval *retval = NULL;
Expand Down
11 changes: 11 additions & 0 deletions src/php7/_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ static void _context_bind(char *name, zval *value) {
zend_hash_str_update(&EG(symbol_table), name, strlen(name), value);
}

static void _context_ini(char *name, char *value) {
zend_string *n, *v;

// Use "permanent" strings
n = zend_string_init(name, strlen(name), 1);
v = zend_string_init(value, strlen(value), 1);
if (zend_alter_ini_entry_ex(n, v, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0) == FAILURE) {
errno = 1;
}
}

static void _context_eval(zend_op_array *op, zval *ret) {
EG(no_extensions) = 1;

Expand Down