Documentation ¶
Overview ¶
Package phpx provides functionalities for interacting with PHP code
Index ¶
- func Marshal(data any) (string, error)
- func MarshalString(s string) string
- func UnmarshalIntermediate[I, T any](dest *T, parser func(I) (T, error), src []byte) (err error)
- type Boolean
- type Executor
- type Integer
- type Server
- func (server *Server) Call(ctx context.Context, function string, args ...any) (value any, err error)
- func (server *Server) Close() error
- func (server *Server) Eval(ctx context.Context, code string) (value any, err error)
- func (server *Server) MarshalCall(ctx context.Context, value any, function string, args ...any) error
- func (server *Server) MarshalEval(ctx context.Context, value any, code string) error
- type ServerError
- type SpawnFunc
- type String
- type Throwable
- type Timestamp
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
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 ¶
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 ¶
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 ¶
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 (*Boolean) UnmarshalJSON ¶
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 ¶
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 (*Integer) UnmarshalJSON ¶
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) 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 ¶
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 ¶
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 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 ¶
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 (*String) UnmarshalJSON ¶
type Timestamp ¶
Timestamp represents a time value in PHP, represented as an integer