php: Move server code into new phpx package
This commit is contained in:
parent
4df5f6387c
commit
2e47626900
13 changed files with 134 additions and 86 deletions
|
|
@ -3,6 +3,7 @@ package extras
|
|||
import (
|
||||
_ "embed"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/php"
|
||||
"golang.org/x/exp/slices"
|
||||
|
|
@ -20,7 +21,7 @@ var pathbuilderPHP string
|
|||
// All returns the ids of all pathbuilders in consistent order.
|
||||
//
|
||||
// server is the server to fetch the pathbuilders from, any may be nil.
|
||||
func (pathbuilder *Pathbuilder) All(server *php.Server) (ids []string, err error) {
|
||||
func (pathbuilder *Pathbuilder) All(server *phpx.Server) (ids []string, err error) {
|
||||
err = pathbuilder.PHP.ExecScript(server, &ids, pathbuilderPHP, "all_list")
|
||||
slices.Sort(ids)
|
||||
return
|
||||
|
|
@ -30,7 +31,7 @@ func (pathbuilder *Pathbuilder) All(server *php.Server) (ids []string, err error
|
|||
// If it does not exist, it returns the empty string and nil error.
|
||||
//
|
||||
// server is the server to fetch the pathbuilders from, any may be nil.
|
||||
func (pathbuilder *Pathbuilder) Get(server *php.Server, id string) (xml string, err error) {
|
||||
func (pathbuilder *Pathbuilder) Get(server *phpx.Server, id string) (xml string, err error) {
|
||||
err = pathbuilder.PHP.ExecScript(server, &xml, pathbuilderPHP, "one_xml", id)
|
||||
return
|
||||
}
|
||||
|
|
@ -38,7 +39,7 @@ func (pathbuilder *Pathbuilder) Get(server *php.Server, id string) (xml string,
|
|||
// GetAll returns all pathbuilders serialized as xml
|
||||
//
|
||||
// server is the server to fetch the pathbuilders from, any may be nil.
|
||||
func (pathbuilder *Pathbuilder) GetAll(server *php.Server) (pathbuilders map[string]string, err error) {
|
||||
func (pathbuilder *Pathbuilder) GetAll(server *phpx.Server) (pathbuilders map[string]string, err error) {
|
||||
err = pathbuilder.PHP.ExecScript(server, &pathbuilders, pathbuilderPHP, "all_xml")
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/mstore"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/php"
|
||||
|
|
@ -35,7 +36,7 @@ var listURIPrefixesPHP string
|
|||
//
|
||||
// server is an optional server to fetch prefixes from.
|
||||
// server may be nil.
|
||||
func (prefixes *Prefixes) All(server *php.Server) ([]string, error) {
|
||||
func (prefixes *Prefixes) All(server *phpx.Server) ([]string, error) {
|
||||
uris, err := prefixes.database(server)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -49,7 +50,7 @@ func (prefixes *Prefixes) All(server *php.Server) ([]string, error) {
|
|||
return append(uris, uris2...), nil
|
||||
}
|
||||
|
||||
func (wisski *Prefixes) database(server *php.Server) (prefixes []string, err error) {
|
||||
func (wisski *Prefixes) database(server *phpx.Server) (prefixes []string, err error) {
|
||||
// get all the ugly prefixes
|
||||
err = wisski.PHP.ExecScript(server, &prefixes, listURIPrefixesPHP, "list_prefixes")
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package extras
|
|||
import (
|
||||
_ "embed"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/php"
|
||||
)
|
||||
|
|
@ -16,11 +17,11 @@ type Settings struct {
|
|||
//go:embed settings.php
|
||||
var settingsPHP string
|
||||
|
||||
func (settings *Settings) Get(server *php.Server, key string) (value any, err error) {
|
||||
func (settings *Settings) Get(server *phpx.Server, key string) (value any, err error) {
|
||||
err = settings.PHP.ExecScript(server, &value, settingsPHP, "get_setting", key)
|
||||
return
|
||||
}
|
||||
|
||||
func (settings *Settings) Set(server *php.Server, key string, value any) error {
|
||||
func (settings *Settings) Set(server *phpx.Server, key string, value any) error {
|
||||
return settings.PHP.ExecScript(server, nil, settingsPHP, "set_setting", key, value)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package php
|
|||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/barrel"
|
||||
)
|
||||
|
|
@ -27,9 +28,9 @@ type PHP struct {
|
|||
// It's arguments are encoded as json using [json.Marshal] and decoded within php.
|
||||
//
|
||||
// The return value of the function is again marshaled with json and returned to the caller.
|
||||
func (php *PHP) ExecScript(server *Server, value any, code string, entrypoint string, args ...any) (err error) {
|
||||
func (php *PHP) ExecScript(server *phpx.Server, value any, code string, entrypoint string, args ...any) (err error) {
|
||||
if server == nil {
|
||||
server, err = php.NewServer()
|
||||
server = php.NewServer()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -45,9 +46,9 @@ func (php *PHP) ExecScript(server *Server, value any, code string, entrypoint st
|
|||
return server.MarshalCall(value, entrypoint, args...)
|
||||
}
|
||||
|
||||
func (php *PHP) EvalCode(server *Server, value any, code string) (err error) {
|
||||
func (php *PHP) EvalCode(server *phpx.Server, value any, code string) (err error) {
|
||||
if server == nil {
|
||||
server, err = php.NewServer()
|
||||
server = php.NewServer()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,36 +0,0 @@
|
|||
package phpserver
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Common PHP Errors
|
||||
var (
|
||||
errPHPInit = "Unable to initialize"
|
||||
errPHPMarshal = "Marshal failed"
|
||||
errPHPInvalid = ServerError{Message: "Invalid code to execute"}
|
||||
errPHPReceive = "Failed to receive response"
|
||||
errPHPClosed = ServerError{Message: "Server closed"}
|
||||
)
|
||||
|
||||
// PHPError represents an error during PHPServer logic
|
||||
type ServerError struct {
|
||||
Message string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (err ServerError) Unwrap() error {
|
||||
return err.Err
|
||||
}
|
||||
|
||||
func (err ServerError) Error() string {
|
||||
if err.Err == nil {
|
||||
return fmt.Sprintf("PHPServer: %s", err.Message)
|
||||
}
|
||||
return fmt.Sprintf("PHPServer: %s: %s", err.Message, err.Err)
|
||||
}
|
||||
|
||||
// Throwable represents an error during php code
|
||||
type Throwable string
|
||||
|
||||
func (throwable Throwable) Error() string {
|
||||
return string(throwable)
|
||||
}
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
package phpserver
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Marshal marshals data as a PHP expression, meaning it can safely be used inside code.
|
||||
//
|
||||
// Typically data is marshaled using [json.Marshal] and decoded in PHP using 'json_decode'.
|
||||
// Special cases may exist for specific datatypes.
|
||||
func Marshal(data any) (string, error) {
|
||||
switch d := data.(type) {
|
||||
case string:
|
||||
return MarshalString(d), nil
|
||||
}
|
||||
|
||||
bytes, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return "json_decode(" + MarshalString(string(bytes)) + ")", nil
|
||||
}
|
||||
|
||||
var replacer = strings.NewReplacer("'", "\\'", "\\", "\\\\")
|
||||
|
||||
// MarshalString marshals s as a php string that can be used safely as a PHP expression.
|
||||
//
|
||||
// See [https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single].
|
||||
func MarshalString(s string) string {
|
||||
return "'" + replacer.Replace(s) + "'"
|
||||
}
|
||||
|
|
@ -1,216 +0,0 @@
|
|||
package phpserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
_ "embed"
|
||||
|
||||
"github.com/tkw1536/goprogram/lib/collection"
|
||||
"github.com/tkw1536/goprogram/lib/nobufio"
|
||||
"github.com/tkw1536/goprogram/stream"
|
||||
)
|
||||
|
||||
// New creates a new server, with execPHP as a method to call a PHP Shell.
|
||||
func New(execPHP func(str stream.IOStream, script string)) (*Server, error) {
|
||||
// create input and output pipes
|
||||
ir, iw, err := os.Pipe()
|
||||
if err != nil {
|
||||
return nil, ServerError{errPHPInit, err}
|
||||
}
|
||||
or, ow, err := os.Pipe()
|
||||
if err != nil {
|
||||
ir.Close()
|
||||
iw.Close()
|
||||
return nil, ServerError{errPHPInit, err}
|
||||
}
|
||||
|
||||
// create a context to close the server
|
||||
context, cancel := context.WithCancel(context.Background())
|
||||
|
||||
// start the shell process, which will close everything once done
|
||||
go func() {
|
||||
defer func() {
|
||||
ir.Close()
|
||||
iw.Close()
|
||||
or.Close()
|
||||
ow.Close()
|
||||
|
||||
cancel()
|
||||
}()
|
||||
|
||||
// start the server
|
||||
io := stream.NewIOStream(ow, nil, ir, 0)
|
||||
execPHP(io, serverPHP)
|
||||
}()
|
||||
|
||||
// return a new server
|
||||
return &Server{
|
||||
in: iw,
|
||||
out: or,
|
||||
c: context,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Server represents a server that executes code within a distillery.
|
||||
// A typical use-case is to define functions using [MarshalEval], and then call those functions [MarshalCall].
|
||||
//
|
||||
// A nil Server will return [ErrServerBroken] on every function call.
|
||||
type Server struct {
|
||||
m sync.Mutex
|
||||
|
||||
in io.WriteCloser
|
||||
out io.Reader
|
||||
c context.Context
|
||||
}
|
||||
|
||||
// MarshalEval evaluates code on the server and Marshals the result into value.
|
||||
// When value is nil, the results are discarded.
|
||||
//
|
||||
// code is directly passed to php's "eval" function.
|
||||
// as such any functions defined will remain in server memory.
|
||||
//
|
||||
// When an exception is thrown by the PHP Code, error is not nil, and dest remains unchanged.
|
||||
func (server *Server) MarshalEval(value any, code string) error {
|
||||
server.m.Lock()
|
||||
defer server.m.Unlock()
|
||||
|
||||
// quick hack: when the server is already done
|
||||
if err := server.c.Err(); err != nil {
|
||||
return errPHPClosed
|
||||
}
|
||||
|
||||
// find a delimiter for the code, and then send
|
||||
delim := findDelimiter(code)
|
||||
io.WriteString(server.in, delim+"\n"+code+"\n"+delim+"\n")
|
||||
|
||||
// read the next line (as a response)
|
||||
data, err := nobufio.ReadLine(server.out)
|
||||
if err != nil {
|
||||
return ServerError{Message: errPHPReceive, Err: err}
|
||||
}
|
||||
|
||||
// read whatever we received
|
||||
var received [2]json.RawMessage
|
||||
if err := json.Unmarshal([]byte(data), &received); err != nil {
|
||||
return ServerError{Message: errPHPMarshal, Err: err}
|
||||
}
|
||||
|
||||
// check if there was an error
|
||||
var errString string
|
||||
if err := json.Unmarshal(received[1], &errString); err == nil && errString != "" {
|
||||
return Throwable(errString)
|
||||
}
|
||||
|
||||
// special case: no return value => no unmarshaling needed
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// read the actual result!
|
||||
return json.Unmarshal(received[0], value)
|
||||
}
|
||||
|
||||
// Eval is like [MarshalEval], but returns the value as an any
|
||||
func (server *Server) Eval(code string) (value any, err error) {
|
||||
err = server.MarshalEval(&value, code)
|
||||
return
|
||||
}
|
||||
|
||||
// MarshalCall calls a previously defined function with the given arguments.
|
||||
// Arguments are sent to php using json Marshal, and are 'json_decode'd on the php side.
|
||||
//
|
||||
// Return values are received as in [MarshalEval].
|
||||
func (server *Server) MarshalCall(value any, function string, args ...any) error {
|
||||
|
||||
// name of function to call
|
||||
name := MarshalString(function)
|
||||
|
||||
// generate code to call
|
||||
var code string
|
||||
switch len(args) {
|
||||
case 0:
|
||||
code = "return call_user_func(" + name + ");"
|
||||
case 1:
|
||||
param, err := Marshal(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
code = "return call_user_func(" + name + "," + param + ");"
|
||||
default:
|
||||
params, err := Marshal(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
code = "return call_user_func_array(" + name + "," + params + ");"
|
||||
}
|
||||
|
||||
// and evaluate the code
|
||||
return server.MarshalEval(value, code)
|
||||
}
|
||||
|
||||
// Call is like [MarshalCall] but returns the return value of the function as an any
|
||||
func (server *Server) Call(function string, args ...any) (value any, err error) {
|
||||
err = server.MarshalCall(&value, function, args...)
|
||||
return
|
||||
}
|
||||
|
||||
const delimiterRune = 'F' // press to pay respect
|
||||
|
||||
// findDelimiter finds a delimiter that does not occur in the input string
|
||||
func findDelimiter(input string) string {
|
||||
// find the longest sequence of delimiter rune
|
||||
var current, longest int
|
||||
for _, r := range input {
|
||||
if r == delimiterRune {
|
||||
current++
|
||||
} else {
|
||||
current = 0
|
||||
}
|
||||
|
||||
if current > longest {
|
||||
longest = current
|
||||
}
|
||||
}
|
||||
// and then return it multipled longer than that
|
||||
return strings.Repeat(string(delimiterRune), longest+1)
|
||||
}
|
||||
|
||||
// Close closes this server and prevents any further code from being run.
|
||||
func (server *Server) Close() error {
|
||||
server.m.Lock()
|
||||
defer server.m.Unlock()
|
||||
|
||||
// if the context is already closed
|
||||
if err := server.c.Err(); err != nil {
|
||||
return errPHPClosed
|
||||
}
|
||||
|
||||
server.in.Close()
|
||||
<-server.c.Done()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//go:embed server.php
|
||||
var serverPHP string
|
||||
|
||||
// pre-process the server.php code to make it shorter
|
||||
func init() {
|
||||
// remove the first '<?php' line
|
||||
lines := strings.Split(serverPHP, "\n")[1:]
|
||||
for i, line := range lines {
|
||||
lines[i] = strings.TrimSpace(line)
|
||||
}
|
||||
|
||||
// remove comment lines
|
||||
lines = collection.Filter(lines, func(line string) bool {
|
||||
return !strings.HasPrefix(line, "//")
|
||||
})
|
||||
|
||||
serverPHP = strings.Join(lines, "")
|
||||
}
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
<?php
|
||||
|
||||
// This file contains code to execute a php execution server.
|
||||
// It is passed as a *command line literal * directly to 'drush:script'.
|
||||
//
|
||||
// As such it is preprocessed and shortened.
|
||||
// It should only contain comments at the beginning of each line, and only starting with '//'.
|
||||
// See server.go.
|
||||
|
||||
// prevent STDIN from being buffered
|
||||
stream_set_read_buffer(STDIN,0);
|
||||
|
||||
// stop outputting errors when executing
|
||||
ob_start(null,0,PHP_OUTPUT_HANDLER_CLEANABLE);
|
||||
|
||||
|
||||
while(1){
|
||||
// read the next line to get an end-of-line marker
|
||||
$marker = fgets(STDIN);
|
||||
if (!$marker) break;
|
||||
|
||||
// accumulate the buffer until the marker is reached
|
||||
// bail out if there is an unexpected end of input
|
||||
$buffer = "";
|
||||
while(1) {
|
||||
$line = fgets(STDIN);
|
||||
if (!$line) break 2;
|
||||
if ($line === $marker) break;
|
||||
$buffer .= $line . "\n";
|
||||
}
|
||||
|
||||
// execute it
|
||||
try{
|
||||
$json = json_encode([eval($buffer),""]);
|
||||
}catch(Throwable $t){
|
||||
$json = json_encode([null,(string)$t]);
|
||||
}
|
||||
if($json===false) {
|
||||
$json = '[null,"Error encoding result"]';
|
||||
}
|
||||
|
||||
// and write out the result
|
||||
ob_end_clean();
|
||||
fwrite(STDOUT,"$json\n");
|
||||
ob_start(null,0,PHP_OUTPUT_HANDLER_CLEANABLE);
|
||||
}
|
||||
|
|
@ -3,19 +3,22 @@ package php
|
|||
import (
|
||||
_ "embed"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/php/phpserver"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
|
||||
"github.com/alessio/shellescape"
|
||||
"github.com/tkw1536/goprogram/stream"
|
||||
)
|
||||
|
||||
type Server = phpserver.Server
|
||||
|
||||
// NewServer returns a new server that can execute code within this distillery.
|
||||
// When err == nil, the caller must call server.Close().
|
||||
//
|
||||
// See [PHPServer].
|
||||
func (php *PHP) NewServer() (*Server, error) {
|
||||
return phpserver.New(func(str stream.IOStream, script string) {
|
||||
php.Barrel.Shell(str, "-c", shellescape.QuoteCommand([]string{"drush", "php:eval", script}))
|
||||
})
|
||||
func (php *PHP) NewServer() *phpx.Server {
|
||||
return &phpx.Server{
|
||||
Executor: phpx.SpawnFunc(php.spawn),
|
||||
}
|
||||
}
|
||||
|
||||
func (php *PHP) spawn(str stream.IOStream, code string) error {
|
||||
_, err := php.Barrel.Shell(str, "-c", shellescape.QuoteCommand([]string{"drush", "php:eval", code}))
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue