Skip to content

Internals Runtime RuntimeExecutor

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

runtime\RuntimeExecutor

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/runtime/RuntimeExecutor.php · Snapshot 8411f4287706.

Runtime state, package API classes, loader, executor and orchestrator have separate responsibilities. Dependency validation precedes package code and nested execution restores context.

Imports and dependencies

use cli\CLIHandler;
use context\Context;
use context\events\RespondContextEvent;
use object\ObjectHandler;
use runtime\enums\ERuntimeFlags;
use runtime\interface\api\IRuntimeAPI;
use runtime\interface\IRuntime;
use runtime\interface\IRuntimeExecutor;
use vfs\resource\event\ContextSwitchEvent;

RuntimeExecutor

Declaration and architectural role

final class RuntimeExecutor implements IRuntimeExecutor

Source description (behavioral corrections are recorded in Known discrepancies):

Runs package APIs through one lifecycle and one request-wide registry.

Constants, enum cases, traits and properties

Declarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.

Member Declaration Purpose or source contract
$sharedObjects public array $sharedObjects = []; Used by inject(), capture(), executeMain().
$passableInstances public array $passableInstances = []; Used by inject(), executeMain().
$routers public array $routers = []; Used by executeMain().
$CLIHandler public ?CLIHandler $CLIHandler = null; Used by inject().
$eventDispatcher public ?\event\EventDispatcher $eventDispatcher = null; Used by activateContext(), inject(), waitForDependencies(), capture(), executeMain().
$runtime private IRuntime $runtime; Used by setRuntime(), initRuntime(), setupEnvironment(), setup(), execute().
$api private IRuntimeAPI $api; Used by initRuntime(), setupEnvironment(), setup(), execute().
$linkStack private array $linkStack = []; Used by executeMain().
$dependencyRunner private ?\Closure $dependencyRunner = null; Used by setDependencyRunner(), waitForDependencies().
Constructor-promoted public function __construct(private readonly \runtime\interface\IRuntimeFlagHandler $flagHandler = new RuntimeFlagHandler()) Initialized from the constructor argument; default expression is evaluated when the argument is omitted.

Method index

Method Signature
__construct public function __construct(private readonly \runtime\interface\IRuntimeFlagHandler $flagHandler = new RuntimeFlagHandler())
setDependencyRunner public function setDependencyRunner(callable $runner): void
setRuntime public function setRuntime(IRuntime $runtime): void
initRuntime public function initRuntime(): void
setupEnvironment public function setupEnvironment(): void
setup public function setup(): void
execute public function execute(): void
run public function run(IRuntime $runtime): void
loadAPI private function loadAPI(string $file): IRuntimeAPI
activateContext private function activateContext(IRuntime $runtime): void
initialize private function initialize(IRuntimeAPI $api, IRuntime $runtime): void
inject private function inject(IRuntimeAPI $api, IRuntime $runtime): void
waitForDependencies private function waitForDependencies(IRuntimeAPI $api, IRuntime $runtime): void
prepareAPI public function prepareAPI(IRuntimeAPI $api, IRuntime $runtime): void
capture private function capture(IRuntimeAPI $api, IRuntime $runtime): void
runComponent private function runComponent(IRuntimeAPI $api, IRuntime $runtime): void
executeAPI public function executeAPI(IRuntimeAPI $api, IRuntime $runtime): void
executeMain private function executeMain(IRuntimeAPI $api, IRuntime $runtime): void

__construct

Source line 27

public function __construct(private readonly \runtime\interface\IRuntimeFlagHandler $flagHandler = new RuntimeFlagHandler());

Parameters

Parameter Type, modifiers and default Purpose
$flagHandler private readonly \runtime\interface\IRuntimeFlagHandler $flagHandler = new RuntimeFlagHandler() Operation input; the exact use, validation and propagation are shown in the implementation trace.

Concrete no-op/default hook: this method performs no work. Override it where the lifecycle or extension contract calls for behavior.

setDependencyRunner

Source line 28

public function setDependencyRunner(callable $runner): void

Parameters

Parameter Type, modifiers and default Purpose
$runner callable $runner Operation input; the exact use, validation and propagation are shown in the implementation trace.

State writes: $this->dependencyRunner.

Implementation, validation guards and interaction order
public function setDependencyRunner(callable $runner): void { $this->dependencyRunner = \Closure::fromCallable($runner); }

