Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify packages into single engine package #13

Merged
merged 1 commit into from
Jan 19, 2016
Merged
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
7 changes: 0 additions & 7 deletions context/php5.go

This file was deleted.

7 changes: 0 additions & 7 deletions context/php7.go

This file was deleted.

File renamed without changes.
26 changes: 11 additions & 15 deletions context/context.go → engine/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.

// Package context contains methods related to PHP engine execution contexts. It
// allows for binding Go variables and executing PHP scripts as a single request.
package context
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 -I../include
// #cgo CFLAGS: -I/usr/include/php/Zend -Iinclude
//
// #include <stdlib.h>
// #include <main/php.h>
Expand All @@ -19,8 +17,6 @@ import (
"io"
"net/http"
"unsafe"

"github.com/deuill/go-php/value"
)

// Context represents an individual execution context.
Expand All @@ -35,15 +31,15 @@ type Context struct {
Header http.Header

context *C.struct__engine_context
values map[string]*value.Value
values map[string]*Value
}

// New creates a new execution context for the active engine and returns an
// error if the execution context failed to initialize at any point.
func New() (*Context, error) {
// NewContext creates a new execution context for the active engine and returns
// an error if the execution context failed to initialize at any point.
func NewContext() (*Context, error) {
ctx := &Context{
Header: make(http.Header),
values: make(map[string]*value.Value),
values: make(map[string]*Value),
}

ptr, err := C.context_new(unsafe.Pointer(ctx))
Expand All @@ -58,10 +54,10 @@ func New() (*Context, error) {

// Bind allows for binding Go values into the current execution context under
// a certain name. Bind returns an error if attempting to bind an invalid value
// (check the documentation for value.New for what is considered to be a "valid"
// (check the documentation for NewValue for what is considered to be a "valid"
// value).
func (c *Context) Bind(name string, val interface{}) error {
v, err := value.New(val)
v, err := NewValue(val)
if err != nil {
return err
}
Expand Down Expand Up @@ -93,7 +89,7 @@ func (c *Context) Exec(filename string) error {
// Eval executes the PHP expression contained in script, and returns a Value
// containing the PHP value returned by the expression, if any. Any output
// produced is written context's pre-defined io.Writer instance.
func (c *Context) Eval(script string) (*value.Value, error) {
func (c *Context) Eval(script string) (*Value, error) {
// When PHP compiles code with a non-NULL return value expected, it simply
// prepends a `return` call to the code, thus breaking simple scripts that
// would otherwise work. Thus, we need to wrap the code in a closure, and
Expand All @@ -108,7 +104,7 @@ func (c *Context) Eval(script string) (*value.Value, error) {

defer C.free(result)

val, err := value.NewFromPtr(result)
val, err := NewValueFromPtr(result)
if err != nil {
return nil, err
}
Expand Down
25 changes: 11 additions & 14 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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 -I../include
// #cgo CFLAGS: -I/usr/include/php/Zend -Iinclude
//
// #include <stdlib.h>
// #include <main/php.h>
Expand All @@ -20,16 +20,13 @@ import (
"io"
"strings"
"unsafe"

"github.com/deuill/go-php/context"
"github.com/deuill/go-php/receiver"
)

// Engine represents the core PHP engine bindings.
type Engine struct {
engine *C.struct__php_engine
contexts []*context.Context
receivers map[string]*receiver.Receiver
contexts []*Context
receivers map[string]*Receiver
}

// New initializes a PHP engine instance on which contexts can be executed. It
Expand All @@ -42,8 +39,8 @@ func New() (*Engine, error) {

e := &Engine{
engine: ptr,
contexts: make([]*context.Context, 0),
receivers: make(map[string]*receiver.Receiver),
contexts: make([]*Context, 0),
receivers: make(map[string]*Receiver),
}

return e, nil
Expand All @@ -52,8 +49,8 @@ func New() (*Engine, error) {
// NewContext creates a new execution context on which scripts can be executed
// and variables can be binded. It corresponds to PHP's RINIT (request init)
// phase.
func (e *Engine) NewContext() (*context.Context, error) {
c, err := context.New()
func (e *Engine) NewContext() (*Context, error) {
c, err := NewContext()
if err != nil {
return nil, err
}
Expand All @@ -70,7 +67,7 @@ func (e *Engine) Define(name string, fn func(args []interface{}) interface{}) er
return fmt.Errorf("Failed to define duplicate receiver '%s'", name)
}

rcvr, err := receiver.New(name, fn)
rcvr, err := NewReceiver(name, fn)
if err != nil {
return err
}
Expand Down Expand Up @@ -118,21 +115,21 @@ func write(w io.Writer, buffer unsafe.Pointer, length C.uint) C.int {

//export engineWriteOut
func engineWriteOut(ctxptr, buffer unsafe.Pointer, length C.uint) C.int {
c := (*context.Context)(ctxptr)
c := (*Context)(ctxptr)

return write(c.Output, buffer, length)
}

//export engineWriteLog
func engineWriteLog(ctxptr unsafe.Pointer, buffer unsafe.Pointer, length C.uint) C.int {
c := (*context.Context)(ctxptr)
c := (*Context)(ctxptr)

return write(c.Log, buffer, length)
}

//export engineSetHeader
func engineSetHeader(ctxptr unsafe.Pointer, operation C.uint, buffer unsafe.Pointer, length C.uint) {
c := (*context.Context)(ctxptr)
c := (*Context)(ctxptr)

header := (string)(C.GoBytes(buffer, C.int(length)))
split := strings.SplitN(header, ":", 2)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion engine/php5.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

package engine

// #cgo CFLAGS: -I../include/php5
// #cgo CFLAGS: -Iinclude/php5
// #cgo LDFLAGS: -lphp5
import "C"
2 changes: 1 addition & 1 deletion engine/php7.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

package engine

// #cgo CFLAGS: -I../include/php7
// #cgo CFLAGS: -Iinclude/php7
// #cgo LDFLAGS: -lphp7
import "C"
File renamed without changes.
26 changes: 10 additions & 16 deletions receiver/receiver.go → engine/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.

// Package receiver implements one-way bindings for using Go method receivers as
// PHP classes, with full support for calling embedded methods as well as getting
// and setting embedded fields for struct method receivers.
package receiver
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 -I../include
// #cgo CFLAGS: -I/usr/include/php/Zend -Iinclude
//
// #include <stdlib.h>
// #include <main/php.h>
//
// #include "receiver.h"
import "C"

import (
"reflect"
"unsafe"

"github.com/deuill/go-php/value"
)

type object struct {
Expand All @@ -36,15 +30,15 @@ type Receiver struct {
objects []*object
}

// New registers a PHP class for the name passed, using function fn as constructor
// for individual object instances as needed by the PHP context.
// NewReceiver registers a PHP class for the name passed, using function fn as
// constructor for individual object instances as needed by the PHP context.
//
// The class name registered is assumed to be unique for the active engine.
//
// The constructor function accepts a slice of arguments, as passed by the PHP
// context, and should return a method receiver instance, or nil on error (in
// which case, an exception is thrown on the PHP object constructor).
func New(name string, fn func(args []interface{}) interface{}) (*Receiver, error) {
func NewReceiver(name string, fn func(args []interface{}) interface{}) (*Receiver, error) {
rcvr := &Receiver{
name: name,
create: fn,
Expand Down Expand Up @@ -79,7 +73,7 @@ func (r *Receiver) Destroy() {
func receiverNew(rcvr unsafe.Pointer, args unsafe.Pointer) unsafe.Pointer {
r := (*Receiver)(rcvr)

va, err := value.NewFromPtr(args)
va, err := NewValueFromPtr(args)
if err != nil {
return nil
}
Expand Down Expand Up @@ -123,7 +117,7 @@ func receiverGet(obj unsafe.Pointer, name *C.char) unsafe.Pointer {
return nil
}

result, err := value.New(o.values[n].Interface())
result, err := NewValue(o.values[n].Interface())
if err != nil {
return nil
}
Expand All @@ -141,7 +135,7 @@ func receiverSet(obj unsafe.Pointer, name *C.char, val unsafe.Pointer) {
return
}

v, err := value.NewFromPtr(val)
v, err := NewValueFromPtr(val)
if err != nil {
return
}
Expand Down Expand Up @@ -171,7 +165,7 @@ func receiverCall(obj unsafe.Pointer, name *C.char, args unsafe.Pointer) unsafe.
}

// Process input arguments.
va, err := value.NewFromPtr(args)
va, err := NewValueFromPtr(args)
if err != nil {
return nil
}
Expand Down Expand Up @@ -202,7 +196,7 @@ func receiverCall(obj unsafe.Pointer, name *C.char, args unsafe.Pointer) unsafe.
return nil
}

v, err := value.New(result)
v, err := NewValue(result)
if err != nil {
return nil
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
31 changes: 15 additions & 16 deletions value/value.go → engine/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.

// Package value implements value transformation between Go and PHP contexts.
package value
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 -I../include
// #cgo CFLAGS: -I/usr/include/php/Zend -Iinclude
//
// #include <stdlib.h>
// #include <stdbool.h>
Expand All @@ -25,12 +24,12 @@ var errInvalidType = func(v interface{}) error {
return fmt.Errorf("Unable to create value of unknown type '%T'", v)
}

// Kind represents the specific kind of type represented in Value.
type Kind int
// ValueKind represents the specific kind of type represented in Value.
type ValueKind int

// PHP types representable in Go.
const (
Null Kind = iota
Null ValueKind = iota
Long
Double
Bool
Expand All @@ -45,8 +44,8 @@ type Value struct {
value *C.struct__engine_value
}

// New creates a PHP value representation of a Go value val. Available bindings
// for Go to PHP types are:
// NewValue creates a PHP value representation of a Go value val. Available
// bindings for Go to PHP types are:
//
// int -> integer
// float64 -> double
Expand All @@ -60,7 +59,7 @@ type Value struct {
// struct fields are passed to the PHP context. Bindings for functions and method
// receivers to PHP functions and classes are only available in the engine scope,
// and must be predeclared before context execution.
func New(val interface{}) (*Value, error) {
func NewValue(val interface{}) (*Value, error) {
ptr, err := C.value_new()
if err != nil {
return nil, fmt.Errorf("Unable to create PHP value from Go value '%v'", val)
Expand Down Expand Up @@ -90,7 +89,7 @@ func New(val interface{}) (*Value, error) {
C.value_set_array(ptr, C.uint(v.Len()))

for i := 0; i < v.Len(); i++ {
vs, err := New(v.Index(i).Interface())
vs, err := NewValue(v.Index(i).Interface())
if err != nil {
C.value_destroy(ptr)
return nil, err
Expand All @@ -106,7 +105,7 @@ func New(val interface{}) (*Value, error) {
C.value_set_array(ptr, C.uint(v.Len()))

for _, key := range v.MapKeys() {
kv, err := New(v.MapIndex(key).Interface())
kv, err := NewValue(v.MapIndex(key).Interface())
if err != nil {
C.value_destroy(ptr)
return nil, err
Expand Down Expand Up @@ -135,7 +134,7 @@ func New(val interface{}) (*Value, error) {
continue
}

fv, err := New(v.Field(i).Interface())
fv, err := NewValue(v.Field(i).Interface())
if err != nil {
C.value_destroy(ptr)
return nil, err
Expand All @@ -153,8 +152,8 @@ func New(val interface{}) (*Value, error) {
return &Value{value: ptr}, nil
}

// NewFromPtr creates a Value type from an existing PHP value pointer.
func NewFromPtr(val unsafe.Pointer) (*Value, error) {
// NewValueFromPtr creates a Value type from an existing PHP value pointer.
func NewValueFromPtr(val unsafe.Pointer) (*Value, error) {
if val == nil {
return nil, fmt.Errorf("Cannot create value from 'nil' pointer")
}
Expand All @@ -172,8 +171,8 @@ func NewFromPtr(val unsafe.Pointer) (*Value, error) {
}

// Kind returns the Value's concrete kind of type.
func (v *Value) Kind() Kind {
return (Kind)(C.value_kind(v.value))
func (v *Value) Kind() ValueKind {
return (ValueKind)(C.value_kind(v.value))
}

// Interface returns the internal PHP value as it lies, with no conversion step.
Expand Down
2 changes: 1 addition & 1 deletion php_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,5 +391,5 @@ func TestContextReverseBind(t *testing.T) {

func init() {
wd, _ := os.Getwd()
testDir = path.Join(wd, ".tests")
testDir = path.Join(wd, "engine/tests")
}
7 changes: 0 additions & 7 deletions receiver/php5.go

This file was deleted.

Loading