Skip to content

nexphant/core ​

Foundational runtime primitives for Nexphant applications, covering contexts, cooperative cancellation, resource tracking, and ownership hierarchies.

Install ​

bash
composer require nexphant/core

What's Included ​

  • Context - Bounded request-scoped data propagation
  • Ownership - Resource tracking and hierarchical owner registration
  • Cancellation - Cooperative task deadlines and cancellation tokens
  • Graceful Drain - Server termination and connection shutdown signals

Context ​

Use the Context primitive to store and retrieve data scoped to execution limits:

php
use Nexphant\Core\Context;

// Create new context
$ctx = Context::create();

// Set scoped context attributes
$ctx->set('user_id', 42);

// Retrieve from context
$userId = $ctx->get('user_id');

Ownership & Registry ​

The OwnerRegistry manages hierarchical RuntimeOwner scopes (requests, jobs, fibers, workers) and handles cascading termination/resource cleanup:

php
use Nexphant\Core\Ownership\OwnerRegistry;
use Nexphant\Core\Ownership\OwnerType;

$registry = OwnerRegistry::instance();

// Open a new request-scoped owner
$owner = $registry->open(OwnerType::REQUEST);

// Get current owner properties
$ownerId = $owner->id();
$type = $owner->type(); // e.g. OwnerType::REQUEST

// Close owner scope (cascade closes children and releases resources)
$registry->close($ownerId, reason: 'finished');

Available owner types in OwnerType enum:

  • OwnerType::WORKER
  • OwnerType::REQUEST
  • OwnerType::FIBER
  • OwnerType::QUEUE_JOB
  • OwnerType::SCHEDULER_TASK
  • OwnerType::CONNECTION
  • OwnerType::TIMER

Cancellation & Deadlines ​

Use CancellationToken to cooperatively check for task abort signals or timeout occurrences:

php
use Nexphant\Core\Cancellation\CancellationToken;

$token = new CancellationToken();

// Register a callback when token is cancelled
$token->onCancel(function (string $reason) {
    // clean up temporary files/resources
});

// Check cancellation status
if ($token->isCancelled()) {
    // stop execution
}

// Or throw exception if cancelled
$token->throwIfCancelled();

// Trigger cancellation
$token->cancel('timeout');

Released under the MIT License.