setRuntime

Source line 29

public function setRuntime(IRuntime $runtime): void

Parameters

Parameter Type, modifiers and default Purpose
$runtime IRuntime $runtime Runtime state carrying package metadata, flags and request registries.

State writes: $this->runtime.

Implementation, validation guards and interaction order
public function setRuntime(IRuntime $runtime): void { $this->runtime = $runtime; }

initRuntime

Source line 30

public function initRuntime(): void

State writes: $this->api.

Implementation, validation guards and interaction order
public function initRuntime(): void
    {
        $this->activateContext($this->runtime);
        $this->api = $this->loadAPI($this->runtime->getRootPath() . 'main.php');
        $this->initialize($this->api, $this->runtime);
    }

setupEnvironment

Source line 36

public function setupEnvironment(): void

Delegates to capture(), waitForDependencies().

Implementation, validation guards and interaction order
public function setupEnvironment(): void
    {
        $this->api->environmentSetup();
        $this->capture($this->api, $this->runtime);
        $this->waitForDependencies($this->api, $this->runtime);
    }

setup

Source line 42

public function setup(): void

Delegates to inject(), capture(), waitForDependencies().

Implementation, validation guards and interaction order
public function setup(): void
    {
        $this->inject($this->api, $this->runtime);
        $this->api->setup();
        $this->capture($this->api, $this->runtime);
        $this->waitForDependencies($this->api, $this->runtime);
    }

execute

Source line 49

public function execute(): void

Delegates to executeMain().

Implementation, validation guards and interaction order
public function execute(): void { $this->executeMain($this->api, $this->runtime); }

run

Source line 51

public function run(IRuntime $runtime): void

Parameters

Parameter Type, modifiers and default Purpose
$runtime IRuntime $runtime Runtime state carrying package metadata, flags and request registries.

Delegates to activateContext(), loadAPI(), runComponent().

Implementation, validation guards and interaction order
public function run(IRuntime $runtime): void
    {
        // Local variables survive nested dependency executions on this executor.
        $this->activateContext($runtime);
        $api = $this->loadAPI($runtime->getRootPath() . 'main.php');
        $this->runComponent($api, $runtime);
    }

loadAPI

Source line 59

private function loadAPI(string $file): IRuntimeAPI

Parameters

Parameter Type, modifiers and default Purpose
$file string $file Uploaded file array, file object or path according to the declared contract.

Return expressions (including local callbacks): $api.

Explicit exceptions: \runtime\exceptions\RuntimeException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
private function loadAPI(string $file): IRuntimeAPI
    {
        $api = ObjectHandler::createObjectFromFile($file);
        if (!$api instanceof IRuntimeAPI) {
            throw new \runtime\exceptions\RuntimeException("Runtime file must declare a package RuntimeAPI: {$file}");
        }
        return $api;
    }

activateContext

Source line 68

private function activateContext(IRuntime $runtime): void

Parameters

Parameter Type, modifiers and default Purpose
$runtime IRuntime $runtime Runtime state carrying package metadata, flags and request registries.

State writes: $this->eventDispatcher.

Implementation, validation guards and interaction order
private function activateContext(IRuntime $runtime): void
    {
        $this->eventDispatcher ??= $runtime->getEventDispatcher();
        $runtime->setEventDispatcher($this->eventDispatcher);
        $dispatcher = $runtime->getEventDispatcher();
        $dispatcher->removeListeners('context.get');
        $dispatcher->addListener('context.get', new RespondContextEvent(
            new Context($runtime->getRootPath(), $runtime->getRuntimeName(), (string) $runtime->getRuntimeId())
        ));
    }

initialize

Source line 79

private function initialize(IRuntimeAPI $api, IRuntime $runtime): void

Parameters

Parameter Type, modifiers and default Purpose
$api IRuntimeAPI $api Operation input; the exact use, validation and propagation are shown in the implementation trace.
$runtime IRuntime $runtime Runtime state carrying package metadata, flags and request registries.

Delegates to activateContext(), inject().

Implementation, validation guards and interaction order
private function initialize(IRuntimeAPI $api, IRuntime $runtime): void
    {
        $this->activateContext($runtime);
        $api->setInfo($runtime);
        // Each component owns its flags; they must not leak from its linker.
        $runtime->setFlags([]);
        $runtime->setWaitFor([]);
        $this->inject($api, $runtime);
    }

inject

Source line 89

private function inject(IRuntimeAPI $api, IRuntime $runtime): void

Parameters

Parameter Type, modifiers and default Purpose
$api IRuntimeAPI $api Operation input; the exact use, validation and propagation are shown in the implementation trace.
$runtime IRuntime $runtime Runtime state carrying package metadata, flags and request registries.

Computes or delegates the operation shown below; no explicit receiver property assignment or throw occurs in this method.

Implementation, validation guards and interaction order
private function inject(IRuntimeAPI $api, IRuntime $runtime): void
    {
        if ($this->eventDispatcher !== null) $runtime->setEventDispatcher($this->eventDispatcher);
        $runtime->setSharedRegistry($this->sharedObjects);
        $runtime->setPassable($this->passableInstances);
        if (property_exists($api, 'eventDispatcher')) {
            $api->eventDispatcher = $runtime->getEventDispatcher();
        }
        if ($this->CLIHandler !== null && property_exists($api, 'CLIHandler')) {
            $api->CLIHandler = $this->CLIHandler;
        }
    }

waitForDependencies

Source line 102

private function waitForDependencies(IRuntimeAPI $api, IRuntime $runtime): void

Parameters

Parameter Type, modifiers and default Purpose
$api IRuntimeAPI $api Operation input; the exact use, validation and propagation are shown in the implementation trace.
$runtime IRuntime $runtime Runtime state carrying package metadata, flags and request registries.

Explicit exceptions: \runtime\exceptions\RuntimeException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
private function waitForDependencies(IRuntimeAPI $api, IRuntime $runtime): void
    {
        $waits = $runtime->getWaitFor();
        foreach (array_unique($waits) as $name) {
            if ($this->dependencyRunner === null) {
                throw new \runtime\exceptions\RuntimeException("No dependency scheduler available for {$name}.");
            }
            ($this->dependencyRunner)($name);
        }
        // A dependency may have changed the context and request registries.
        if ($this->eventDispatcher !== null) $runtime->setEventDispatcher($this->eventDispatcher);
        $dispatcher = $runtime->getEventDispatcher();
        $dispatcher->removeListeners('context.get');
        $dispatcher->addListener('context.get', new RespondContextEvent(
            new Context($runtime->getRootPath(), $runtime->getRuntimeName(), (string) $runtime->getRuntimeId())
        ));
        $this->inject($api, $runtime);
    }

prepareAPI

Source line 121

public function prepareAPI(IRuntimeAPI $api, IRuntime $runtime): void

Parameters

Parameter Type, modifiers and default Purpose
$api IRuntimeAPI $api Operation input; the exact use, validation and propagation are shown in the implementation trace.
$runtime IRuntime $runtime Runtime state carrying package metadata, flags and request registries.

Delegates to initialize(), capture(), waitForDependencies().

Implementation, validation guards and interaction order
public function prepareAPI(IRuntimeAPI $api, IRuntime $runtime): void
    {
        $this->initialize($api, $runtime);
        $api->environmentSetup();
        $this->capture($api, $runtime);
        $this->waitForDependencies($api, $runtime);
        $api->setup();
        $this->capture($api, $runtime);
        $this->waitForDependencies($api, $runtime);
    }

capture

Source line 132

private function capture(IRuntimeAPI $api, IRuntime $runtime): void

Parameters

Parameter Type, modifiers and default Purpose
$api IRuntimeAPI $api Operation input; the exact use, validation and propagation are shown in the implementation trace.
$runtime IRuntime $runtime Runtime state carrying package metadata, flags and request registries.

State writes: $this->sharedObjects, $this->eventDispatcher.

Implementation, validation guards and interaction order
private function capture(IRuntimeAPI $api, IRuntime $runtime): void
    {
        $runtime->setFlags($this->flagHandler->normalize($runtime->getFlags()));
        $this->sharedObjects = $runtime->getSharedRegistry();
        if (isset($api->eventDispatcher)) {
            $this->eventDispatcher = $api->eventDispatcher;
            $runtime->setEventDispatcher($this->eventDispatcher);
        }
    }

runComponent

Source line 142

private function runComponent(IRuntimeAPI $api, IRuntime $runtime): void

