forked from deuill/go-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
277 lines (226 loc) · 6.65 KB
/
engine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2017 Alexander Palaistras. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
// Package engine provides methods allowing for the initialization and teardown
// of PHP engine bindings, off which execution contexts can be launched.
package gophp
//
// #include <stdlib.h>
// #include <main/php.h>
// #include "include/gophp_receiver.h"
// #include "include/gophp_context.h"
// #include "include/gophp_engine.h"
import "C"
import (
"fmt"
"io"
"net/http"
"strings"
"unsafe"
)
// Engine represents the core PHP engine bindings.
type Engine struct {
engine *C.struct__php_engine
contexts map[*C.struct__gophp_context]*Context
receivers map[string]*Receiver
}
// This contains a reference to the active engine, if any.
var engine *Engine
// New initializes a PHP engine instance on which contexts can be executed. It
// corresponds to PHP's MINIT (module init) phase.
func New() (*Engine, error) {
if engine != nil {
return nil, fmt.Errorf("Cannot activate multiple engine instances")
}
ptr, err := C.engine_init()
if err != nil {
return nil, fmt.Errorf("PHP engine failed to initialize")
}
engine = &Engine{
engine: ptr,
contexts: make(map[*C.struct__gophp_context]*Context),
receivers: make(map[string]*Receiver),
}
return engine, nil
}
// NewContext creates a new execution context for the active engine and returns
// an error if the execution context failed to initialize at any point. This
// corresponds to PHP's RINIT (request init) phase.
func (e *Engine) NewContext() (*Context, error) {
ptr, err := C.new_context()
if err != nil {
return nil, fmt.Errorf("Failed to initialize context for PHP engine" + err.Error())
}
ctx := &Context{
Header: make(http.Header),
context: ptr,
values: make([]*Value, 0),
}
// Store reference to context, using pointer as key.
e.contexts[ptr] = ctx
return ctx, nil
}
// Define 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 (e *Engine) Define(name string, fn func(args []interface{}) interface{}) error {
if _, exists := e.receivers[name]; exists {
return fmt.Errorf("Failed to define duplicate receiver '%s'", name)
}
rcvr := &Receiver{
name: name,
create: fn,
objects: make(map[*C.struct__zend_object]*ReceiverObject),
}
n := C.CString(name)
defer C.free(unsafe.Pointer(n))
C.receiver_define(n)
e.receivers[name] = rcvr
return nil
}
// Destroy shuts down and frees any resources related to the PHP engine bindings.
func (e *Engine) Destroy() {
if e.engine == nil {
return
}
for _, r := range e.receivers {
r.Destroy()
}
e.receivers = nil
for _, c := range e.contexts {
c.Destroy()
}
e.contexts = nil
C.engine_shutdown()
C.free(unsafe.Pointer(e.engine))
e.engine = nil
engine = nil
}
func write(w io.Writer, buffer unsafe.Pointer, length C.uint) C.int {
// Do not return error if writer is unavailable.
if w == nil {
return C.int(length)
}
written, err := w.Write(C.GoBytes(buffer, C.int(length)))
if err != nil {
return -1
}
return C.int(written)
}
//export engineWriteOut
func engineWriteOut(ctx *C.struct__gophp_context, buffer unsafe.Pointer, length C.uint) C.int {
if engine == nil || engine.contexts[ctx] == nil {
return -1
}
return write(engine.contexts[ctx].Output, buffer, length)
}
//export engineWriteLog
func engineWriteLog(ctx *C.struct__gophp_context, buffer unsafe.Pointer, length C.uint) C.int {
if engine == nil || engine.contexts[ctx] == nil {
return -1
}
return write(engine.contexts[ctx].Log, buffer, length)
}
//export engineSetHeader
func engineSetHeader(ctx *C.struct__gophp_context, operation C.uint, buffer unsafe.Pointer, length C.uint) {
if engine == nil || engine.contexts[ctx] == nil {
return
}
header := (string)(C.GoBytes(buffer, C.int(length)))
split := strings.SplitN(header, ":", 2)
for i := range split {
split[i] = strings.TrimSpace(split[i])
}
switch operation {
case 0: // Replace header.
if len(split) == 2 && split[1] != "" {
engine.contexts[ctx].Header.Set(split[0], split[1])
}
case 1: // Append header.
if len(split) == 2 && split[1] != "" {
engine.contexts[ctx].Header.Add(split[0], split[1])
}
case 2: // Delete header.
if split[0] != "" {
engine.contexts[ctx].Header.Del(split[0])
}
}
}
//export engineReceiverNew
func engineReceiverNew(rcvr *C.struct__zend_object, args unsafe.Pointer) C.int {
n := C.GoString(C.get_receiver_name(rcvr))
if engine == nil || engine.receivers[n] == nil {
fmt.Println("ENGINE IS NULL")
return 1
}
va, err := NewValueFromPtr(args)
if err != nil {
fmt.Println(err.Error())
return 110
}
defer va.Destroy()
obj, err := engine.receivers[n].NewObject(va.Slice())
if err != nil {
return 12
}
engine.receivers[n].objects[rcvr] = obj
return 0
}
//export engineReceiverGet
func engineReceiverGet(rcvr *C.struct__zend_object, name *C.char) unsafe.Pointer {
n := C.GoString(C.get_receiver_name(rcvr))
if engine == nil || engine.receivers[n].objects[rcvr] == nil {
return nil
}
val, err := engine.receivers[n].objects[rcvr].Get(C.GoString(name))
if err != nil {
return nil
}
return val.Ptr()
}
//export engineReceiverSet
func engineReceiverSet(rcvr *C.struct__zend_object, name *C.char, val unsafe.Pointer) {
n := C.GoString(C.get_receiver_name(rcvr))
if engine == nil || engine.receivers[n].objects[rcvr] == nil {
return
}
v, err := NewValueFromPtr(val)
if err != nil {
return
}
engine.receivers[n].objects[rcvr].Set(C.GoString(name), v.Interface())
}
//export engineReceiverExists
func engineReceiverExists(rcvr *C.struct__zend_object, name *C.char) C.int {
n := C.GoString(C.get_receiver_name(rcvr))
if engine == nil || engine.receivers[n].objects[rcvr] == nil {
return 0
}
if engine.receivers[n].objects[rcvr].Exists(C.GoString(name)) {
return 1
}
return 0
}
//export engineReceiverCall
func engineReceiverCall(rcvr *C.struct__zend_object, name *C.char, args unsafe.Pointer) unsafe.Pointer {
n := C.GoString(C.get_receiver_name(rcvr))
if engine == nil || engine.receivers[n].objects[rcvr] == nil {
return nil
}
// Process input arguments.
va, err := NewValueFromPtr(args)
if err != nil {
return nil
}
defer va.Destroy()
val := engine.receivers[n].objects[rcvr].Call(C.GoString(name), va.Slice())
if val == nil {
return nil
}
return val.Ptr()
}