-
Notifications
You must be signed in to change notification settings - Fork 0
Cross Module API
Ray Fung edited this page Feb 16, 2026
·
4 revisions
The Cross-Module API allows modules to expose and consume typed commands through a provider/consumer handshake. This enables service-style communication between independent modules without tight coupling.
The flow is:
-
Provider registers API commands via
Agent::addAPICommand()in__onReady(). -
Consumer requests an API handle via
Agent::getAPI()in__onReady(). - Framework performs a handshake: calls the provider's
__onAPICall()with the consumer's module code. - If the provider grants access, the consumer receives an
APIobject and can invoke commands.
// In controller __onReady()
public function __onReady(Agent $agent): bool
{
// Register commands with the API
$agent->addAPICommand('getVersion', function() {
return '1.0.0';
});
$agent->addAPICommand('processData', function($data) {
// Process and return result
return strtoupper($data);
});
return true;
}// Provider's controller
public function __onAPICall(Agent $agent, string $moduleCode): bool
{
// Grant access to specific modules
return in_array($moduleCode, [
'demo/consumer_module',
'demo/another_module',
]);
}
⚠️ Warning: Security: Always validate the caller's module code in__onAPICall(). Returningtrueunconditionally allows any module to call your API.
// Consumer's controller __onReady()
public function __onReady(Agent $agent): bool
{
$api = $agent->getAPI('demo/provider_module');
if ($api) {
// Invoke the provider's command
$version = $api->exec('getVersion');
$result = $api->exec('processData', 'hello');
}
return true;
}| Method | Return | Description |
|---|---|---|
exec(string $command, ...$args) |
mixed |
Execute a registered API command |
has(string $command) |
bool |
Check if a command exists |
getModuleCode() |
string |
Get the provider's module code |
| Method | Context | Description |
|---|---|---|
addAPICommand(string $name, callable $callback) |
Provider | Register a callable command in your API |
getAPI(string $moduleCode) |
Consumer | Request API handle from another module |
- Register API commands in
__onReady(), not__onInit(), as all modules must be initialized first. - Use
__onAPICall()to whitelist consumers by module code. - Always check the return value of
getAPI()— it returnsnullif the handshake is denied. - Keep API commands focused and well-named to serve as a stable contract between modules.