Skip to content

Local PHP Quick Start

The local PHP client runs ExecKernel inside your application. You give it a way to load graphs and a place to keep sessions; it provides operations for starting a session, making a choice and retrieving the result. In this tutorial, you will learn to connect those components and use the results in your application.

We will reuse the example graph from Your first graph, which represents a choice between collecting an order from a store and having it delivered. Delivery depends on the context value delivery_available. To see how the engine responds when an option is blocked, we will start a session with that value set to false, prepare the choices for an interface and select collection.

You need the PHP package installed with Composer and the complete graph file saved as graphs/delivery-options.json in your project. The PHP example will live in the project root, alongside vendor/. Everything runs in this PHP process; no API service is required.

There are three things to supply before the client can execute a graph: the engine, a graph loader and a session store.

use ExecKernel\Infrastructure\Engine\ExecKernelFactory;
use ExecKernel\Infrastructure\Loader\LocalGraphLoader;
use ExecKernel\Infrastructure\SessionStore\InMemorySessionStore;
$engine = (new ExecKernelFactory())->build();
$loader = new LocalGraphLoader(basePath: __DIR__ . '/graphs');
$store = new InMemorySessionStore();

ExecKernelFactory builds the engine with its standard components and built-in rules. LocalGraphLoader resolves graph names against the directory we supply, so asking for delivery-options loads graphs/delivery-options.json. The loader also validates the definition.

The store keeps each session’s progress separately from the graph. We use InMemorySessionStore so the example can run without configuring storage. Its data lasts only as long as this store instance; we will return to that distinction when considering an application with separate requests.

Now connect these components through the local adapter:

use ExecKernel\Client\ExecKernelLocalAdapter;
use ExecKernel\Client\ExecKernelLocalClient;
$client = new ExecKernelLocalClient(
new ExecKernelLocalAdapter($engine, $loader, $store)
);

The adapter loads the graph, invokes the engine and saves the session as part of the client’s operations. The client presents the result as PHP objects, so the application can work with the current position and its payload without handling the engine’s internal result directly.

These fragments belong to one program. A complete copyable version, including Composer’s autoloader, appears below.

Call start() with the graph name, the context for this execution and the presentation language:

$state = $client->start(
'delivery-options',
['delivery_available' => false],
'en'
);

This creates a new session at the graph’s root, node 1. The application has supplied false because it has already determined that the order is outside the delivery area. In a real checkout, calculate this value from the order rather than hard-coding it.

The returned SessionStateDTO is the SDK’s representation of the session. Its sessionId identifies this execution, nodeId tells you where it is, and isTerminal tells you whether navigation has ended. The payload contains the text and choices for the current position.

Here $state->payload is a NodePayloadDTO. It has the question in title and two entries in edges: collection is enabled, while home delivery is blocked. That is already enough information for an application to prepare its interface.

Your application does not need to expose every SDK field to its template. It can select the fields its interface uses:

$before = [
'heading' => $state->payload->title,
'choices' => array_map(
static fn (array $edge): array => [
'edgeId' => $edge['id'],
'label' => $edge['labels'][0]['text'],
'enabled' => $edge['enabled'],
],
$state->payload->edges
),
];

Each choice carries the edge ID to submit, the localized label to display and whether the control should be enabled. For this graph, every edge has a label in the requested language; labels[0] contains that resolved label.

The array is ordinary application data, not another ExecKernel type. A template can use heading for the question, render choices as buttons and disable the home-delivery button. When rendering the strings as HTML, escape them using the template’s normal escaping facilities.

Illustrative application view before and after navigation. At node 1, the collection button sends edge 10 and the home-delivery button is disabled because delivery_available is false. After choosing edge 10, node 2 is terminal and the application displays the collection title and the instruction to bring the order number.

Illustrative interface built from the returned data. ExecKernel supplies the data; your application supplies the interface.

Suppose the customer selects collection. The corresponding edge ID is 10, so the application requests that transition:

$state = $client->navigate($state, 10);

The client returns a new session object. Assigning it to $state makes subsequent code use the result of the transition. The engine validates the edge against the current session; disabling a button is a presentation choice, not a substitute for that check.

The session is now at node 2, and isTerminal is true. Its payload is a TerminalPayloadDTO, with the terminal’s title and outputs in place of further choices:

$after = [
'heading' => $state->payload->title,
'messages' => array_column($state->payload->outputs, 'content'),
];

The delivery graph uses only content outputs, so this extracts the message “Bring your order number when you collect your order.” An application can show it in a confirmation panel. Recording the delivery method on an order or arranging collection remains application work.

Save this as local-delivery.php, or download the complete PHP example. It creates the two application views described above and prints them as JSON so you can inspect the data a controller would pass to a template.

Complete local-delivery.php
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use ExecKernel\Client\ExecKernelLocalAdapter;
use ExecKernel\Client\ExecKernelLocalClient;
use ExecKernel\DTO\NodePayloadDTO;
use ExecKernel\DTO\TerminalPayloadDTO;
use ExecKernel\Infrastructure\Engine\ExecKernelFactory;
use ExecKernel\Infrastructure\Loader\LocalGraphLoader;
use ExecKernel\Infrastructure\SessionStore\InMemorySessionStore;
$engine = (new ExecKernelFactory())->build();
$loader = new LocalGraphLoader(basePath: __DIR__ . '/graphs');
$store = new InMemorySessionStore();
$client = new ExecKernelLocalClient(
new ExecKernelLocalAdapter($engine, $loader, $store)
);
$state = $client->start(
'delivery-options',
['delivery_available' => false],
getenv('EXECKERNEL_LANG') ?: 'en'
);
if (!$state->payload instanceof NodePayloadDTO) {
throw new LogicException('Expected delivery choices.');
}
$before = [
'heading' => $state->payload->title,
'choices' => array_map(
static fn (array $edge): array => [
'edgeId' => $edge['id'],
'label' => $edge['labels'][0]['text'],
'enabled' => $edge['enabled'],
],
$state->payload->edges
),
];
$state = $client->navigate($state, 10);
if (!$state->payload instanceof TerminalPayloadDTO) {
throw new LogicException('Expected a collection result.');
}
$after = [
'heading' => $state->payload->title,
'messages' => array_column($state->payload->outputs, 'content'),
];
echo json_encode(
['before' => $before, 'after' => $after],
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR
), PHP_EOL;

Run it from your project directory:

Terminal window
php local-delivery.php

The before object contains the question and two choices: edge 10 has enabled: true and edge 20 has enabled: false. The after object contains “Collection selected” and the collection instruction. To use the Spanish text from the same graph, run EXECKERNEL_LANG=es php local-delivery.php.

For another useful experiment, change delivery_available to true and the selected edge to 20. Both initial choices will be enabled, and the final view will ask the customer to confirm the delivery address. Changing only the edge while leaving coverage set to false produces a rejected transition.

The graph name identifies a reusable definition; the session ID identifies one execution. If a customer returns in a later request, retain the session ID in your application’s own session or order record and ask the client to resume it:

$state = $client->resume($sessionId, 'en');

Here $sessionId means the ID previously returned in $state->sessionId. This works only if the corresponding state is still in the configured store. Recreating an in-memory store in a later PHP request gives you an empty store, so a multi-request application needs persistent session storage.

That storage choice is independent of the graph and its presentation. You can keep the same pattern—start or resume, render the payload, request a chosen edge—when integrating ExecKernel into a controller or service in your application.