phpx

package
v0.0.0-...-dae269f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 5, 2023 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Overview

Package phpx provides functionalities for interacting with PHP code

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(data any) (string, error)

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 MarshalString

func MarshalString(s string) string

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 UnmarshalIntermediate

func UnmarshalIntermediate[I, T any](dest *T, parser func(I) (T, error), src []byte) (err error)

UnmarshalIntermediate unmarshals src into dest using an intermediate value of type I.

It first unmarshals src into a new value of type I. It then calls parser to parse I into T.

Types

type Boolean

type Boolean bool

Boolean represents a boolean php value.

The value can be marshaled to and from php and will behave as a PHP would behave.

The value will always be marshaled as "true" or "false". Unmarshaling uses AsBoolean.

func AsBoolean

func AsBoolean(value any) (b Boolean, ok bool)

AsBoolean tries to cast the given value to a boolean.

It is able to handle any value that would be json.Unmarshaled from a corresponding PHP value. Value treates all values as the boolean true, except for the ones listed at doc.

func (Boolean) MarshalJSON

func (b Boolean) MarshalJSON() ([]byte, error)

func (*Boolean) UnmarshalJSON

func (b *Boolean) UnmarshalJSON(data []byte) (err error)

type Executor

type Executor interface {
	// Spawn spawns a new (independent) process executing code.
	// It should return only once the execution terminates.
	Spawn(ctx context.Context, str stream.IOStream, code string) error
}

Executor represents anything that can spawn

type Integer

type Integer int64

Integer represents a boolean integer value.

The value can be marshaled to and from php and will behave as a PHP would behave.

The value will always be marshaled as an integer directly Unmarshaling uses AsInteger.

func AsInteger

func AsInteger(value any) (i Integer, ok bool)

AsInteger tries to cast the given value to an integer.

It is able to handle any value that would be json.Unmarshaled from a corresponding PHP value. Value casting is described at doc.

func (Integer) MarshalJSON

func (i Integer) MarshalJSON() ([]byte, error)

func (*Integer) UnmarshalJSON

func (i *Integer) UnmarshalJSON(data []byte) (err error)

type Server

type Server struct {
	// Context to use for the server
	Context context.Context

	// Executor is the executor used by this server.
	// It may not be modified concurrently with other processes.
	Executor Executor
	// contains filtered or unexported fields
}

Server represents a server that executes PHP code. A typical use-case is to define functions using [MarshalEval], and then call those functions [MarshalCall].

A server, once used, should be closed using the [Close] method.

func (*Server) Call

func (server *Server) Call(ctx context.Context, function string, args ...any) (value any, err error)

Call is like [MarshalCall] but returns the return value of the function as an any

func (*Server) Close

func (server *Server) Close() error

Close closes this server and prevents any further code from being run.

func (*Server) Eval

func (server *Server) Eval(ctx context.Context, code string) (value any, err error)

Eval is like [MarshalEval], but returns the value as an any

func (*Server) MarshalCall

func (server *Server) MarshalCall(ctx context.Context, value any, function string, args ...any) error

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) MarshalEval

func (server *Server) MarshalEval(ctx context.Context, value any, code string) error

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.

type ServerError

type ServerError struct {
	Message string
	Err     error
}

PHPError represents an error during PHPServer logic

func (ServerError) Error

func (err ServerError) Error() string

func (ServerError) Unwrap

func (err ServerError) Unwrap() error

Unwrap returns the underlying error

type SpawnFunc

type SpawnFunc func(ctx context.Context, str stream.IOStream, code string) error

SpawnFunc implements Executor

func (SpawnFunc) Spawn

func (sf SpawnFunc) Spawn(ctx context.Context, str stream.IOStream, code string) error

type String

type String string

String represents a string php value.

The value can be marshaled to and from php and will behave as a PHP would behave.

The value will always be marshaled as a literal string. Unmarshaling uses AsString.

func AsString

func AsString(value any) (s String, ok bool)

AsString tries to cast the given value to a string.

It is able to handle any value that would be json.Unmarshaled from a corresponding PHP value. Value casting is described at doc.

func (String) MarshalJSON

func (s String) MarshalJSON() ([]byte, error)

func (*String) UnmarshalJSON

func (s *String) UnmarshalJSON(data []byte) (err error)

type Throwable

type Throwable string

Throwable represents an error during php code

func (Throwable) Error

func (throwable Throwable) Error() string

type Timestamp

type Timestamp time.Time

Timestamp represents a time value in PHP, represented as an integer

func (Timestamp) MarshalJSON

func (ts Timestamp) MarshalJSON() ([]byte, error)

func (Timestamp) Time

func (ts Timestamp) Time() time.Time

func (*Timestamp) UnmarshalJSON

func (ts *Timestamp) UnmarshalJSON(data []byte) (err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL