Skip to content

Developer Guide Runtimes

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

Runtimes and runtime APIs

Developer Guide · First package · Events and objects

Internals: Runtime, RuntimeAPI, RuntimeExecutor, RuntimeOrchestrator, ERuntimeFlags.

A package runtime is startup behavior, separate from the state object runtime\Runtime. Extend one of the API classes in main.php or a linked PHP file. Files loaded by ObjectHandler must declare a concrete IRuntimeAPI implementation constructible without arguments. A class's actual name—not its filename or link label—identifies a passable instance.

API Purpose Main public/protected additions Automatic environment flags
RuntimeAPI Plain startup tasks/services/commands shared, passable, waits, metadata, local object and command helpers None
RuntimeLinkerAPI Split a package into ordered runtime files createLink(name, path), getLinks() RuntimeLinker, CreatePassable
RuntimeControllerAPI Register controllers or lazy factories protected addController(), public getController() Controller, CreatePassable
RuntimeRouterAPI Register routes addRouter(), getRouters() Router, CreatePassable, AccessOtherInstances, EventDispatcher

The first package demonstrates all three specialized APIs. Use RuntimeAPI for a service provider that needs no routes/controllers/links; use a linker for a larger application's composition root. Router runtimes can return controllers from controller runtimes through getPassable().

Initialization hooks

setInfo() is framework initialization: it supplies $id, $name, $package, $runtime, $rootPath and $runtimePath, and the dispatcher. runtimePath is the package root, including for linked components; it does not become the linked file's directory.

Override environmentSetup() to add flags and wait requests, setup() to prepare dependencies, and main() to register/execute behavior. Always call parent::environmentSetup() in a specialized API override to retain its required flags. If overriding setInfo(), call parent::setInfo($runtime) before using metadata. Other empty base hooks have no mandatory parent work, but chaining preserves future/custom parent behavior.

Complete service runtime MessageHub/main.php (its own valid manifest/install record is assumed):

<?php
namespace MessageHub;

use runtime\api\RuntimeAPI;
use runtime\enums\ERuntimeFlags;

final class Main extends RuntimeAPI
{
    public function environmentSetup(): void
    {
        parent::environmentSetup();
        $this->setRuntimeFlag(ERuntimeFlags::UseShared);
        $this->setRuntimeFlag(ERuntimeFlags::CreatePassable);
    }

    public function main(): void
    {
        $this->setShared('metrics', new \ArrayObject(['messages' => 0]));
    }
}

In a consuming runtime's environmentSetup(), $this->waitForPackage('MessageHub') ensures it runs before setup. Prefer a manifest dependency when it is a real required package/version. waitForRuntime() is an alias for waitForPackage(); it does not target a linked file. Waits use package names and reject missing/disabled/non-runtime targets and cycles.

Flag behavior

Flag Current effect or constraint
CreatePassable After main, retain the API at [packageName][get_class(api)] for retrieval.
Router After main, collect the API's getRouters() result for manager registration. Missing getRouters() fails.
RuntimeLinker After main, require IRuntimeLinkerCapabilities and run links in order. Real paths must remain inside package root; link cycles fail.
Controller Set by controller API; registration/retrieval is provided by that API. The executor does not separately enforce this flag's capability interface.
AccessOtherInstances Describes passable access, but current getPassable() is not gated by this flag.
UseShared Describes shared access, but current shared methods are not gated by it.
EventDispatcher Describes dispatcher usage; the executor already injects the dispatcher when the property exists.
WaitForPackage Describes waiting; waits are scheduled from the actual wait list, not gated by this flag.
CommandProvider Describes CLI command provisioning; addCommand() checks CLI availability rather than this flag.

Only native ERuntimeFlags enum cases are accepted; normalization removes duplicates. Each linked child receives its own flags and waits, so it must declare its own requirements. Shared/passable registries and the dispatcher remain request-wide. The orchestrator transitions NOT_STARTED → RUNNING → EXECUTED, or STOPPED on failure. IGNORED/PAUSED enum cases exist but are not part of the normal orchestrator flow. PACKAGE_MAX_LOAD_TRY is not used by this scheduler.

Do not invoke framework execution helpers such as initRuntime()/prepareAPI() from ordinary package code. They are extension/testing seams; normal package code should implement its API lifecycle and let bootstrap execute it.

Clone this wiki locally