1
0
mirror of https://github.com/deuill/go-php.git synced 2024-09-21 08:50:45 +00:00

Split code into seperate directories.

This commit is contained in:
Alex Palaistras 2015-10-02 00:48:33 +01:00
parent 48a7c537a3
commit 7f797605e9
15 changed files with 125 additions and 89 deletions

View File

@ -1,15 +0,0 @@
#ifndef CONTEXT_H
#define CONTEXT_H
typedef struct _engine_context {
php_engine *engine; // Parent engine instance.
void *parent; // Pointer to parent Go context, used for passing to callbacks.
} engine_context;
engine_context *context_new(php_engine *engine, void *parent);
void context_exec(engine_context *context, char *filename);
void context_bind(engine_context *context, char *name, void *zvalptr);
int context_write(engine_context *context, const char *message, unsigned int length);
void context_destroy(engine_context *context);
#endif

View File

@ -4,26 +4,24 @@
#include <main/SAPI.h>
#include <main/php_main.h>
#include "engine.h"
#include "context.h"
#include "_cgo_export.h"
engine_context *context_new(php_engine *engine, void *parent) {
engine_context *context_new(void *parent) {
engine_context *context;
#ifdef ZTS
void ***tsrm_ls = engine->tsrm_ls;
#endif
// Initialize context.
context = (engine_context *) malloc((sizeof(engine_context)));
if (context == NULL) {
return_multi(NULL, 1);
errno = 1;
return NULL;
}
context->engine = engine;
context->parent = parent;
#ifdef ZTS
TSRM_FETCH()
context->ptsrm_ls = &tsrm_ls;
#endif
context->parent = parent;
SG(server_context) = (void *) context;
// Initialize request lifecycle.
@ -31,15 +29,17 @@ engine_context *context_new(php_engine *engine, void *parent) {
SG(server_context) = NULL;
free(context);
return_multi(NULL, 1);
errno = 1;
return NULL;
}
return_multi(context, 0);
errno = 0;
return context;
}
void context_exec(engine_context *context, char *filename) {
#ifdef ZTS
void ***tsrm_ls = context->engine->tsrm_ls;
void ***tsrm_ls = *context->ptsrm_ls;
#endif
// Attempt to execute script file.
@ -54,27 +54,21 @@ void context_exec(engine_context *context, char *filename) {
php_execute_script(&script TSRMLS_CC);
} zend_end_try();
return_multi(NULL, 0);
errno = 0;
return NULL;
}
void context_bind(engine_context *context, char *name, void *zvalptr) {
zval *value = (zval *) zvalptr;
#ifdef ZTS
void ***tsrm_ls = context->engine->tsrm_ls;
void ***tsrm_ls = *context->ptsrm_ls;
#endif
ZEND_SET_SYMBOL(EG(active_symbol_table), name, value);
return_multi(NULL, 0);
}
int context_write(engine_context *context, const char *message, unsigned int length) {
int written = contextWrite(context->parent, (void *) message, length);
if (written != length) {
php_handle_aborted_connection();
}
return written;
errno = 0;
return NULL;
}
void context_destroy(engine_context *context) {

View File

@ -1,7 +1,10 @@
package php
package context
// #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 <stdlib.h>
// #include "engine.h"
// #include "context.h"
import "C"
@ -9,16 +12,18 @@ import (
"fmt"
"io"
"unsafe"
"github.com/deuill/go-php/value"
)
type Context struct {
context *C.struct__engine_context
writer io.Writer
values map[string]*Value
values map[string]*value.Value
}
func (c *Context) Bind(name string, value interface{}) error {
v, err := NewValue(value)
func (c *Context) Bind(name string, val interface{}) error {
v, err := value.New(val)
if err != nil {
return err
}
@ -28,7 +33,7 @@ func (c *Context) Bind(name string, value interface{}) error {
if _, err = C.context_bind(c.context, n, v.Ptr()); err != nil {
v.Destroy()
return fmt.Errorf("Binding value '%v' to context failed", value)
return fmt.Errorf("Binding value '%v' to context failed", val)
}
c.values[name] = v
@ -48,6 +53,10 @@ func (c *Context) Exec(filename string) error {
return nil
}
func (c *Context) Write(p []byte) (int, error) {
return c.writer.Write(p)
}
func (c *Context) Destroy() {
for _, v := range c.values {
v.Destroy()
@ -57,14 +66,15 @@ func (c *Context) Destroy() {
c = nil
}
//export contextWrite
func contextWrite(ctxptr unsafe.Pointer, buffer unsafe.Pointer, length C.uint) C.int {
context := (*Context)(ctxptr)
func New(w io.Writer) (*Context, error) {
ctx := &Context{writer: w, values: make(map[string]*value.Value)}
written, err := context.writer.Write(C.GoBytes(buffer, C.int(length)))
ptr, err := C.context_new(unsafe.Pointer(ctx))
if err != nil {
return C.int(-1)
return nil, fmt.Errorf("Failed to initialize context for PHP engine")
}
return C.int(written)
ctx.context = ptr
return ctx, nil
}

17
context/context.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef CONTEXT_H
#define CONTEXT_H
typedef struct _engine_context {
#ifdef ZTS
void *ptsrm_ls; // Pointer to TSRM local storage.
#endif
void *parent; // Pointer to parent Go context, used for passing to callbacks.
} engine_context;
engine_context *context_new(void *parent);
void context_exec(engine_context *context, char *filename);
void context_bind(engine_context *context, char *name, void *zvalptr);
void context_destroy(engine_context *context);
#endif

View File

@ -1,10 +1,12 @@
package php
package context_test
import (
"os"
"path"
"strconv"
"testing"
php "github.com/deuill/go-php"
)
var testDir string
@ -59,8 +61,8 @@ func (m *MockWriter) Reset() {
func TestContextExec(t *testing.T) {
var w MockWriter
e, _ := New()
ctx, _ := e.NewContext(&w)
e, _ := php.New()
ctx, _ := php.NewContext(&w)
defer e.Destroy()
defer ctx.Destroy()
@ -83,8 +85,8 @@ func TestContextExec(t *testing.T) {
func TestContextBind(t *testing.T) {
var w MockWriter
e, _ := New()
ctx, _ := e.NewContext(&w)
e, _ := php.New()
ctx, _ := php.NewContext(&w)
defer e.Destroy()
defer ctx.Destroy()
@ -107,5 +109,5 @@ func TestContextBind(t *testing.T) {
func init() {
wd, _ := os.Getwd()
testDir = path.Join(wd, ".tests")
testDir = path.Join(wd, "..", ".test")
}

View File

@ -7,8 +7,9 @@
#include <main/php_variables.h>
#include <TSRM/TSRM.h>
#include "engine.h"
#include "context.h"
#include "engine.h"
#include "_cgo_export.h"
const char engine_ini_defaults[] =
"html_errors = 0\n"
@ -21,7 +22,13 @@ const char engine_ini_defaults[] =
static int engine_ub_write(const char *str, uint str_length TSRMLS_DC) {
engine_context *context = (engine_context *) SG(server_context);
return context_write(context, str, str_length);
int written = uwrite(context->parent, (void *) str, str_length);
if (written != str_length) {
php_handle_aborted_connection();
}
return written;
}
static void engine_send_header(sapi_header_struct *sapi_header, void *server_context TSRMLS_DC) {
@ -99,7 +106,8 @@ php_engine *engine_init(void) {
tsrm_shutdown();
#endif
return_multi(NULL, 1);
errno = 1;
return NULL;
}
engine = (php_engine *) malloc((sizeof(php_engine)));
@ -108,7 +116,8 @@ php_engine *engine_init(void) {
engine->tsrm_ls = tsrm_ls;
#endif
return_multi(engine, 0);
errno = 0;
return engine;
}
void engine_shutdown(php_engine *engine) {

View File

@ -1,36 +1,24 @@
package php
package engine
// #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
// #cgo CFLAGS: -I/usr/include/php/Zend -I../context
// #cgo LDFLAGS: -L${SRCDIR}/context -lphp5
//
// #include "engine.h"
// #include "context.h"
// #include "engine.h"
import "C"
import (
"fmt"
"io"
"unsafe"
"github.com/deuill/go-php/context"
)
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
@ -44,3 +32,15 @@ func New() (*Engine, error) {
return &Engine{engine: ptr}, nil
}
//export uwrite
func uwrite(ctxptr unsafe.Pointer, buffer unsafe.Pointer, length C.uint) C.int {
context := (*context.Context)(ctxptr)
written, err := context.Write(C.GoBytes(buffer, C.int(length)))
if err != nil {
return C.int(-1)
}
return C.int(written)
}

View File

@ -1,8 +1,6 @@
#ifndef ENGINE_H
#define ENGINE_H
#define return_multi(value, error) errno = error; return value
typedef struct _php_engine {
#ifdef ZTS
void ***tsrm_ls; // Local storage for thread-safe operations, used across the PHP engine.

View File

@ -1,19 +1,21 @@
package php
package engine_test
import (
"os"
"testing"
php "github.com/deuill/go-php"
)
func TestNewEngineContext(t *testing.T) {
e, err := New()
e, err := php.New()
if err != nil {
t.Errorf("New(): %s", err)
}
defer e.Destroy()
ctx, err := e.NewContext(os.Stdout)
ctx, err := php.NewContext(os.Stdout)
if err != nil {
t.Errorf("NewContext(): %s", err)
}

16
php.go Normal file
View File

@ -0,0 +1,16 @@
package php
import (
"io"
"github.com/deuill/go-php/context"
"github.com/deuill/go-php/engine"
)
func New() (*engine.Engine, error) {
return engine.New()
}
func NewContext(w io.Writer) (*context.Context, error) {
return context.New(w)
}

View File

@ -3,7 +3,6 @@
#include <main/php.h>
#include "engine.h"
#include "value.h"
void *value_create_long(long int value) {

View File

@ -1,5 +1,9 @@
package php
package value
// #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 <stdlib.h>
// #include <stdbool.h>
// #include "value.h"
@ -26,7 +30,7 @@ func (v *Value) Destroy() {
v = nil
}
func NewValue(val interface{}) (*Value, error) {
func New(val interface{}) (*Value, error) {
var ptr unsafe.Pointer
// Determine value type and create PHP value from the concrete type.
@ -52,7 +56,7 @@ func NewValue(val interface{}) (*Value, error) {
ptr = C.value_create_array(C.uint(v.Len()))
for i := 0; i < v.Len(); i++ {
vs, err := NewValue(v.Index(i).Interface())
vs, err := New(v.Index(i).Interface())
if err != nil {
return nil, err
}
@ -67,7 +71,7 @@ func NewValue(val interface{}) (*Value, error) {
ptr = C.value_create_array(C.uint(v.Len()))
for _, k := range v.MapKeys() {
vm, err := NewValue(v.MapIndex(k).Interface())
vm, err := New(v.MapIndex(k).Interface())
if err != nil {
return nil, err
}