Skip to main content

Crate speedyspec_php

Crate speedyspec_php 

Source
Expand description

§speedyspec-php

crates.io docs.rs MIT

Embedded, statically linked PHP interpreter for speedyspec.

Part of the SpeedySpec platform. Role: platform. Seam: container.

§What it gives you

  • Php::boot: starts the embedded interpreter once per process, with the hook registry and native areas in BootOptions.
  • Php::eval, Php::call, Php::require and Php::serve: run code, call callables, load files and serve a WebRequest.
  • NativeArea: a set of native functions and classes, with request start and end hooks.
  • DOCS_RS: build.rs links nothing when it is set, so docs.rs builds the docs without a PHP.

§Install

cargo add speedyspec-php

§Requirements

A static PHP built by scripts/build-php.sh at speedyspec-php’s pinned php-version, with SPEEDYSPEC_PHP_PREFIX, PHP, PHP_CONFIG and SODIUM_LIB_DIR set. Rust 1.99.

§Features

  • embed (on by default) links the embedded PHP. It is required to run PHP; without it, Php::boot returns an error. It is off only for docs.rs builds, whose ext-php-rs bindings cannot build it.

§Example

Boot PHP and evaluate an expression.

use std::rc::Rc;

use speedyspec_hooks::Value;
use speedyspec_hooks_memory::MemoryHooks;
use speedyspec_php::{BootOptions, Php};

let php = Php::boot(BootOptions { hooks: Rc::new(MemoryHooks::new()), preload: &[], areas: vec![] })
    .expect("PHP boots");
assert_eq!(php.eval("example", "return 6 * 7;").unwrap(), Value::Int(42));

§Where it fits

Every crate that runs PHP builds on this one: speedyspec-php-ext for the extension’s shared parts, speedyspec-container-php and speedyspec-wp-hooks-php for their areas, and the runtime itself. The pinned PHP version ships in the package as php-version, and a consumer’s build-php.sh reads it from the package it resolved.

§Versioning

SemVer, 0.x: a minor version may break the API. MSRV is Rust 1.99. SpeedySpec’s own version (ADR-028) is the PHP runtime’s major, not this crate’s.

§License

MIT

§In detail

Embedded, statically linked PHP for speedyspec.

Php::boot starts the interpreter once per process. PHP is process-global and non-thread-safe, so a Php stays on the thread that booted it. The hook registry passed in BootOptions is exposed to PHP as SpeedySpec\Hooks\Hook and the WordPress-named functions.

Re-exports§

pub use ext_php_rs;

Modules§

native
Native functions written as plain Rust closures over Values.

Structs§

BootOptions
What Php::boot needs.
CallFailure
A PHP call that failed: the error, and the thrown object when PHP threw one (a Value::Opaque), so a native function can rethrow the original exception.
Callable
A PHP callable held by Rust, called through Zend’s fast path. See the module docs.
Php
A booted PHP engine with one request open.
Served
What serving one web request came to.

Enums§

Output
Where PHP’s output (echo, error display, …) is written.
PhpError
Why PHP could not boot or run something.

Constants§

PHP_VERSION
The PHP version linked into this binary: the one pinned in this crate’s php-version file, which the build refuses to link anything else than.

Traits§

NativeArea
One area of the extension.
WebRequest
One HTTP request, as PHP sees it.

Functions§

call
Calls a PHP callable from anywhere on the thread that booted PHP while a request is open, as Php::call does (an adapter of a port holds no Php handle). Fails without touching PHP when no request is open.
call_catching
As call, keeping the exception object a failed call threw.
eval
Runs PHP statements (no <?php tag) from anywhere on the thread that booted PHP while a request is open, as Php::eval does (a native area holds no Php handle). Fails without touching PHP when no request is open.
execution_context
Identifies the execution context PHP is running in now: the main context or one fiber (the address of its zend_fiber_context, EG(current_fiber_context)), 0 when PHP is not running. Within one context calls nest strictly, also while other contexts are suspended mid-call, so per-context bookkeeping that is saved and restored around a call stays correct when a fiber suspends inside it. An address can be reused by a later fiber once its context is destroyed.
global
$GLOBALS[$name], through references; None when it is not set or no request is open. Runs no PHP code.
global_array_contains
array_key_exists( $key, $GLOBALS[$name] ).
global_array_get
$GLOBALS[$name][$key], converted to a Value; None when absent. Only that entry is converted.
global_array_keys
The keys of the array in $GLOBALS[$name], in PHP’s order, as bytes (integer keys in decimal); None when the global is unset or not an array.
global_pop
array_pop($GLOBALS[$name]), in place, as WordPress pops $wp_current_filter: one entry into PHP. False (nothing done) when the global is not an array.
global_push
$GLOBALS[$name][] = $value, in place, as WordPress pushes $wp_current_filter: one entry into PHP. False (nothing done) when the global is not an array.
object_id
spl_object_id($value) for a PHP object held as a Value::Opaque.
set_global
$GLOBALS[$name] = $value: a variable referencing the global sees the new value too.
set_ini
INI settings for every request of this process, as php.ini would give them (so PHP_INI_PERDIR and PHP_INI_SYSTEM settings such as upload_max_filesize apply, which ini_set() cannot change). Call before Php::boot, once. A key or value with a quote, a newline or a NUL is refused.