Parameters

Parameter Type, modifiers and default Purpose
$api IRuntimeAPI $api Operation input; the exact use, validation and propagation are shown in the implementation trace.
$runtime IRuntime $runtime Runtime state carrying package metadata, flags and request registries.

Delegates to prepareAPI(), executeAPI().

Implementation, validation guards and interaction order
private function runComponent(IRuntimeAPI $api, IRuntime $runtime): void
    {
        $this->prepareAPI($api, $runtime);
        $this->executeAPI($api, $runtime);
    }

executeAPI

Source line 148

public function executeAPI(IRuntimeAPI $api, IRuntime $runtime): void

Parameters

Parameter Type, modifiers and default Purpose
$api IRuntimeAPI $api Operation input; the exact use, validation and propagation are shown in the implementation trace.
$runtime IRuntime $runtime Runtime state carrying package metadata, flags and request registries.

Delegates to executeMain().

Implementation, validation guards and interaction order
public function executeAPI(IRuntimeAPI $api, IRuntime $runtime): void
    {
        $this->executeMain($api, $runtime);
    }

executeMain

Source line 153

private function executeMain(IRuntimeAPI $api, IRuntime $runtime): void

Parameters

Parameter Type, modifiers and default Purpose
$api IRuntimeAPI $api Operation input; the exact use, validation and propagation are shown in the implementation trace.
$runtime IRuntime $runtime Runtime state carrying package metadata, flags and request registries.

State writes: $this->sharedObjects, $this->eventDispatcher, $this->passableInstances[$runtime->getRuntimeName()][get_class($api)], $this->routers[], $this->linkStack[$path].

Explicit exceptions: \runtime\exceptions\RuntimeException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
private function executeMain(IRuntimeAPI $api, IRuntime $runtime): void
    {
        $this->activateContext($runtime);
        $this->inject($api, $runtime);
        $switch = new ContextSwitchEvent();
        $switch->contextName = $runtime->getRuntimeName();
        $dispatcher = $runtime->getEventDispatcher();
        $dispatcher->removeListeners('vfs.context.request');
        $dispatcher->addListener('vfs.context.request', $switch);
        $api->main();
        $flags = $this->flagHandler->normalize($runtime->getFlags());
        $this->sharedObjects = $runtime->getSharedRegistry();
        if (isset($api->eventDispatcher)) {
            $this->eventDispatcher = $api->eventDispatcher;
            $runtime->setEventDispatcher($this->eventDispatcher);
            $dispatcher = $this->eventDispatcher;
        }
        if (in_array(ERuntimeFlags::CreatePassable, $flags, true)) {
            $this->passableInstances[$runtime->getRuntimeName()][get_class($api)] = $api;
        }
        if (in_array(ERuntimeFlags::Router, $flags, true)) {
            if (!method_exists($api, 'getRouters')) {
                throw new \runtime\exceptions\RuntimeException('Router runtimes must provide getRouters().');
            }
            $this->routers[] = $api;
        }
        if (in_array(ERuntimeFlags::RuntimeLinker, $flags, true)) {
            if (!$api instanceof \runtime\interface\api\IRuntimeLinkerCapabilities) {
                throw new \runtime\exceptions\RuntimeException('Linker runtimes must implement IRuntimeLinkerCapabilities.');
            }
            foreach ($api->getLinks() as $file) {
                $path = realpath($file);
                if ($path === false || !str_starts_with($path, realpath($runtime->getRootPath()) . DIRECTORY_SEPARATOR)) {
                    throw new \runtime\exceptions\RuntimeException("Linked runtime must be inside its package: {$file}");
                }
                if (isset($this->linkStack[$path])) {
                    throw new \runtime\exceptions\RuntimeException("Circular runtime link: {$file}");
                }
                $this->linkStack[$path] = true;
                try {
                    // A separate state prevents child flags/waits from overwriting its parent.
                    $child = Runtime::create($runtime->getPackage());
                    $child->rootPath = $runtime->getRootPath();
                    $child->setEventDispatcher($dispatcher);
                    $this->runComponent($this->loadAPI($path), $child);
                } finally {
                    unset($this->linkStack[$path]);
                    $this->activateContext($runtime);
                    $this->inject($api, $runtime);
                }
            }
        }
    }

Related types

Clone this wiki locally