1
0
mirror of https://github.com/deuill/go-php.git synced 2024-09-21 08:50:45 +00:00
go-php/engine/engine.go
Alex Palaistras 7ed2c81ec6 Move NewContext function to engine package
Call order is now enforced when using the root `php` package by moving
the `NewContext` function in the `engine` package, as a method of the
`Engine` method receiver. Further clean-ups have been made to the code
as a result of this change.
2015-10-16 14:42:15 +01:00

66 lines
1.7 KiB
Go

// Copyright 2015 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 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../context
// #cgo LDFLAGS: -L${SRCDIR}/context -lphp5
//
// #include "context.h"
// #include "engine.h"
import "C"
import (
"fmt"
"io"
"github.com/deuill/go-php/context"
)
// Engine represents the core PHP engine bindings.
type Engine struct {
engine *C.struct__php_engine
contexts []*context.Context
}
// 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(w io.Writer) (*context.Context, error) {
c, err := context.New(w)
if err != nil {
return nil, err
}
e.contexts = append(e.contexts, c)
return c, nil
}
// Destroy shuts down and frees any resources related to the PHP engine bindings.
func (e *Engine) Destroy() {
for _, c := range e.contexts {
c.Destroy()
}
if e.engine != nil {
C.engine_shutdown(e.engine)
e.engine = nil
}
}
// 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) {
ptr, err := C.engine_init()
if err != nil {
return nil, fmt.Errorf("PHP engine failed to initialize")
}
return &Engine{engine: ptr, contexts: make([]*context.Context, 0)}, nil
}