Skip to main content

nexus-core

The core package contains actor system abstractions, behaviors, supervision, lifecycle signals, and the ActorSystem entry point. Concurrency primitives (Duration, Cancellable, mailbox contracts) are provided by nexus-runtime, and nexus-core depends on that package.

Composer: nexus-actors/core

View class diagram

RuntimeMailbox in this diagram refers to the mailbox interface provided by nexus-runtime.

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. See method reference below.
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.
ReceiveBehavior<T>Concrete Behavior for stateless message handling.
WithStateBehavior<T, S>Concrete Behavior for stateful message handling.
SetupBehavior<T>Concrete Behavior wrapping a factory closure; resolved at actor startup.
SameBehavior<T>Sentinel Behavior — keep current behavior unchanged.
StoppedBehavior<T>Sentinel Behavior — stop the actor.
UnhandledBehavior<T>Sentinel Behavior — route message to dead letters.
EmptyBehavior<T>Sentinel Behavior — no-op; discard all messages.
SupervisedBehavior<T>Wrapper Behavior that installs a custom SupervisionStrategy for its inner behavior.
WithTimersBehavior<T>Wrapper Behavior that provides a TimerScheduler to its factory closure.
WithStashBehavior<T>Wrapper Behavior that provides a StashBuffer to its factory closure.
UnstashAllBehavior<T>Internal Behavior carrying stashed envelopes to replay; produced by StashBuffer::unstashAll().

ActorContext method reference

SignatureDescription
self(): ActorRef<T>Returns the ActorRef of the current actor.
parent(): ?ActorRef<object>Returns the parent ActorRef, or null for top-level actors.
path(): ActorPathReturns the actor's hierarchical path.
spawn(Props<C> $props, string $name): ActorRef<C>Spawns a named child actor.
stop(ActorRef<object> $child): voidStops the given child actor.
child(string $name): ?ActorRef<object>Returns a child ActorRef by name, or null if not found.
children(): array<string, ActorRef<object>>Returns all live children keyed by name.
watch(ActorRef<object> $target): voidRegisters a death-watch on $target; delivers Terminated when it stops.
unwatch(ActorRef<object> $target): voidCancels a death-watch registration.
scheduleOnce(Duration $delay, object $message): CancellableSchedules a one-shot message to self() after $delay.
scheduleRepeatedly(Duration $initialDelay, Duration $interval, object $message): CancellableSchedules a recurring message to self().
sender(): ?ActorRef<object>Returns the sender of the current message, or null for fire-and-forget.
reply(object $message): voidSends a message back to the current envelope's sender, propagating correlationId.
stash(): voidStashes the current message for later replay.
unstashAll(): voidReplays all stashed messages into the mailbox.
log(): LoggerInterfaceReturns the PSR-3 logger bound to this actor.
spawnTask(Closure $task): CancellableSpawns a background task tied to this actor's lifecycle; cancelled automatically on stop.

Mailbox namespace

Monadial\Nexus\Core\Mailbox\

Class / InterfaceDescription
EnvelopeImmutable message wrapper. Properties: message, sender (ActorPath), target (ActorPath), metadata (array). Factory: of(object, ActorPath, ActorPath).

Mailbox contracts/configuration are provided under Monadial\Nexus\Runtime\Mailbox\....

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.
MaxRetriesExceededExceptionThrown when supervision retry limits are exceeded.
InvalidActorPathExceptionThrown for malformed actor path strings.
InvalidActorStateTransitionThrown for illegal state machine transitions.
InvalidBehaviorExceptionThrown for invalid behavior configurations.
NexusLogicExceptionThrown for programming errors (extends LogicException).

Core actor APIs use runtime types from nexus-runtime, including Duration, Cancellable, mailbox contracts/configuration, and mailbox exceptions under Monadial\Nexus\Runtime\....

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.