Bundle
in package
Class Bundle
A flexible, static container for storing arbitrary named values at runtime. Each "thing" is stored under a unique key, and can be anything: objects, arrays, config fragments, closures, strings, or even other bundles.
This class exists as a lightweight alternative to Cargo — intentionally simple, with no magic getters, expiration, encryption, or autoload dependencies.
Why Bundle? Because sometimes you just need to carry around a few named things without the overhead. Think of it as your stash bag for runtime state, tagged with whatever keys make sense.
Examples of use:
- Centralized config fragments
- Shared service references
- Arbitrary runtime flags
- Debug/testing hooks
Table of Contents
Properties
- $things : array<string, mixed>
- This holds random stuff so you’ll figure it out — they’re just… things.
Methods
- add() : mixed
- Add any value to the bundle.
- addOnce() : void
- Add a value only if it hasn't already been added.
- all() : array<string, mixed>
- Get all stored values as a key/value array.
- allKeys() : array<string|int, string>
- List all keys currently in the bundle.
- as() : mixed
- Get a stored value and assert it is an instance of the given class.
- clear() : void
- Clear the entire bundle.
- count() : int
- Get the number of items currently stored.
- debugSummary() : string
- Return an HTML-rendered diagnostic table of the bundle contents, with expandable sections for each value.
- drop() : mixed
- Remove a value from the bundle.
- dropBy() : void
- Drop items from the bundle that match a condition.
- dropIfKeyContains() : void
- Drop items whose tag contains a given substring.
- dropIfKeyEndsWith() : void
- Drop items whose tag ends with the given substring.
- dropIfKeyStartsWith() : void
- Drop items whose tag starts with the given substring.
- each() : void
- Apply a callback to each item in the bundle.
- exists() : bool
- Check if a key exists and is not null.
- filter() : void
- Filter the bundle in-place using a callback.
- findKeysByType() : array<string|int, string>
- Find all keys where the value matches the given type or class.
- get() : mixed|null
- Retrieve a value from the bundle by key.
- has() : bool
- Check if a tag exists in the bundle.
- import() : void
- Bulk import multiple values into the bundle.
- keepIfKeyContains() : void
- Keep only items whose key contains the given substring.
- keepIfKeyEndsWith() : void
- Keep only items whose key ends with the given substring.
- keepIfKeyStartsWith() : void
- Keep only items whose tag starts with the given substring.
- removeNulls() : void
- Drop any keys where the value is null.
- rename() : void
- Rename a stored key without changing the value.
- require() : mixed
- Get a value or throw if it’s not there.
- summary() : array<string, string>
- Return a summary of what's in the bundle: key => type/class.
- things() : array<string|int, mixed>
- typeOf() : string|null
- Get the type (or class) of the stored value at a key.
Properties
$things
This holds random stuff so you’ll figure it out — they’re just… things.
protected
static array<string, mixed>
$things
= []
Internal store of tagged values
Methods
add()
Add any value to the bundle.
public
static add(string $key, mixed $value) : mixed
Parameters
- $key : string
- $value : mixed
addOnce()
Add a value only if it hasn't already been added.
public
static addOnce(string $key, mixed $value) : void
Parameters
- $key : string
- $value : mixed
Tags
all()
Get all stored values as a key/value array.
public
static all() : array<string, mixed>
Return values
array<string, mixed>allKeys()
List all keys currently in the bundle.
public
static allKeys() : array<string|int, string>
Return values
array<string|int, string>as()
Get a stored value and assert it is an instance of the given class.
public
static as(string $className, string $key) : mixed
Parameters
- $className : string
-
Fully qualified class name to assert
- $key : string
-
Key of the thing in the bundle
Tags
clear()
Clear the entire bundle.
public
static clear() : void
count()
Get the number of items currently stored.
public
static count() : int
Return values
intdebugSummary()
Return an HTML-rendered diagnostic table of the bundle contents, with expandable sections for each value.
public
static debugSummary() : string
Return values
stringdrop()
Remove a value from the bundle.
public
static drop(string $key) : mixed
Parameters
- $key : string
dropBy()
Drop items from the bundle that match a condition.
public
static dropBy(callable $callback) : void
Parameters
- $callback : callable
-
Receives ($key, $value) and returns bool
dropIfKeyContains()
Drop items whose tag contains a given substring.
public
static dropIfKeyContains(string $match) : void
Parameters
- $match : string
dropIfKeyEndsWith()
Drop items whose tag ends with the given substring.
public
static dropIfKeyEndsWith(string $suffix) : void
Parameters
- $suffix : string
dropIfKeyStartsWith()
Drop items whose tag starts with the given substring.
public
static dropIfKeyStartsWith(string $prefix) : void
Parameters
- $prefix : string
each()
Apply a callback to each item in the bundle.
public
static each(callable $callback) : void
Parameters
- $callback : callable
-
Receives ($key, $value)
exists()
Check if a key exists and is not null.
public
static exists(string $key) : bool
Parameters
- $key : string
Return values
boolfilter()
Filter the bundle in-place using a callback.
public
static filter(callable $callback) : void
Parameters
- $callback : callable
-
Receives ($key, $value) and returns bool
findKeysByType()
Find all keys where the value matches the given type or class.
public
static findKeysByType(string $type) : array<string|int, string>
Parameters
- $type : string
Return values
array<string|int, string>get()
Retrieve a value from the bundle by key.
public
static get(string $key) : mixed|null
Parameters
- $key : string
Return values
mixed|nullhas()
Check if a tag exists in the bundle.
public
static has(string $key) : bool
Parameters
- $key : string
Return values
boolimport()
Bulk import multiple values into the bundle.
public
static import(array<string, mixed> $things) : void
Existing keys will be overwritten with the new values.
Parameters
- $things : array<string, mixed>
-
Keyed values to store
keepIfKeyContains()
Keep only items whose key contains the given substring.
public
static keepIfKeyContains(string $match) : void
Parameters
- $match : string
keepIfKeyEndsWith()
Keep only items whose key ends with the given substring.
public
static keepIfKeyEndsWith(string $suffix) : void
Parameters
- $suffix : string
keepIfKeyStartsWith()
Keep only items whose tag starts with the given substring.
public
static keepIfKeyStartsWith(string $prefix) : void
Parameters
- $prefix : string
removeNulls()
Drop any keys where the value is null.
public
static removeNulls() : void
rename()
Rename a stored key without changing the value.
public
static rename(string $from, string $to) : void
Parameters
- $from : string
- $to : string
require()
Get a value or throw if it’s not there.
public
static require(string $key) : mixed
Parameters
- $key : string
Tags
summary()
Return a summary of what's in the bundle: key => type/class.
public
static summary() : array<string, string>
Useful for debugging, logging, or just reminding yourself what kind of chaos you've stored in here.
Return values
array<string, string> —An associative array of key => type/class name
things()
public
static things() : array<string|int, mixed>
Return values
array<string|int, mixed>typeOf()
Get the type (or class) of the stored value at a key.
public
static typeOf(string $key) : string|null
Parameters
- $key : string