Plugin architecture and lifecycle
A plugin groups extensions that your application registers while preparing ExecKernel: rules, listeners, validators, loaders, or presentation changes. Its code registers what it provides, while a manifest describes those capabilities and their engine compatibility. This lets you reuse the same set of extensions across application configurations.
Your application loads the classes, constructs the plugin, and chooses which registry to pass to the engine. A plugin becomes active in instances that use that registry. To follow the full process, we will package a small extension and compare the result with and without it.
Start with a working extension
Section titled “Start with a working extension”Events and runtime listeners introduced ActivitySummaryListener. It listens to starts and choices in the learning-path graph and retains each session’s current node, payload type, and last observed operation. In that graph, choosing the guide through edge 101 moves from node 10 to terminal node 20.
We will reuse that class. The plugin will register it when the engine is built, allowing the application to enable this feature through one configuration.
The example uses the following file layout. vendor/autoload.php belongs to your existing ExecKernel installation; the other files are available from this page and the preceding guide.
activity-demo/ vendor/autoload.php learning-path.json ActivitySummaryListener.php ActivitySummaryPlugin.php activity-plugin.json plugin-lifecycle.phpImplement the plugin
Section titled “Implement the plugin”PluginInterface has two methods. Use register() to add extensions to the registry and boot() to finish initializing the plugin after its registrations have been checked. This listener needs no additional initialization, so its boot() method is empty.
<?phpdeclare(strict_types=1);
namespace App\Extensions;
use ExecKernel\Domain\Engine\PluginInterface;use ExecKernel\Domain\Engine\PluginRegistry;
final class ActivitySummaryPlugin implements PluginInterface{ public function __construct( private readonly ActivitySummaryListener $activity, ) {}
public function register(PluginRegistry $registry): void { $registry->addListener( ActivitySummaryListener::class, fn (): ActivitySummaryListener => $this->activity, ); }
public function boot(): void { // This plugin needs no additional initialization. }}addListener() accepts the class and a factory that constructs or supplies its instance. Here the factory returns the object already held by the application, allowing the application to read the summary from that same object after an operation.
The registry does not automatically resolve constructor dependencies. If an extension needs an application repository or service, supply it when constructing the extension and provide a factory like this one. Without a factory, the class must be constructible without arguments.
Describe its capabilities
Section titled “Describe its capabilities”The activity-plugin.json file contains this manifest:
{ "name": "application-activity", "version": "1.0.0", "requires": { "execkernel": "0.2.0" }, "provides": { "listeners": [ "App\\Extensions\\ActivitySummaryListener" ] }}name identifies the plugin within the registry, and version identifies the plugin’s own version. requires.execkernel expresses compatibility with the engine version configured on the registry. This example requires exactly 0.2.0.
provides.listeners lists the classes that register() will add. The registry compares declared and added capabilities. Declaring a class without registering it, or registering an undeclared capability, makes registration fail. Writing a name in the JSON file does not load or execute that class.
Register before constructing the engine
Section titled “Register before constructing the engine”Once the classes are available, the application constructs the listener, registry, and plugin instance:
use App\Extensions\ActivitySummaryListener;use App\Extensions\ActivitySummaryPlugin;use ExecKernel\Domain\Engine\PluginRegistry;use ExecKernel\Domain\Versioning\CompatibilityMode;use ExecKernel\Infrastructure\Engine\ExecKernelFactory;
$activity = new ActivitySummaryListener('learning-path');$registry = new PluginRegistry( engineVersion: '0.2.0', compatibilityMode: CompatibilityMode::STRICT,);
$registry->registerPluginFromManifest( __DIR__ . '/activity-plugin.json', new ActivitySummaryPlugin($activity),);
$engine = (new ExecKernelFactory())->build(pluginRegistry: $registry);registerPluginFromManifest() reads the file, checks its structure and compatibility, calls register(), verifies the capabilities, and calls boot(). The plugin is registered only if all of these steps succeed.
The example explicitly sets engineVersion: '0.2.0' and CompatibilityMode::STRICT on PluginRegistry. The application supplies this version; the registry does not discover it from Composer. In this mode, an incompatible manifest prevents loading. This plugin compatibility check is independent of the policy used to load and version graphs.
When ExecKernelFactory receives the registry, the engine obtains the extensions it needs and connects listeners to its dispatcher. Consuming the registry seals it: further registrations are rejected. Register every plugin before building the engine or using a loader that consumes that registry.
boot() runs during plugin registration, not whenever a session starts. It is also not a second place to call addListener(): the registry blocks further additions during that phase.
Compare the same route
Section titled “Compare the same route”The complete example constructs two engines with separate registries. It leaves one registry empty and registers ActivitySummaryPlugin in the other. Both engines start learning-path and choose edge 101.
| Result after choosing | Without the plugin | With the plugin |
|---|---|---|
| Current node | 20 |
20 |
| Payload type | terminal |
terminal |
| Activity summary | null |
Node 20, type terminal, operation choose. |
The added effect is the summary. Navigation still uses the same graph. In this comparison, the listener object exists in both cases, but only the engine using the plugin delivers events to it.
Download ActivitySummaryPlugin.php, activity-plugin.json, and plugin-lifecycle.php. Add ActivitySummaryListener.php and learning-path.json to the same directory. The script loads the classes explicitly; your application can use its usual autoload configuration instead.
Resolve loading failures before running sessions
Section titled “Resolve loading failures before running sessions”Plugin names and registered capabilities must be unique within the registry. A duplicate, a class that does not implement the required contract, or a mismatch with provides prevents loading from completing. If register() or boot() fails, the registry restores its previous capabilities, factories, and manifests.
That restoration applies to the registry. If plugin code has already written to a database or called a service, those actions are not undone. Keep extension preparation focused on constructing and connecting dependencies, and handle external operations in the appropriate application flow.
The manifest supports engine version requirements; it does not resolve dependencies between plugins or install PHP packages. Your application organizes code loading and registration order. Once a configuration is sealed, create a new registry for a different configuration.
Choose what to extend
Section titled “Choose what to extend”Packaging is shared, but each extension has a different responsibility:
| What your application needs | Extension point |
|---|---|
| Its own decision for a condition | Rule. |
| Additional checks on a graph | Validator. |
| Graphs from an application source | Loader. |
| A reaction to published events | Listener. |
| Additional presentation data in a payload | Output hook. |
| HTTP notifications through a custom implementation | Webhook dispatcher. |
| Recognition and validation of another terminal output type | Terminal output type. |
Chart Lab demonstrates another use of this mechanism. ChartPlugin registers ChartPayloadHook, which adds chart data to a payload, and the demo interface draws the charts. It complements this guide with a presentation extension: the engine supplies data, and the frontend uses its own renderer.
The plugin manifest and extension reference covers the accepted fields, all seven capabilities, and their signatures.