Skip to main content

nexus-core

The core package contains all actor system abstractions, behaviors, supervision, mailboxes, lifecycle signals, and the ActorSystem entry point. It has no runtime dependency -- concurrency is delegated to whichever Runtime implementation is injected.

Composer: nexus-actors/core

View class diagram

Actor namespace

Monadial\Nexus\Core\Actor\

Class / InterfaceDescription
ActorRef<T>Interface for sending messages to an actor. Methods: tell(T), ask(callable, Duration): R, path(): ActorPath, isAlive(): bool.
ActorContext<T>Interface passed to behavior handlers. Provides self(), parent(), spawn(), stop(), child(), children(), watch(), unwatch(), scheduleOnce(), scheduleRepeatedly(), stash(), unstashAll(), log(), sender().
ActorSystemEntry point. Created via ActorSystem::create(string $name, Runtime $runtime). Spawns top-level actors under /user, manages the runtime lifecycle, and provides the dead-letter endpoint.
Behavior<T>Immutable behavior definition. Factory methods: receive(Closure), withState(mixed, Closure), setup(Closure), same(), stopped(), unhandled(), empty(). Instance method: onSignal(Closure).
BehaviorWithState<T, S>Result of a stateful behavior handler. Factory methods: next(S), same(), stopped(), withBehavior(Behavior, S).
Props<T>Actor configuration. Factory methods: fromBehavior(Behavior), fromFactory(callable), fromContainer(ContainerInterface, string), fromStatefulFactory(callable). Instance methods: withMailbox(MailboxConfig), withSupervision(object).
ActorHandler<T>Interface for class-based actors. Single method: handle(ActorContext, T): Behavior.
StatefulActorHandler<T, S>Interface for stateful class-based actors. Methods: initialState(): S, handle(ActorContext, T, S): BehaviorWithState.
AbstractActor<T>Base class implementing ActorHandler with optional lifecycle hooks: onPreStart(ActorContext), onPostStop(ActorContext).
ActorPathImmutable hierarchical path (e.g., /user/orders/order-123). Methods: root(), fromString(string), child(string), name(), parent(), equals(), isChildOf(), isDescendantOf(), depth().
ActorCell<T>Internal engine per actor. Manages behavior evaluation, state machine transitions, children, stash buffer, and supervision. Implements ActorContext<T>.
LocalActorRef<T>In-process ActorRef that delivers messages via a Mailbox.
DeadLetterRefNull-object ActorRef that captures undeliverable messages. Always returns false from isAlive().
ActorStateEnum: New, Starting, Running, Suspended, Stopping, Stopped.
BehaviorTagEnum: Receive, WithState, Setup, Same, Stopped, Unhandled, Empty.
CancellableInterface for cancelling scheduled operations. Methods: cancel(), isCancelled().

Mailbox namespace

Monadial\Nexus\Core\Mailbox\

Class / InterfaceDescription
MailboxInterface for actor mailboxes. Methods: enqueue(Envelope): EnqueueResult, dequeue(): Option<Envelope>, dequeueBlocking(Duration): Envelope, count(), isFull(), isEmpty(), close().
MailboxConfigImmutable configuration. Factory methods: bounded(int $capacity, OverflowStrategy), unbounded(). Instance methods: withCapacity(int), withStrategy(OverflowStrategy).
EnvelopeImmutable message wrapper. Properties: message, sender (ActorPath), target (ActorPath), metadata (array). Factory: of(object, ActorPath, ActorPath).
EnqueueResultEnum: Accepted, Dropped, Backpressured.
OverflowStrategyEnum: DropNewest, DropOldest, Backpressure, ThrowException.

Supervision namespace

Monadial\Nexus\Core\Supervision\

