Documentation
¶
Overview ¶
Package core holds the part of PHP's standard library that computes: strings, arrays, json, the language constructs exposed as functions, the tokenizer, the environment and the platform surface a program expects to find before it does anything interesting. It also holds the phpscript extensions that are each too small to be worth a package: SharedMemory, defer and register_shutdown_function.
The line against stdlib/compat is behavioural, not historical: compat holds the surface whose behaviour is the interpreter's (output buffering, preg_*, the date functions), core holds the surface whose behaviour is its own.
Every file registers its own area from its own init(), so an area is added, moved or dropped without touching another file. Importing the package is what wires them in, the way a program imports a database/sql driver:
import _ "github.com/titpetric/phpscript/stdlib/core"
stdlib/imports.go does that for you.
Index ¶
- func RegisterDefer(rt *runner.Runtime)
- func RegisterSharedMemory(rt *runner.Runtime)
- func RegisterShutdown(rt *runner.Runtime)
- func SharedMemoryContext(ctx context.Context, s *SharedMemory) context.Context
- type JSONDecoder
- type JSONEncoder
- type SharedMemory
- func (s *SharedMemory) Clear(_ context.Context)
- func (s *SharedMemory) Count(_ context.Context, key string) string
- func (s *SharedMemory) Delete(_ context.Context, key string) bool
- func (s *SharedMemory) Get(_ context.Context, key string) string
- func (s *SharedMemory) Has(_ context.Context, key string) bool
- func (s *SharedMemory) Incr(_ context.Context, key string) int64
- func (s *SharedMemory) Set(_ context.Context, key, value string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterDefer ¶
RegisterDefer installs defer() in the global function namespace.
func RegisterSharedMemory ¶
RegisterSharedMemory installs SharedMemory in the runtime.
func RegisterShutdown ¶
RegisterShutdown installs register_shutdown_function() in the global function namespace. Callbacks run in registration order when Runtime.Run finishes, including after exit or an execution error.
func SharedMemoryContext ¶
func SharedMemoryContext(ctx context.Context, s *SharedMemory) context.Context
SharedMemoryContext binds an SharedMemory instance into the context.
Types ¶
type JSONDecoder ¶ added in v0.5.0
type JSONDecoder struct {
// contains filtered or unexported fields
}
JSONDecoder reads JSON values from a stream, one call per value.
func NewJSONDecoder ¶ added in v0.5.0
func NewJSONDecoder(stream io.Reader) (*JSONDecoder, error)
NewJSONDecoder returns a decoder reading from $stream.
UseNumber is on, as it is in json_decode(): without it Go reads every number as a float, and the 7 in a document would come back as 7.0. With it a whole number is an int and only a fractional one is a float, which is what php answers and what a row written back out has to preserve.
func (*JSONDecoder) Decode ¶ added in v0.5.0
func (d *JSONDecoder) Decode() (any, error)
decode reads the next value from the stream and returns it, or throws at the end of the stream.
Go's Decode fills a pointer and answers an error; PHP has no out-parameter, so the value comes back instead and the error is thrown.
It goes through jsonDecodeStream, which json_decode() uses, rather than through Go's Decode into an any. Decode would build a map[string]any, and a Go map has no order: the same document would hand back its keys in a different order on every run. The two spellings of decoding therefore agree on the shape as well as the values - an object is an ordered array keyed by its field names.
The end of the stream is an error rather than a null, because a null is a value JSON can carry: `while ($d->more())` is the loop, not a test against what decode() returned.
func (*JSONDecoder) More ¶ added in v0.5.0
func (d *JSONDecoder) More() bool
more reports whether another value is waiting in the stream. It is what ends a decode loop, and it is false at the end of the stream and inside a document that has been read to its close.
type JSONEncoder ¶ added in v0.5.0
type JSONEncoder struct {
// contains filtered or unexported fields
}
JSONEncoder writes JSON values to a stream. It is Go's json.Encoder with the one difference PHP forces: the value it is given is a PHP value, so it is converted the way json_encode() converts one, and the two spellings answer the same JSON for the same array.
func NewJSONEncoder ¶ added in v0.5.0
func NewJSONEncoder(stream io.Writer) (*JSONEncoder, error)
NewJSONEncoder returns an encoder writing to $stream.
func (*JSONEncoder) Encode ¶ added in v0.5.0
func (e *JSONEncoder) Encode(value any) error
encode writes $value to the stream as JSON, followed by a newline.
The newline is Go's, not php's: an Encoder writes a stream of values, and the newline is what separates one from the next. A single value followed by a newline is still valid JSON to any reader.
func (*JSONEncoder) SetIndent ¶ added in v0.5.0
func (e *JSONEncoder) SetIndent(prefix, indent string)
set_indent makes the encoder write each value across several lines, $indent per level under $prefix; called with two empty strings it goes back to one line per value.
type SharedMemory ¶
type SharedMemory struct {
// contains filtered or unexported fields
}
SharedMemory is a thread-safe in-memory key-value and atomic counter store exposed as SharedMemory to scripts.
func NewSharedMemory ¶
func NewSharedMemory() *SharedMemory
NewSharedMemory returns a new empty SharedMemory instance.
func NewSharedMemoryBinding ¶
func NewSharedMemoryBinding(ctx context.Context) (*SharedMemory, error)
NewSharedMemoryBinding is a key-value and counter store shared across requests: `new SharedMemory` returns the store the host bound into the runtime context, or a fresh empty store when none is bound.
func (*SharedMemory) Clear ¶
func (s *SharedMemory) Clear(_ context.Context)
Clear resets all keys and counters.
func (*SharedMemory) Count ¶
func (s *SharedMemory) Count(_ context.Context, key string) string
Count returns a counter as a formatted string.
func (*SharedMemory) Delete ¶
func (s *SharedMemory) Delete(_ context.Context, key string) bool
Delete removes a key from storage.
func (*SharedMemory) Get ¶
func (s *SharedMemory) Get(_ context.Context, key string) string
Get retrieves a string value by key, or returns empty string if missing.
func (*SharedMemory) Has ¶
func (s *SharedMemory) Has(_ context.Context, key string) bool
Has checks if a key exists in data or counters.