Documentation
¶
Overview ¶
Package gophper runs the PHP interpreter (php-src compiled to WebAssembly) on wazero, with no cgo.
An Engine compiles the PHP binaries once and runs them many times. [FastCGIServer] serves PHP over FastCGI on top of it, like php-fpm.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrEngineClosed = errors.New("gophper: the engine is closed")
ErrEngineClosed is returned by a run that starts after Close.
var License string
License is gophper's own license, the MIT License. The PHP binaries carry others: see github.com/mpyw/gophper-wasm's Licenses.
Functions ¶
func HostFS ¶
HostFS returns a file system config that mounts the whole host: "/" on Unix, and each drive at /c, /d and so on on Windows. Paths inside PHP are then HostToGuest of host paths.
func HostPaths ¶
HostPaths is an Options.HostPath for HostFS: every path PHP sees is its host file, and writable.
func HostToGuest ¶
HostToGuest returns the path inside PHP for a host path, under HostFS: the same path, except on Windows, where D:\app is /d/app.
Types ¶
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine compiles the PHP binaries once and runs them many times. It is safe for concurrent use. Each run gets a fresh PHP instance.
func NewEngine ¶
func NewEngine(ctx context.Context, cfg EngineConfig) (*Engine, error)
NewEngine prepares the runtime. The PHP binaries are compiled on first use, or by Compile. wazero sees only the deadline and cancellation of ctx, here and in every run: its values, wazero's experimental settings among them, are not passed on. A Function gets the run's ctx with them.
func (*Engine) BuildID ¶
BuildID identifies the PHP binaries this Engine runs. Two builds of the same PHP release differ in it, while opcache's own system id stays the same. A cache of compiled scripts belongs under it.
func (*Engine) Close ¶
Close cancels the runs in progress and waits for them, then releases the runtime and the compilation cache. A run that starts after it returns ErrEngineClosed.
func (*Engine) Compile ¶
Compile compiles the PHP binaries now: php.wasm for RunCLI, and php-cgi.wasm for RunCGI. Otherwise each is compiled on its first run, which then takes seconds with an empty CacheDir. When ctx is done first, Compile returns its error, and the compile goes on for the next run.
type EngineConfig ¶
type EngineConfig struct {
// CacheDir stores compiled machine code between processes. Empty disables the cache.
CacheDir string
// ExtensionDir holds extensions built by gophper-wasm's scripts/build-ext.sh.
// PHP loads them with extension=<name> or dl(). Empty means none.
ExtensionDir string
// PHPBinary is a host program that runs PHP, such as a script that
// runs "gophper php". PHP_BINARY reports it, so that a script starting
// PHP again, as Composer and Laravel do, runs it. Empty leaves
// PHP_BINARY empty.
PHPBinary string
}
EngineConfig configures an Engine.
func DefaultEngineConfig ¶
func DefaultEngineConfig() EngineConfig
DefaultEngineConfig caches compiled code in a per-user directory.
type Function ¶
Function is a PHP function written in Go.
Arguments arrive as Go values: nil, bool, int64, float64, string, []any for a list (keys 0 to n-1, in order), and map[string]any for any other array. An object arrives as its properties. The result goes back the same way, and slices, arrays and maps of other types work too. An error becomes a RuntimeException in PHP, with its message.
ctx is the run's: the caller's, with its values and cancel cause, and canceled when the run ends. The script waits while the function runs.
type Options ¶
type Options struct {
// Args are passed to PHP, excluding argv[0].
Args []string
// Env holds environment variables in "KEY=VALUE" form. GOPHPER_CWD is
// left out: it is Dir's.
Env []string
// Dir is the working directory PHP starts in. It must be inside FS.
// Empty means "/". WASI has no inherited working directory.
Dir string
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
// FS is exposed to PHP. Nil means no file system access.
FS wazero.FSConfig
// HostPath maps a path PHP sees to the host file behind it. WASI lacks
// permissions, owners and locks, so the host does those on that file.
// Child processes get files and their working directory through it too.
// It reports whether PHP may change the file, and ok is false for a path
// with no host file behind it. Nil means no path has one.
HostPath func(path string) (host string, writable, ok bool)
// Processes lets PHP start host programs: proc_open, exec and the rest.
// A child runs outside FS, with the rights of the Go process. It shares
// a Stdin that is an *os.File, and reads the null device for any other.
Processes bool
// Network lets PHP use TCP, UDP and DNS, with the network of the Go
// process. Without it, sockets fail with EACCES, and lookups fail as
// a resolver that cannot be reached would. Unix sockets go through
// HostPath instead, as files do.
Network bool
// MemoryLimit caps PHP's linear memory, in bytes: its code and data as
// well as what the script allocates. A script that needs more ends with
// "Out of memory", which memory_limit cannot lift. Zero means none
// below WebAssembly's own 4 GiB.
MemoryLimit int64
// Functions are PHP functions written in Go, by name. Each becomes an
// internal function, which a script calls as any other.
Functions map[string]Function
// Signals delivers signals to PHP, as from signal.Notify. A signal the
// script handles, with pcntl_signal(), runs the handler. One it ignores
// is dropped. Any other ends the run, with exit code 128 plus the
// signal's Linux number, as its default action would.
Signals <-chan os.Signal
}
Options configures a single PHP run, for the CLI or the CGI SAPI.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
gophper
command
Command gophper runs PHP on wazero.
|
Command gophper runs PHP on wazero. |
|
gophper/internal/genlicenses
command
Command genlicenses writes modlicenses.txt for gophper licenses: the license files of every Go module linked into cmd/gophper, and Go's own.
|
Command genlicenses writes modlicenses.txt for gophper licenses: the license files of every Go module linked into cmd/gophper, and Go's own. |
|
internal
|
|
|
dylink
Package dylink links wasm side modules into a running php.wasm instance, as the dynamic loader links a .so.
|
Package dylink links wasm side modules into a running php.wasm instance, as the dynamic loader links a .so. |
|
dylink/wasmbin
Package wasmbin reads and writes the parts of the WebAssembly binary format that dynamic linking needs: a module's imports, exports and dylink.0 section, and small modules built from scratch.
|
Package wasmbin reads and writes the parts of the WebAssembly binary format that dynamic linking needs: a module's imports, exports and dylink.0 section, and small modules built from scratch. |
|
fcgi
Package fcgi implements the responder role of the FastCGI protocol.
|
Package fcgi implements the responder role of the FastCGI protocol. |
|
hostfn
Package hostfn runs PHP functions written in Go.
|
Package hostfn runs PHP functions written in Go. |
|
hostnet
Package hostnet gives PHP sockets and DNS through host functions.
|
Package hostnet gives PHP sockets and DNS through host functions. |
|
hostpath
Package hostpath maps host paths to the paths PHP sees, and back.
|
Package hostpath maps host paths to the paths PHP sees, and back. |
|
hostproc
Package hostproc runs child processes for PHP.
|
Package hostproc runs child processes for PHP. |
|
hostsig
Package hostsig gives PHP signals through host functions.
|
Package hostsig gives PHP signals through host functions. |
|
hostsys
Package hostsys answers the guest's questions about the system it runs on: users and groups, file locks and the host name.
|
Package hostsys answers the guest's questions about the system it runs on: users and groups, file locks and the host name. |
|
hostvm
Package hostvm is the host side of one PHP instance's virtual machine: its linear memory, the interrupt flags php-src checks, and the timer of max_execution_time.
|
Package hostvm is the host side of one PHP instance's virtual machine: its linear memory, the interrupt flags php-src checks, and the timer of max_execution_time. |
|
wasi
Package wasi holds what every package of host functions shares with the guest: WASI's errno values, and the PHP instance a host function serves.
|
Package wasi holds what every package of host functions shares with the guest: WASI's errno values, and the PHP instance a host function serves. |
|
Package server serves PHP: over HTTP with HTTPHandler, or behind a web server over FastCGI with FastCGIServer, like php-fpm.
|
Package server serves PHP: over HTTP with HTTPHandler, or behind a web server over FastCGI with FastCGIServer, like php-fpm. |