Class / InterfaceDescription
SupervisionStrategyImmutable strategy configuration. Factory methods: oneForOne(int $maxRetries, ?Duration $window, ?Closure $decider), allForOne(...), exponentialBackoff(Duration $initialBackoff, Duration $maxBackoff, int $maxRetries, float $multiplier, ?Closure $decider). Instance method: decide(Throwable): Directive.
DirectiveEnum: Restart, Stop, Resume, Escalate.
StrategyTypeEnum: OneForOne, AllForOne, ExponentialBackoff.

Lifecycle namespace

Monadial\Nexus\Core\Lifecycle\

Class / InterfaceDescription
SignalMarker interface for lifecycle signals.
PreStartSignal delivered after an actor starts.
PostStopSignal delivered before an actor stops.
PreRestartSignal delivered before an actor restarts.
PostRestartSignal delivered after an actor restarts.
ChildFailedSignal delivered to a parent when a child actor fails.
TerminatedSignal delivered when a watched actor stops.

Message namespace

Monadial\Nexus\Core\Message\

Class / InterfaceDescription
SystemMessageMarker interface for system-level messages.
PoisonPillSystem message that initiates graceful actor shutdown.
KillSystem message that forces immediate actor termination.
SuspendSystem message that transitions an actor to the suspended state.
ResumeSystem message that transitions a suspended actor back to running.
WatchSystem message that registers a watcher for death notifications.
UnwatchSystem message that removes a watcher registration.
DeadLetterWraps an undeliverable message with the original sender and intended recipient.

Exception namespace

Monadial\Nexus\Core\Exception\

ClassDescription
NexusExceptionBase exception for all Nexus errors.
ActorExceptionBase exception for actor-related errors.
ActorInitializationExceptionThrown when actor setup fails.
AskTimeoutExceptionThrown when an ask() call exceeds its timeout.
MailboxExceptionBase exception for mailbox errors.
MailboxClosedExceptionThrown when enqueuing or dequeuing from a closed mailbox.
MailboxOverflowExceptionThrown when a bounded mailbox overflows with the ThrowException strategy.
MaxRetriesExceededExceptionThrown when supervision retry limits are exceeded.
InvalidActorPathExceptionThrown for malformed actor path strings.
InvalidActorStateTransitionThrown for illegal state machine transitions.
InvalidBehaviorExceptionThrown for invalid behavior configurations.
InvalidMailboxConfigExceptionThrown for invalid mailbox configurations.
NexusLogicExceptionThrown for programming errors (extends LogicException).

Duration

Monadial\Nexus\Core\Duration

Nanosecond-precision, immutable duration value object. Implements Stringable.

Factory methods:

Duration::seconds(5);
Duration::millis(500);
Duration::micros(1000);
Duration::nanos(1_000_000);
Duration::zero();

Arithmetic:

$a = Duration::seconds(1);
$b = Duration::millis(500);

$a->plus($b); // 1s 500ms
$a->minus($b); // 500ms
$a->multipliedBy(3); // 3s
$a->dividedBy(2); // 500ms

Conversions: toNanos(), toMicros(), toMillis(), toSeconds(), toSecondsFloat().

Comparisons: equals(), isGreaterThan(), isLessThan(), isZero(), compareTo().

Pipe functions

Monadial\Nexus\Core\Actor\Functions\

Pipe-friendly functions for composing Props:

use function Monadial\Nexus\Core\Actor\Functions\withMailbox;
use function Monadial\Nexus\Core\Actor\Functions\withSupervision;

// Designed for use with PHP's pipe operator:
// $behavior |> Props::fromBehavior(...) |> withMailbox($config) |> withSupervision($strategy)

$props = Props::fromBehavior($behavior)
->withMailbox(MailboxConfig::bounded(100))
->withSupervision(SupervisionStrategy::oneForOne());
  • withMailbox(MailboxConfig): Closure(Props): Props -- Returns a closure that applies a mailbox config to Props.
  • withSupervision(SupervisionStrategy): Closure(Props): Props -- Returns a closure that applies a supervision strategy to Props.